r/learnSQL • u/Possible_Seaweed_916 • 1d ago
What SQL concept did you understand in tutorials but mess up in practice?
For me, joins are the best example of this. The diagram makes perfect sense. Then you join two tables, get twice as many rows as you expected, and suddenly you’re questioning everything.
What was that topic for you? And what finally made it click?
18
Upvotes
3
u/sud1890 1d ago
Create Procedure and Create function
4
u/Discordbothost 1d ago
Joins too. What clicked: it's not a lookup, it's "match every left row against every right row where the condition holds." If the key isn't unique on one side, you get a row per match. Checking for duplicate keys first killed the "why 2x rows" surprises.
1
u/QPGMR_de 1d ago
Recursive CTEs - that took a bit to wrap my head around that.
And LAG/LEAD with window functions - also not the easy stuff to get it right.
1
u/shine_on 1d ago
I was writing a merge statement today and I knew that some of the columns can contain NULLs so I was using isnull(<columnname>,'') on both sides of the comparison. However some of the columns were numeric, and doing that isnull on a numeric column results in zero being output, not an empty string. So it was returning two rows, the one with NULL in that column, and the one with zero in that column. Changing the comparison to be isnull(<columnname>,-1) for the numeric columns fixed it, but it took me half an hour to work out where I was going wrong, and I've been working with SQL for over 20 years now.
Earlier this week I was calculating percentages and I forgot to multiply the result by 1.0, so all the results came out as 0.0000 which was the correct number but rounded to the nearest integer.
I always have to look up the syntax for the merge command. Also lead and lag because I don't use them too often.
1
u/Proof_Escape_2333 1d ago
I still dont understand correlated subquery
1
u/Oh_Another_Thing 1d ago
Self joins, and then you filter them, and then you need you need to left or right join it, and it's done combination of several things to get the results you want, and it does not come intuitively at all.
1
u/navicat-gu 20h ago
Joins are mine too, specifically the fan-out version. Join customers to orders and to payments, SUM both, and every number is multiplied, because each order row pairs with each payment row. The fix is to aggregate each child table in its own CTE first, then join the totals.
A few others that bit me in real work:
WHERE o.status = 'paid'throws away the NULL rows the LEFT JOIN kept. Put that condition in theONclause instead.NOT EXISTSinstead.BETWEEN '2026-12-01' AND '2026-12-31'misses everything after midnight on the 31st. Use>= '2026-12-01' AND < '2027-01-01'.The habit that catches most of these: check the row count after each join, and ask "should this be one row per customer / order / day?" before you aggregate.
I work on Navicat, and showing the row count next to every result is something I lean on in Navicat 18 (Navicat Premium Lite is free to try). Any client that shows it works, though. The habit is what matters.
Disclosure: RC, Director of Digital Strategy @ Navicat.