What is the most magical and mesmerizing code you have ever seen? by Tim Boudreau
Answer by Tim Boudreau:
The bit of SQL in(internet archive version linked), accurately described as “one of the great mind-blowing SQL queries”:SELECT s1.article, s1.dealer, s1.price FROM shop s1 LEFT JOIN shop s2 ON s1.article = s2.article AND s1.price < s2.price WHERE s2.article IS NULL;
At a glance, you think it would return nothing every time. But no. And like a zen koan, once you understand what it does, and why it does what it does, you gain enlightenment – a deeper understanding of what SQL really is, how it relates to set theory and why that matters if you want to master it.