SQL Golf: 5 SQL Tricks You Should (Probably) Never Use
This title could be clearer and more informative.Try out Clickbait Shieldfor free (5 uses left this month).
SQL golf challenges developers to write the shortest possible queries. This article explores five extreme techniques from the Quackmas 2025 competition: column position references (#N), boolean math for counting (sum(condition)), the mode() function to replace GROUP BY/ORDER BY/LIMIT, comma join syntax, and DuckDB-specific shortcuts like FROM-first syntax and QUALIFY. While these tricks produced winning solutions as short as 36 characters, they sacrifice readability for brevity. Some techniques like mode() and QUALIFY are genuinely useful in production, while others like #N column references and whitespace elimination should stay in competitions.
Table of contents
How we got hereThe basics: every character countsBoolean math: the trick that makes you feel smartThe mode() trick: replacing three clauses with one functionJoin golf: comma syntax is backDuckDB shortcuts you might not know aboutThe subtraction trick: my favorite solutionThe final leaderboardShould you golf in production?Try it yourselfSort: