Stop 15 · Interview PreparationCoding & SQL · 2/3

SQL Coding Questions

Part of the Data Science Roadmap

In short

Almost every data science interview has a SQL round, even for ML-heavy roles. Joins, GROUP BY and window functions come up again and again, so practise them until they're boring.

Live SQL exercises testing joins, aggregations, and window functions from Phase 4 — a near-universal component of data science interviews, even at ML-heavy companies.

The mascot stands on a small ladder beside a bookshelf, holding a stack of books.

Think of it like this

The smart librarian

A good librarian doesn't bring every book to her desk and then sort through them. She knows the shelf and row and picks just the three you need. SQL rounds test the same skill: filter, join and group in the query so the database does the work.

Watch it happen

The order SQL actually runs in

FROM/JOIN
WHERE
GROUP BY
HAVING
SELECT
ORDER BY

1/6First the tables are picked and joined. Watch for joins that multiply rows.

The big ideas

What you need to know

Know the run order

It explains most errors, like why WHERE can't use a column alias or a window function.

Joins and duplicates

Before joining, ask: is this one-to-one or one-to-many? Unexpected duplicates inflate sums.

Window functions

ROW_NUMBER, RANK, LAG and SUM() OVER handle 'first order', 'previous month' and running totals.

NULLs are sneaky

COUNT(*) counts rows; COUNT(col) skips NULLs. NULL = NULL is not true.

See it in code

Walk through the code

sql
1WITH ranked AS (2  SELECT3    customer_id,4    order_date,5    amount,6    ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date) AS rn7  FROM orders8)9SELECT customer_id, order_date AS first_order_date, amount10FROM ranked11WHERE rn = 1;

1/3Number each customer's orders by date, restarting at 1 for every customer.

'Find each customer's first order' is an interview favourite. A window function inside a CTE handles it cleanly.

Save yourself the pain

Common mistakes

Oops

Writing WHERE ROW_NUMBER() OVER (...) = 1 in a single query.

Do this instead

Compute the window in a CTE or subquery, then filter in the outer query.

Oops

LEFT JOIN, then a WHERE condition on the right table's column.

Do this instead

That drops unmatched rows and acts like an INNER JOIN. Put the condition in the ON clause instead.

Your mission

Four classic queries

~35 min

Quick check

Why can't you write WHERE ROW_NUMBER() OVER (...) = 1 directly in one query?

Go deeper

  1. 1Practice on a SQL interview question platform under light time pressure.
  2. 2Review joins and window functions specifically — the most commonly tested topics.
  3. 3Talk through your query logic out loud before writing it, even in practice.

More coding & sql at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge