r/learnSQL • u/thequerylab • Mar 09 '26
If you have an SQL interview soon, don’t ignore these small things!!!!
I’ve noticed something about SQL interviews.
Most people don’t fail because they don’t know SQL.
They fail because they forget tiny things while typing under pressure. It's pressure!!!
Few examples I’ve seen in real interviews:
1. COUNT(column) vs COUNT(*)
If the column contains NULL values:
COUNT(column)→ ignores NULLsCOUNT(*)→ counts every row
So if someone asks “how many rows are there?”, COUNT(column) can give the wrong number!
2. LEFT JOIN + WHERE trap
Example:
SELECT *
FROM orders o
LEFT JOIN payments p
ON o.id = p.order_id
WHERE p.status = 'success'
The WHERE condition removes rows where p.status is NULL.
So the LEFT JOIN effectively behaves like an INNER JOIN.
To keep the LEFT JOIN behavior, the condition usually goes in the ON clause.
3. Using DISTINCT to hide join problems
Sometimes joins create duplicates because the relationship isn’t 1-to-1.
A lot of people just do:
SELECT DISTINCT ...
But interviewers usually want you to explain why duplicates appeared in the first place.
- WHERE vs HAVING
WHERE filters rows before grouping.
HAVING filters after GROUP BY.
So something like this won’t work:
WHERE COUNT(*) > 5
It needs to be:
HAVING COUNT(*) > 5
These are all very small things and basics, but they come up surprisingly often in interviews.
Curious what others have seen.
What’s a small SQL thing people still mess up in interviews even though they know it?
Always interesting to hear these and your interview experiences.
17
u/ComicOzzy Mar 09 '26
Counting the number of total rows when you should have been counting the number of matching rows.
Let's say you wanted to count the number of orders placed per customer. Some customers may have signed up on your site, but they've placed no orders. You don't want to exclude customers with 0 orders, so you use a LEFT JOIN...
But I've seen people write this:
When they should have written this:
Because COUNT(*) is counting rows rather than non-NULL values, it will count the customers with no orders as having 1 order because it counted the row that had no matching order.
28
u/xaomaw Mar 09 '26 edited Mar 09 '26
Example for your 2. LEFT JOIN + WHERE trap
Instead of:
You might want:
P.S:
Use "SELECT o.id, p.status" instead of "SELECT *" 😁
1
u/thequerylab Mar 25 '26
If you’re preparing seriously, this is something worth trying. It’s designed to help you get interview-ready in approx 30 days with structured practice, and I’ve been getting really positive feedback from people who’ve used it
https://thequerylab.com/courses/sql-interview-preparation-kit
5
u/Top-Run-21 Mar 09 '26
Hey thank you it's really helpful
guys let's continue if anyone has more tips.
4
u/Mrminecrafthimself Mar 09 '26 edited Mar 09 '26
My team lead would be drooling over the “DISTINCT” one. We inherited a lot of code from other teams that used DISTINCT, GROUP BY, UNION, and QUALIFY all in one query. Guess that dev really wanted unique rows
This kind of stuff is why I roll my eyes at people saying “bro you can master SQL in a couple days/weeks.” Like yeah you can figure out the basics and syntax of it in a few days or weeks. Of course. The syntax is pretty simple and straight forward.
What you will not have after a few weeks is the deeper understanding of how the data is actually being manipulated behind the scenes. How many rows your query actually creates on the backend before it returns the final output. Whether you’re inadvertently duplicating or excluding things. How to avoid spool space errors and reduce runtimes.
2
u/Better-Credit6701 Mar 09 '26
I've had interviews that didn't include other DBAs, only the development team that was causing the issues by bad design and coding. They didn't like me pointing out the issues they were having.
2
u/TheMagarity Mar 09 '26
1 needs to be questioned during an interview because count(col) combined with group by col will list nulls among the output. Only if it is strictly count(col) are the results potentially off from nulls.
2
u/wyliec22 Mar 09 '26
When I interviewed candidates for SQL positions, I focused more on the logic used than precise syntax.
2
u/Interesting_Drop_396 Mar 24 '26
Query Execution Order - Basic question but almost half of junior developers fail in this
2
u/adastra1930 Mar 09 '26
Number 3 has me screaming!! A DISTINCT on a query is almost always a red flag that the person who wrote it does not understand how data is structured, and the rest of their query and logic is likely garbage. I run into so many of these legacy queries at work and they fill me with dread 😫
1
u/Born-School778 Mar 09 '26
How to avoid using Distinct. Please?
1
u/Curious-Dingo5412 Mar 10 '26
Perfect
1
u/Spiritual_Dealer_640 Mar 10 '26
RemindMe! 1 Day
1
u/gidiyup Mar 11 '26
RemindMe! 4 Days
1
u/ElegantPianist9389 Mar 13 '26
Reminder! 3 Days
1
u/Educational_Ad9289 Mar 27 '26
Can anyone help me to teach me SQl from starting
1
u/Inevitable_Paper_776 Mar 29 '26
that's a great tutorial right there! you can try learning in a conversational way with AI and with your data locally or public. Give Ruddy a try.