When I started learning SQL at The Information Lab, one thing I found confusing was that the order in which we write a SQL query isn't the same as the order in which SQL logically processes it. Fortunately, two rather memorable mnemonics involving feminism and smelly feet helped me make sense of it.
Sweaty Feet Will Give Horrible Odors
This is the mnemonic for remembering the order in which we write SQL clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY. Not particularly pleasant, but surprisingly effective.
For example, imagine we're working for a retail company and management wants to know which regions generated at least $500 in completed sales. We could write:

Notice that we start with SELECT, even though we haven't told SQL which table to retrieve the data from. This brings us to the second mnemonic, which is my favorite.
Feminists Will Get Human Society Organized
This one helps us remember SQL's logical processing order: FROM, WHERE, GROUP BY, HAVING, SELECT, and ORDER BY. Certainly more aspirational than the first one, and thankfully, it smells better too.
Let's return to our retail example. SQL logically begins with FROM to identify the orders table. WHERE then filters out orders that weren't completed. GROUP BY organizes the remaining records by region, and HAVING keeps only those regions where the total sales amount reaches at least $500. SELECT determines which columns appear in our results, and ORDER BY sorts them from highest to lowest sales.
Of course, this describes the logical processing order, not necessarily how the database engine physically executes the query. Database engines can optimize the execution plan, but the logical order helps us understand what our query means.
Why Does This Matter?
One of the most useful things about understanding this order is finally making sense of WHERE and HAVING. Both filter data, but they do so at different stages. WHERE filters individual rows before grouping, while HAVING filters groups after aggregation.
For instance, we cannot write WHERE SUM(amount) >= 500 because the groups and their aggregated values haven't been formed at the logical WHERE stage. Instead, we write HAVING SUM(amount) >= 500.
This might seem like a small distinction, but it makes a difference when working with business data. Filtering the wrong records or misunderstanding when aggregation happens can produce misleading results or queries that don't run at all.
Final Thoughts
Something I'm beginning to appreciate about SQL is that memorizing the syntax is one thing, but understanding why it works is another. Once I understood the logical processing order, GROUP BY, WHERE, and HAVING started making considerably more sense. And if you forget everything else from this blog, just remember: sweaty feet will give horrible odors, but feminists will get human society organized. Personally, I know which one I'm rooting for.
And since feminists are apparently going to get human society organized, I might as well recommend Data Feminism by Catherine D'Ignazio and Lauren F. Klein. It's a fascinating book about data, power, and inequality. SQL might help us organize our data, but feminism might help us question who collected it, whose voices are missing, and who benefits from it. Frankly, both could use a little more attention.
