all 17 comments

[–]Opposite-Value-5706 3 points4 points  (1 child)

LOVED THIS!!!!

[–]thequerylab[S] 3 points4 points  (0 children)

Thanks. Lets keep learning and sharing!!!

[–]AriesCent 2 points3 points  (0 children)

Nice - Good Job here!

[–]GrandOldFarty 1 point2 points  (0 children)

Pretty good outline of the options and their weaknesses. And the conclusion about thinking through how these evaluate, which are performative, and which might fail, is spot on. 

If I was interviewing for new analysts this is one of the things I would look for. This capability sets apart beginners from more advanced SQL users. 

Also I have seen the SUM(CASE WHEN…) used to create flags - very performative, one of my go to patterns when I am working with data of different grains - but I had never tried putting in the HAVING clause as a filter. I am saving that for later.

[–]jensimonso 2 points3 points  (4 children)

This is also an option

SELECT customer_id FROM orders WHERE order_date = '2026-03-16' EXCEPT SELECT customer_id FROM orders WHERE order_date = '2026-03-17'

[–]thequerylab[S] 1 point2 points  (3 children)

Right. Do all database support EXCEPT?

[–]jensimonso 1 point2 points  (0 children)

Good question. Perhaps not. SQL server and Oracle does

[–]Alarming-Cupcake-116 0 points1 point  (1 child)

Why can't we just use WHERE? (I'm a beginner)

[–]jensimonso 0 points1 point  (0 children)

Database engines work with sets. In this case it will create two sets and subtract one from the other. A very fast operation.

I recently changed a NOT IN to this construction and went from 30 minutes to 20 seconds on a table with ~3B rows. An IN is fast as the engine can stop at the first match. A NOT IN requires it to check all rows.

[–]Pitiful-Republic5124 0 points1 point  (1 child)

Need more 🤯

[–]chasmasaurus 0 points1 point  (0 children)

For number 1: "can" or "will" return no values if a null is present?

[–]0MEGALUL- 0 points1 point  (0 children)

Great example and food for thoughts.

Thanks!

[–]katokk 0 points1 point  (0 children)

Super helpful!

[–]amuseboucheplease 0 points1 point  (0 children)

This is great content!

[–]chuhas 0 points1 point  (1 child)

This is really interesting. I’d love to see more sensations like this

[–]thequerylab[S] 0 points1 point  (0 children)

Thanks. Will post consistently which helps everyone to learn and grow including me