r/learnSQL • • 2d 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?

16 Upvotes

14 comments sorted by

3

u/sud1890 2d ago

Create Procedure and Create function

3

u/Ok_Carpet_9510 1d ago

Create function and create procedure are technically not part of SQL. They're procedural language extensions to SQL.

It's been a while since I last wrote any but if remember correctly, a procedure generally is not required to return a value, a function must return a value.

You might use a procedure to increase employee salaries.

You might use a function to return the average, minimum, or maximum salary.

1

u/Possible_Seaweed_916 2d ago

real for creating ctes

4

u/Discordbothost 2d 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.

2

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.

2

u/Possible_Seaweed_916 1d ago

right quite confusing though

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 1d 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:

  • LEFT JOIN + WHERE on the right table quietly turns into an INNER JOIN, because WHERE o.status = 'paid' throws away the NULL rows the LEFT JOIN kept. Put that condition in the ON clause instead.
  • NOT IN with a NULL in the subquery returns no rows at all. Use NOT EXISTS instead.
  • COUNT(*) vs COUNT(col) vs COUNT(DISTINCT col) give three different answers as soon as there are NULLs or duplicates.
  • BETWEEN with timestamps: 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.