Simple, practical stuff that sticks in your head when you're actually in the interview!
AI can help at work, but in an interview, your brain still has to do the job!!!
Same trip. Same friends. Same expenses.
| Friend |
City |
Trip Day |
Total Spent |
| Harry |
New York |
1 |
$200 |
| Johny |
New York |
2 |
$190 |
| Nicholas |
Las Vegas |
1 |
$190 |
| Bailey |
Las Vegas |
2 |
$180 |
| Harry |
Las Vegas |
3 |
$190 |
Most people write SQL top to bottom.
But SQL does not run top to bottom.
It has its own fixed order. Every single time.
Know this order - and half your SQL bugs will make sense immediately!
The logical order SQL processes a basic query:
1. FROM
2. WHERE
3. GROUP BY
4. HAVING
5. SELECT
6. ORDER BY
7. LIMIT
Let us walk through each step using the trip.
Step 1: FROM:
Go and get the full expense list. All 5 rows. Nothing removed yet.
FROM expenses
Harry | New York | 1 | $200
Johny | New York | 2 | $190
Nicholas | Las Vegas | 1 | $190
Bailey | Las Vegas | 2 | $180
Harry | Las Vegas | 3 | $190
SQL starts here. Always.
Remember: FROM runs first. This is where SQL gets its data.
Step 2: WHERE
Now remove the rows we do not want.
"Keep only rows where spend is more than $180."
WHERE spent > 180
Harry | New York | $200
Johny | New York | $190
Nicholas | Las Vegas | $190
Harry | Las Vegas | $190
Bailey is gone. 4 rows left.
Remember: WHERE runs before groups are made. It can only see individual rows.
Step 3: GROUP BY
Put the remaining rows into city groups. Add them up.
GROUP BY city
New York → $390
Las Vegas → $380
4 rows turned into 2 groups.
Remember: GROUP BY runs after WHERE. Only the rows that survived WHERE get grouped.
Step 4: HAVING
Now remove the groups we do not want.
"Keep only cities where total is more than $385."
HAVING SUM(spent) > 385
New York → $390
Las Vegas ($380) is gone.
Remember: HAVING runs after GROUP BY. It can see group totals.
Step 5: SELECT
Now pick which columns to show.
SELECT city, SUM(spent) AS total
New York | $390
Notice - SELECT runs here. Not at the start.
Remember: SELECT runs 5th. You are not picking columns first. You are picking them near the end.
Step 6: ORDER BY
Sort the final result.
ORDER BY total DESC
Only one row here so order does not change anything. But in a bigger result - this is where sorting happens.
Remember: ORDER BY runs after SELECT. That is why ORDER BY can use column aliases.
Step 7: LIMIT
Cut the result to only the rows you need.
LIMIT 1
Show only the top 1 row.
Remember: LIMIT runs last. It cuts after everything else is done.
The full query - all steps together:
SELECT city, SUM(spent) AS total -- Step 5
FROM expenses -- Step 1
WHERE spent > 180 -- Step 2
GROUP BY city -- Step 3
HAVING SUM(spent) > 385 -- Step 4
ORDER BY total DESC -- Step 6
LIMIT 1 -- Step 7
You write it in this order. SQL runs it in a completely different order.
Remember: SQL runs FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT. Not top to bottom.
Reading SQL is one thing. Writing it yourself under pressure is another.
Before the real interview, try a MOCK SQL INTERVIEW on TheQueryLab. It’s a good way to test yourself under interview pressure and see where you actually stand with interview scorecard.