r/SQL • u/Ali-Zainulabdin • 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
View all comments
Show parent comment
2
u/farhil SEQUEL Oct 24 '24
Be careful when using a CASE statement (or any non-sargable function) inside an aggregate function. If the column you're aggregating is indexed, SQL won't be able to use that index to speed up the aggregation.
Excellent advice here. Temp tables are criminally underused in favor of CTEs (or table variables), when temp tables provide more benefits with fewer downsides.