SQL for Data Analysts: The Queries Employers Actually Test
Data analyst SQL interviews repeat the same patterns: filtering, joins, grouping, and window functions. Here are the seven queries employers test.
By Galactic News Editorial · Published 2026-11-17T16:28:41+00:00 · Updated 2026-11-17T16:28:41+00:00
Data analyst interviews test SQL more than any other skill. The questions are rarely exotic. They repeat the same patterns: filtering, joining, grouping, and ranking rows. This guide covers the queries employers actually test, with working examples you can practise on any database.
What SQL interviews for data analysts actually test
Most data analyst SQL interviews cover five areas. Basic querying and filtering comes first. Aggregation with GROUP BY and HAVING comes second. JOINs and NULL handling come third. Subqueries and CTEs come fourth. Window functions come fifth.
Interviewers usually test these skills through business-style problems. Common examples include top products per category, month-over-month growth, and customers who never ordered. The data is small. The thinking is what matters.
Learn the ideas in layers. Start with SELECT, WHERE, and ORDER BY. Then add aggregates and GROUP BY. Then JOINs. Then subqueries and CTEs. Finish with window functions. Practise every step on a real database, not just by reading.
The seven query patterns that come up again and again
These seven patterns cover most of what an interviewer will ask you to write. Learn them until they feel automatic.
1. Filter rows, then filter groups
WHERE filters rows before grouping. HAVING filters groups after aggregation. Mixing them up is one of the most common interview mistakes.
SELECT customer_id, SUM(amount) AS total
FROM orders
WHERE status = 'completed'
GROUP BY customer_id
HAVING SUM(amount) > 10000;This query totals completed orders per customer. It keeps only customers who spent more than 10,000. The WHERE clause runs first. The HAVING clause runs after the grouping.
2. Find rows with no match
A LEFT JOIN keeps every row from the left table. Rows with no match get NULL on the right side. That makes it the standard way to find missing records.
SELECT c.customer_id, c.name
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.customer_id
WHERE o.order_id IS NULL;This finds customers who never placed an order. The same pattern finds products never sold and employees with no assigned project.
3. Aggregate by group
GROUP BY collapses rows into one row per group. Aggregate functions such as COUNT, SUM, and AVG then calculate one value per group.
SELECT product_category, SUM(amount) AS total_sales
FROM sales
GROUP BY product_category;Only grouped columns and aggregate expressions can appear in the SELECT list with GROUP BY. Selecting a plain column that is neither grouped nor aggregated is an error in standard SQL.
4. Rank rows without collapsing them
Window functions calculate across related rows while keeping every row. They use an OVER clause to define the window. This is the single most tested advanced topic.
SELECT product_name, amount,
RANK() OVER (ORDER BY amount DESC) AS sales_rank
FROM sales;ROW_NUMBER gives every row a unique number. RANK gives tied rows the same rank and skips the next number. DENSE_RANK gives tied rows the same rank with no gaps.
5. Top N per group
Interviewers love this one. Find the top three products in each category, or the top earner in each department. The pattern is a CTE with ROW_NUMBER, then a filter.
WITH ranked AS (
SELECT employee_name, department, salary,
ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
)
SELECT employee_name, department, salary
FROM ranked
WHERE rn <= 3;PARTITION BY restarts the numbering for each department. ORDER BY inside OVER controls the ranking order. The outer query keeps only the top three rows per group.
6. Compare a row with the previous row
LAG reads a value from the previous row. LEAD reads the next row. They power month-over-month growth questions.
SELECT month, revenue,
LAG(revenue) OVER (ORDER BY month) AS prev_month_revenue
FROM monthly_sales;Subtract the previous month from the current month to get growth. This pattern also finds gaps, streaks, and first-time events.
7. Find duplicates
Group by the key and keep groups with a count above one. Then ask what caused the duplicates before deleting anything.
SELECT order_id, COUNT(*) AS copies
FROM orders
GROUP BY order_id
HAVING COUNT(*) > 1;Duplicates often point to a pipeline bug. Mention that in the interview. It shows you think about data quality, not just syntax.
SQL for data analysts: window functions, the advanced differentiator
Window functions separate strong candidates from the rest. The PostgreSQL documentation defines them clearly. A window function performs a calculation across a set of table rows that are somehow related to the current row. Unlike a grouped aggregate, window functions do not cause rows to become grouped into a single output row.
That difference is the whole point. GROUP BY collapses rows. A window function adds a calculated column and keeps every row. You can show each employee beside the average salary of their department in one query.
SELECT employee_name, department, salary,
AVG(salary) OVER (PARTITION BY department) AS dept_avg_salary
FROM employees;Remember the placement rules. Window functions are allowed only in the SELECT list and the ORDER BY clause. They cannot appear directly in WHERE, GROUP BY, or HAVING. To filter on a window result, wrap the query in a subquery or CTE first, as in the top-N example above.
Also learn the execution order of a query. It runs FROM and JOIN, then WHERE, then GROUP BY, then HAVING, then SELECT, then DISTINCT, then ORDER BY, then LIMIT. This order explains why you cannot use a SELECT alias inside WHERE.
A four-week practice plan
Four focused weeks is enough to cover the tested material. Practise a little every day rather than in one long session.
| Week | Focus | Daily practice |
|---|---|---|
| 1 | SELECT, WHERE, ORDER BY, LIMIT | Write 10 filtering queries on a sample database |
| 2 | JOINs, GROUP BY, HAVING, NULLs | Solve 10 join and aggregation problems |
| 3 | Subqueries, CTEs, CASE WHEN | Rewrite 5 queries using CTEs instead of subqueries |
| 4 | Window functions and business problems | Solve top-N, running total, and growth problems |
Free practice resources make this plan easy to follow. The Mode SQL Tutorial is a free, text-based course built for analysts. It covers 52 lessons from basic SELECT through window functions. PostgreSQL's official tutorial chapter on window functions is a short, precise reference. SQLBolt offers interactive lessons with a built-in database and no signup.
Use a consistent dialect while you learn. PostgreSQL is a good choice. It is free, widely used, and its documentation is excellent. Syntax for dates and strings differs across MySQL, PostgreSQL, and SQL Server, so adapt examples to the dialect the employer uses.
Bottom line
Employers test a small set of SQL patterns again and again. Filtering, joining, grouping, and ranking cover most interviews. Window functions are the advanced skill that sets candidates apart. Learn the seven patterns above, practise them on real data, and explain your thinking out loud. That is what gets you hired.