ML & Data Science Interviews · SQL & pandas for DS · Lesson 1 of 1
SQL & pandas essentials for interviews
Many DS/analytics interviews include a live SQL (or pandas) round. The recurring patterns are aggregation, joins, window functions, and de-duplication — know these cold.
- GROUP BY + aggregates: counts/sums/averages per key; HAVING filters groups (vs WHERE, which filters rows before grouping).
- JOINs: inner vs left/right/full — and why a LEFT JOIN + IS NULL finds non-matches.
- Window functions:
ROW_NUMBER(),RANK(),LAG()overPARTITION BY ... ORDER BY ...— for top-N-per-group, running totals, period-over-period. - In pandas:
groupby().agg(),merge(),pivot_table(),drop_duplicates(), and boolean masks mirror the same operations.
-- Top earner per department (classic window-function question) SELECT department, name, salary FROM ( SELECT department, name, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rnk FROM employees ) t WHERE rnk = 1;
Reach for a window function'Top N per group', 'running total', and 'compare to previous row' almost always want a window function, not a self-join. Recognizing that instantly is a strong signal in a SQL screen.
◆ Lock it in
- WHERE filters rows, HAVING filters groups; know your JOIN types.
- Window functions solve top-N-per-group, running totals, and period comparisons.
- pandas mirrors SQL: groupby/merge/pivot_table/drop_duplicates.
Feynman drill — say it out loudExplain the difference between WHERE and HAVING, and when you'd reach for a window function.
Step 1 rate your confidence · Step 2 pick your answer
You need the single highest-paid employee within each department. What's the cleanest tool?
Step 1 — how sure are you?