SQL · All levels
SQL Interview Preparation: Joins, Grouping, Subqueries and Window Functions
Practise the SQL patterns asked in analyst and developer interviews — joins, GROUP BY and HAVING, subqueries, window functions and the classic Nth-highest-salary question.
Published 4 Dec 20263 min read
SQL rounds are usually hands-on: you are given tables and asked to write queries. The examples below use two simple tables:
employees(id, name, dept_id, salary, manager_id, joined_on)departments(id, name)
Logical order of execution
Queries are written SELECT … FROM … WHERE … GROUP BY … HAVING … ORDER BY … LIMIT, but are logically processed as:
FROM/JOINWHEREGROUP BYHAVINGSELECTORDER BYLIMIT/OFFSET
This explains why you cannot use a SELECT alias inside WHERE in most databases, and why aggregate filters belong in HAVING.
Joins
| Join | Returns |
|---|---|
| INNER JOIN | Only rows with a match in both tables |
| LEFT JOIN | All rows from the left table, matched rows (or NULLs) from the right |
| RIGHT JOIN | The mirror of LEFT JOIN |
| FULL OUTER JOIN | All rows from both sides |
| CROSS JOIN | Every combination of rows |
| Self join | A table joined to itself (e.g. employee → manager) |
-- Employees with their manager's name
SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;
Q: Find departments with no employees.
SELECT d.name
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.id
WHERE e.id IS NULL;
WHERE vs HAVING
WHERE filters rows before grouping; HAVING filters groups after aggregation.
SELECT dept_id, COUNT(*) AS headcount, AVG(salary) AS avg_salary
FROM employees
WHERE joined_on >= '2023-01-01'
GROUP BY dept_id
HAVING COUNT(*) > 5;
Subqueries and CTEs
Q: Employees earning more than their department's average.
WITH dept_avg AS (
SELECT dept_id, AVG(salary) AS avg_sal
FROM employees
GROUP BY dept_id
)
SELECT e.name, e.salary
FROM employees e
JOIN dept_avg d ON d.dept_id = e.dept_id
WHERE e.salary > d.avg_sal;
A CTE (WITH …) makes multi-step logic easier to read than nested subqueries.
Window functions
Window functions calculate across related rows without collapsing them into one row.
| Function | Ties behaviour |
|---|---|
ROW_NUMBER() |
Unique numbers: 1, 2, 3, 4 |
RANK() |
Gaps after ties: 1, 2, 2, 4 |
DENSE_RANK() |
No gaps: 1, 2, 2, 3 |
Q: Second highest salary (handling ties).
SELECT DISTINCT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) t
WHERE rnk = 2;
Q: Top 3 earners in each department.
SELECT *
FROM (
SELECT e.*, DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
FROM employees e
) t
WHERE rnk <= 3;
Other useful ones: SUM(...) OVER (ORDER BY joined_on) for running totals, and LAG() / LEAD() to compare with the previous/next row.
Finding duplicates
SELECT name, COUNT(*)
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;
NULL handling
NULL = NULLis not true; useIS NULL/IS NOT NULL.- Aggregates like
AVGandCOUNT(column)ignore NULLs;COUNT(*)counts all rows. COALESCE(a, b)returns the first non-NULL value.
DELETE vs TRUNCATE vs DROP
DELETEremoves selected rows (can useWHERE; logged row by row).TRUNCATEremoves all rows quickly, keeping the table structure.DROPremoves the table entirely.
Exact behaviour (e.g. rollback support) varies between database systems — say so in an interview.
Practice checklist
- Nth highest salary, with and without window functions.
- Employees who joined in the last 30 days.
- Month-wise count of hires.
- Customers who placed orders in two consecutive months (use
LAG). - Explain how an index speeds up a
WHEREclause and when it does not help.
- #sql
- #data
- #databases