# SQL Execution Order Mnemonics #SQL To help myself memorize the order in which SQL statements are executed, I've crafted a mnemonic that encapsulates the execution order in a fun and easy-to-remember way: ``` FRowning, JOyless, WHiney, GRanny is HAving SEafood WIth DIsintinctivy ODerous LIme (From, Join, Where, Group By, Having, Select, Window, Distinct, Order by, Limit) ``` This quirky sentence represents the SQL execution order as follows: - **From** (FRowning) - **Join** (JOyless) - **Where** (WHiney) - **Group By** (GRanny) - **Having** (HAving) - **Select** (SEafood) - **Window functions** (WIth) - **Distinct** (DIstinctively) - **Order by** (ODerous) - **Limit** (LIme) I designed mnemonics so that the first two letters match SQL keywords. Here's the dalle3 generated image of above ![[sql-mnemonics.png]] Let's break down each part of the mnemonic and explain the corresponding SQL clauses: 1. **FRowning** - `FROM`: The `FROM` clause specifies the table or tables from which the data will be retrieved. It is the first clause to be executed in the query. 2. **JOyless** - `JOIN`: The `JOIN` clause is used to combine rows from two or more tables based on a related column between them. It is executed after the `FROM` clause. 3. **WHiney** - `WHERE`: The `WHERE` clause filters the rows based on a specified condition. It is executed after the `FROM` and `JOIN` clauses. 4. **GRanny** - `GROUP BY`: The `GROUP BY` clause is used to group the result set by one or more columns. It is executed after the `WHERE` clause. 5. **HAving** - `HAVING`: The `HAVING` clause is used to filter the grouped rows that satisfy a certain condition. It is similar to the `WHERE` clause but operates on the grouped rows instead of individual rows. It is executed after the `GROUP BY` clause. 6. **SEafood** - `SELECT`: The `SELECT` clause is used to specify the columns to be included in the result set. It is executed after the `FROM`, `JOIN`, `WHERE`, `GROUP BY`, and `HAVING` clauses. 7. **WIth** - `WINDOW`: The `WINDOW` clause is used to define a window or a set of rows over which an aggregate function or a window function operates. It is executed after the `SELECT` clause. 8. **DIsintinctivy** - `DISTINCT`: The `DISTINCT` keyword is used to remove duplicate rows from the result set. It is executed after the `SELECT` and `WINDOW` clauses. 9. **ODerous** - `ORDER BY`: The `ORDER BY` clause is used to sort the result set based on one or more columns. It is executed after the `SELECT`, `WINDOW`, and `DISTINCT` clauses. 10. **LIme** - `LIMIT`: The `LIMIT` clause is used to restrict the number of rows returned in the result set. It is the last clause to be executed in the query. By remembering the mnemonic "FRowning, JOyless, WHiney, GRanny is HAving SEafood WIth DIstinctively ODerous LIme," you can easily recall the SQL execution order, which is crucial for both understanding how SQL queries are processed and for optimizing query performance. ## Why Though To illustrate the practical benefits of knowing SQL execution order, consider the following SQL query example: ```SQL WITH PART AS ( SELECT TIMESTAMP, SAMPLE_IDENTIFIER FROM TABLE_WITH_BILLION_ROWS WHERE TIMESTAMP >= '2024-03-10' AND TIMESTAMP < '2024-03-11' ) , DEDUPED_DATA AS ( SELECT DISTINCT PART.TIMESTAMP, FACT_TABLE_WITH_BILLION_ROWS.* -- Use DISTINCT to eliminate duplicates FROM FACT_TABLE_WITH_BILLION_ROWS INNER JOIN PART USING (SAMPLE_IDENTIFIER) ) SELECT * FROM DEDUPED_DATA; ``` Getting a handle on the SQL execution order really clears up why this query doesn't bog down, even though it's dealing with huge tables. If you didn't know better, you'd think running this kind of query on tables with billions of rows would take forever. But that's not the case; SQL first grabs the data (`FROM`), then merges it with other data (`JOIN`), weeds out any repeats (`DISTINCT`) from ==that already trimmed-down set==. ## Related Resources - [SQL queries don't start with SELECT](https://jvns.ca/blog/2019/10/03/sql-queries-don-t-start-with-select/) by Julia Evans - https://x.com/ValentinaCupac/status/1556906177686036480?s=20 - https://x.com/SecurityTrybe/status/1721066424586899543?s=20