SQL for Data Science: The Queries That Actually Matter (2026)
In this article (7 sections)
SQL is the most underrated skill in data science. Beginners rush past it toward machine learning, then discover on the job that they spend more time *getting and shaping data with SQL* than training models — and that interviewers test it hard because it's a reliable signal of whether you've done real work. Here's the SQL that actually matters, roughly in the order to learn it.
Why SQL, not just pandas?
You can do a lot of the same operations in pandas, so why learn SQL? Because the data lives in databases, and it's far more efficient to filter and aggregate *before* pulling it into Python than to drag millions of rows across and do it locally. In most companies, SQL is simply how you get data. It's not optional.
Level 1 — the everyday core
- SELECT / FROM / WHERE — retrieving and filtering rows.
- ORDER BY, LIMIT — sorting and sampling.
- GROUP BY + aggregate functions — COUNT, SUM, AVG, MIN, MAX. The workhorse of analysis.
- HAVING — filtering *after* aggregation (the distinction from WHERE trips people up).
- CASE WHEN — conditional logic; used constantly for bucketing and cleaning.
Master this level and you can answer most business questions on a single table.
Level 2 — joins & subqueries
Real data is spread across tables, so you must combine them:
- JOINs — INNER, LEFT, RIGHT, FULL. Understand exactly what each keeps and drops; LEFT JOIN behaviour is a top interview topic.
- Subqueries — a query inside a query, for filtering or intermediate results.
- CTEs (WITH clauses) — named subqueries that make complex logic readable. Use these instead of nesting; reviewers love them.
Level 3 — window functions (the differentiator)
Window functions are what separate someone who "knows SQL" from someone who can genuinely work with it — and they're the single most common advanced-SQL interview topic. Learn:
- ROW_NUMBER, RANK, DENSE_RANK — ranking within groups (e.g. top-3 products per region).
- LAG / LEAD — comparing a row to the previous/next one (month-over-month change, churn detection).
- Running totals & moving averages — SUM/AVG OVER with a window frame.
If you can write a query with a CTE and a window function that answers a real business question, you're ahead of most self-taught candidates — and a lot of graduates.
What to skip (for now)
You can defer database administration, stored procedures, triggers, and deep query-optimisation internals. Know that indexes exist and roughly why they speed up queries, and be able to read an EXPLAIN plan at a basic level — that's enough early on.
How to practise
SQL is learned by doing, not reading. Use an interactive platform (there are many free ones), grab a real dataset, and answer questions you actually care about. Aim to reach the point where window functions feel natural. SQL is Stage 2 of the Data Science Roadmap and pairs directly with Python.
Want SQL taught alongside the full data-science stack, with real datasets and mentor review?
See the Data Science course →The bottom line
SQL is where real data-science work begins, and it's tested heavily precisely because it's hard to fake. Get comfortable through joins, CTEs and window functions on real data, and you've built the foundation everything else in the field sits on.
Pankit Kumar has 10 years in Data Science & AI, building and shipping production systems in regulated pharma and clinical environments. He is a freelance trainer at Boston Institute of Analytics, AnalytixLabs and Scaler, and has taught this material to thousands of working professionals.
This article is part of our Data Science programme — 6 months. From data foundations to machine learning, deep learning and deployment.
Explore Data Science