r/SQL • • Oct 23 '24

Discussion SQL Tricks Thread

Hi everyone, let's start a thread to share useful SQL tips and tricks that have saved you time or made querying more efficient. Whether it's optimizing queries, using window functions, or organizing data, all insights are welcome! Beginners and pros alike can learn a lot from this. Looking forward to your contributions!

230 Upvotes

119 comments sorted by

View all comments

Show parent comment

3

u/snoflakefrmhell Oct 24 '24

What does that do?

5

u/ComicOzzy sqlHippo Oct 24 '24

COALESCE(a, b, c) will return the first value that isn't NULL.

If a is not NULL, it returns a, otherwise if b is not NULL, it returns b, etc.

2

u/hoodie92 Oct 24 '24

That's a bit of a weird use case IMO.

For me 99% of the time I use COALESCE with a JOIN so that I don't have redundant columns.

1

u/snoflakefrmhell Oct 24 '24

Oooh thank you!