DEV Community

Cover image for The SQL Mistake Everyone Makes: Writing vs Execution Order
Prashant Patil
Prashant Patil

Posted on

The SQL Mistake Everyone Makes: Writing vs Execution Order

When I first started learning SQL, my approach was very straightforward. I would begin with the SELECT statement, list the columns I wanted, add the FROM clause, and then shape the query based on the problem.

This worked well for simple queries. However, as problems became more complex, I repeatedly ran into the same issue: I had to rewrite queries from scratch because of small mistakes. This wasted time and slowed down my progress.

The Problem

The core issue was simple — I was writing SQL in the order it appears, not in the order it executes.

Because of this, I:

  • Applied filters at the wrong stage
  • Misused aggregate functions
  • Got incorrect results
  • Rewrote queries multiple times

The Turning Point

While watching a developer solve multiple SQL problems, I noticed a pattern:

  • They rarely rewrote queries
  • They followed a structured flow
  • They thought in terms of execution

That is when I realized:

SQL is not executed in the order we write it.

Understanding this changed everything.

SQL Execution Order

The actual execution order of SQL is:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. DISTINCT
  7. ORDER BY
  8. LIMIT

Each step transforms the data before passing it to the next stage.

Clause Breakdown

FROM

Selects the base table and performs joins.

WHERE

Filters rows before grouping.

GROUP BY

Groups rows based on column values.

HAVING

Filters grouped data.

SELECT

Chooses columns and applies calculations.

DISTINCT

Removes duplicates.

ORDER BY

Sorts results.

LIMIT

Restricts number of rows returned.

Example

Table: orders

order_id customer amount city
1 A 100 Pune
2 B 200 Mumbai
3 A 150 Pune
4 C 300 Delhi
5 B 250 Mumbai

Problem

Find total order amount per city where total > 200 and sort descending.

Query

SELECT city, SUM(amount) AS total_amount
FROM orders
WHERE amount > 100
GROUP BY city
HAVING SUM(amount) > 200
ORDER BY total_amount DESC;
Enter fullscreen mode Exit fullscreen mode

Execution Walkthrough

Step 1: FROM

All rows are selected.

Step 2: WHERE

Filter amount > 100.

Remaining rows:

  • Mumbai: 200, 250
  • Pune: 150
  • Delhi: 300

Step 3: GROUP BY

Groups formed:

  • Mumbai
  • Pune
  • Delhi

Step 4: HAVING

Filter groups:

  • Mumbai (450) -> keep
  • Pune (150) -> remove
  • Delhi (300) -> keep

Step 5: SELECT

Result:
| city | total_amount |
|--------|-------------|
| Mumbai | 450 |
| Delhi | 300 |

Step 6: ORDER BY

Sorted descending:

  • Mumbai
  • Delhi

Key Takeaway

Do not think in terms of writing SQL.

Think in terms of execution.

This will:

  • Reduce errors
  • Improve speed
  • Help solve complex queries

Conclusion

Understanding SQL execution order is a fundamental skill. Once you start thinking in execution steps instead of syntax order, your ability to write efficient and correct queries improves significantly.

Top comments (0)