SQL interview problems are usually testing whether you understand the shape of the data, not whether you can type keywords quickly. Before writing a query, identify what one row represents and what the desired result should contain.
Understand the grain
If an orders table has one row per order and an order_items table has several rows per order, joining them changes the number of rows. A count of orders after that join may be inflated unless you count distinct order identifiers or aggregate at the correct stage.
Choose the right join
An INNER JOIN keeps matching records from both sides. A LEFT JOIN preserves every record from the left table and adds matching values when they exist. This is useful for finding customers with no orders, but putting a filter on the right table in the WHERE clause can accidentally turn it back into an inner join.
SELECT c.customer_id, COUNT(o.order_id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.customer_id
GROUP BY c.customer_id;
Aggregate in stages
Use WHERE to filter rows before grouping and HAVING to filter groups after aggregation. When a query becomes difficult to reason about, use a common table expression to name an intermediate result. That makes the grain visible and gives you a place to validate the intermediate row count.
Window functions answer a different question
GROUP BY reduces rows. Window functions calculate across related rows while keeping the detail. Use ROW_NUMBER for a ranked record, LAG for a previous value, and SUM() OVER for a running total. Always define the partition and ordering explicitly.
Performance and safety
Select only the columns needed, filter deliberately, inspect execution plans on realistic data, and index columns that support common selective access patterns. Indexes are not free: they consume storage and add write cost. Never optimise a query before verifying its result.
References
Start with table grain
Write down what one row represents in every table before joining. An orders table may contain one row per order, while an order-items table contains one row per product within an order. Joining them and then summing order-level revenue can multiply the amount by the number of items. Aggregate the item table to the required grain first, or use a distinct business key deliberately.
Build a query in checkpoints
Develop complex SQL in stages. First select the source rows and verify the date filter. Next add the join and compare the row count. Then calculate derived fields, aggregate, and finally add ranking or presentation logic. Common table expressions make these stages visible and easier to test. Name each CTE for its business meaning rather than its implementation detail.
Window functions in practical analysis
Window functions preserve individual rows while adding context such as a customer total, previous transaction or rank within a department. Always specify the partition and ordering that match the question. A running total needs a deterministic order, and a “previous row” calculation needs a tie-breaking column when timestamps are equal.
Performance and safety checks
- Select only columns needed for the result.
- Filter early when it does not change the meaning of the query.
- Inspect the query plan for large joins.
- Use parameters for user-provided values rather than string concatenation.
- Test nulls and empty result sets explicitly.