What actually happens when you run an SQL query?

What actually happens when you run an SQL query?

BackerLeader 8 58 104
calendar_todayschedule1 min read

Do you know what actually happens when you run an SQL query?

It’s not as simple as: “SQL just fetches rows from a table.”

Behind the scenes, your query goes through a step-by-step execution order and knowing this is often an interview question for beginners.

Execution Order:

  1. FROM (pick the table)

  2. WHERE (filter rows)

  3. GROUP BY (make buckets)

  4. HAVING (filter buckets)

  5. SELECT (choose what to show)

  6. ORDER BY (sort the results)

  7. LIMIT (control how much to show)

Notice how WHERE comes before grouping, while HAVING comes after grouping.

Notice how ORDER BY is one of the last steps, even though we often write it after SELECT.

Common Confusions:

a) WHERE vs HAVING

  1. WHERE filters rows before grouping.

  2. HAVING filters groups after aggregation.

Example:

WHERE city = 'Delhi' → filter students from Delhi first.

HAVING COUNT(*) > 10 → only keep cities with more than 10 students.

b) GROUP BY vs ORDER BY

GROUP BY → bundles rows into categories (e.g., students per city).

ORDER BY → decides the sequence in which results are displayed (e.g., sort cities by student count).

c) LIMIT with ORDER BY

Important tasks like “find top 3 highest salaries” or “top 5 cheapest products.”

If you found this useful, drop a like or share it with someone preparing for SQL interviews.

Follow for more such insightful posts , I’m breaking SQL down into small, clear lessons you’ll actually remember.

1 Comment

0 votes
🔥 Join developers growing publicly
Share your knowledge, build in public, and grow your developer presence with a global community.

More Posts

5 Things This Playwright SQL Fixture Does So You Don't Have To

vitalicset - Apr 13

What is SQL? A Beginner's Complete Guide

Abdelhakim Baalla - Jul 10, 2025

INNER JOIN vs Correlated Subquery in SQL: What Is Actually Faster?

rozhnev - Jun 22

Working with nested JSON: exploring it, mocking it, and querying it in Postgres

catssaymeow - Jul 3

Getting Started with PostgreSQL: Best Practices from Development to Production

Sunny - Mar 15
chevron_left
6.9k Points170 Badges
Indiayogicode.org
35Posts
97Comments
7Connections
AI Engineer focused on building applications & writing practical guides on coding and machine learning.

Related Jobs

View all jobs →

Commenters (This Week)

4 comments
2 comments
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!