Order of Execution
Order of Execution

Order of Execution

Multi-select
Status
Done

Here's a concise version to remember SQL execution with JOIN:

  1. FROM
  2. JOIN: Combine tables (e.g., join customers and orders).
  3. WHERE: Filter rows (e.g., orders over 50).
  4. GROUP BY: Group the filtered data.
  5. HAVING: Filter groups (e.g., customers with >2 orders).
  6. SELECT: Choose columns to display.
  7. DISTINCT: Remove duplicate rows if needed.
  8. ORDER BY: Sort the results.
  9. LIMIT: Restrict the number of rows returned.
  10. image
SELECT Distinct customer_id, COUNT(order_id) AS total_orders
FROM customers
JOIN orders ON customers.customer_id = orders.customer_id
WHERE orders.amount > 50
GROUP BY customer_id
HAVING COUNT(order_id) > 2
ORDER BY total_orders DESC
LIMIT 5;

Here's a concise table that summarizes the SQL execution order with JOIN, along with the mnemonic for quick recall:

Step
Clause
Purpose
1
FROM & JOIN
Retrieve data and combine tables via JOIN.
2
WHERE
Filter rows before any aggregation.
3
GROUP BY
Group rows for aggregation functions.
4
HAVING
Filter the groups after aggregation.
5
SELECT
Choose the columns to display.
6
DISTINCT
Remove duplicate rows, if necessary.
7
ORDER BY
Sort the final result set.
8
LIMIT
Limit the number of rows returned.

Mnemonic:

Funke Wears Green Hart, Speaking Distinctly Over Lunch

  • Funke: FROM
  • Wears: WHERE
  • Green: GROUP BY
  • Hart: HAVING
  • Speaking: SELECT
  • Distinctly: DISTINCT
  • Over: ORDER BY
  • Lunch: LIMIT