Lesson 0.2
How to answer an interview SQL question
A simple routine that keeps you calm and catches mistakes before the interviewer does.
Imagine an interviewer at Uber asks you a SQL question. Most mistakes come from rushing, not from not knowing SQL.
1Say the question back in one line
Repeat the question in your own words before you write anything. It shows you listened, and the interviewer can correct you early.
Quick check
The interviewer says: "We want to know how busy each city was last month." What's the best way to say it back?
2Decide what the result looks like
Write down the columns and what one row stands for, like "one row per city per day". If you know the shape, the query almost writes itself.
Quick check
"Show completed trips per city for each day of March." What does one row stand for?
3Ask questions that change the answer
Ask about anything that changes the number: ties, NULLs, cancelled rows, the exact date range, and whether to count people or events.
Quick check
"What's the average fare per city?" Which question changes the answer the most?
4Build it one step at a time
Write one small step, run it, and check the row count before adding the next. CTEs (module 4) make each step easy to see.
WITH completed AS ( -- step 1: only completed trips SELECT city, fare FROM trips WHERE status = 'completed' ) SELECT city, ROUND(AVG(fare), 2) AS avg_fare -- step 2: one row per city FROM completed GROUP BY city;
Tip
Talk while you type: "First I'll keep only completed trips, then I'll average per city." If you get stuck, the interviewer can follow your thinking and help.
5Check the result before you say "done"
Look at a few rows and ask: does this make sense? Watch for numbers that are too big (a join that doubled rows) or zero (whole-number division).
Quick check
Your query says the average trip in Chicago costs $9,840. What's the most likely cause?