DEV Community

Cover image for 5 Surprising Truths About Your SQL SELECT Query (And Why They Matter)
Yukti Sahu
Yukti Sahu

Posted on

5 Surprising Truths About Your SQL SELECT Query (And Why They Matter)

If you're learning SQL, you likely started with a simple SELECT statement. You wrote SELECT, then FROM, then maybe a WHERE clause, and hit "execute." It's natural to assume the database reads and runs your code sequentially, just as you wrote it. This top-to-bottom logic seems intuitive, but it's an illusion.

The truth is, the SQL database engine has a hidden, logical order of operations that is completely different from the order in which you write the clauses. This execution pipeline is one of the most fundamental concepts to grasp, as it dictates what you can and can't do in a query. Understanding it separates those who can write basic queries from those who can write powerful, efficient, and bug-free code.

This article will reveal five counter-intuitive but crucial truths about how SQL queries actually work. By the end, you'll think more like the database itself, enabling you to build more sophisticated and accurate queries.


1. Your Query Doesn't Run in the Order You Write It

The most significant surprise for beginners is that the coding order of an SQL query is not its execution order. The way we are required to structure the code for readability and syntax is completely different from the logical steps the database takes to produce the result.

Standard Coding Order:

SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
Enter fullscreen mode Exit fullscreen mode

Actual Logical Execution Order:

  1. FROM (Gets the source data)
  2. WHERE (Filters individual rows)
  3. GROUP BY (Aggregates rows into groups)
  4. HAVING (Filters the aggregated groups)
  5. SELECT (Selects the final columns, creates aliases, and handles DISTINCT)
  6. ORDER BY (Sorts the final result set)
  7. TOP / LIMIT (Restricts the number of rows returned)

Notice how SELECT, which you write first, is one of the last things executed. This explains why you can't use a column alias created in the SELECT clause within a WHERE clause—the WHERE clause is processed long before the SELECT clause is evaluated.


2. WHERE and HAVING Are Filters for Different Stages of Your Data

At first glance, WHERE and HAVING seem to do the same thing: filter rows. However, they operate at completely different points in the query's execution pipeline.

  • WHERE filters individual rows from the source table before any grouping or aggregation. Think of it as a bouncer checking IDs at the door.
  • HAVING filters groups of rows after aggregation. It's like security checking groups that have already formed inside.

You cannot use aggregate functions like SUM(), AVG(), or COUNT() in a WHERE clause because aggregation hasn’t happened yet. Conversely, HAVING is almost always used with GROUP BY, as its purpose is to filter aggregated results.


3. GROUP BY Doesn't Just Group—It Transforms Your Data

The GROUP BY clause does more than just sort or arrange your data—it fundamentally transforms it. It takes multiple rows that share the same value in a specified column and combines them into a single summary row.

Example:

Before GROUP BY:

first_name country
Maria Germany
Martin Germany
Joan USA
Peter USA
George UK

After GROUP BY country:

country
Germany
USA
UK

Rule: Any column in your SELECT list must either be part of an aggregate function (SUM(score), COUNT(customer_id)) or be listed in the GROUP BY clause. Otherwise, the database will throw an error because it doesn't know which value to pick for the collapsed group.


4. You Can Build Powerful Ranked Reports with TOP and ORDER BY

The TOP clause (or LIMIT in other SQL dialects) is often used to get a sample of rows. Its true power is unlocked when combined with ORDER BY. Together, they create ranked reports and allow top-N analysis.

Examples:

-- Top 3 customers with highest scores
SELECT TOP 3 *
FROM customers
ORDER BY score DESC;

-- Lowest 2 customers
SELECT TOP 2 *
FROM customers
ORDER BY score ASC;

-- Two most recent orders
SELECT TOP 2 *
FROM orders
ORDER BY order_date DESC;
Enter fullscreen mode Exit fullscreen mode

Sorting with ORDER BY before using TOP ensures precise control over which rows you retrieve.


5. DISTINCT Is an "Expensive" Tool—Use It Wisely

DISTINCT removes duplicate rows from a result set. For example:

SELECT DISTINCT country
FROM customers;
Enter fullscreen mode Exit fullscreen mode

While useful, DISTINCT can be computationally expensive, because the database often needs to perform a full sort or hash of the result set.

Best practices:

  • Use DISTINCT only when necessary.
  • Avoid using it on columns that are already unique (like primary keys).
  • Redundant use adds unnecessary processing overhead, slowing down queries.

Conclusion: Thinking Like the Database

Becoming an effective SQL practitioner isn’t just about memorizing syntax—it’s about understanding how the database interprets your commands.

By internalizing these truths—realizing that SELECT runs almost last, or that WHERE filters raw rows while HAVING filters aggregated groups—you move beyond just writing queries. You start to anticipate how the database processes your request, allowing you to craft more efficient, accurate, and powerful SQL queries.

Which of these concepts will change the way you write SQL the most?

Top comments (0)