Filtering, Sorting, and Aggregations in SQL (GROUP BY, HAVING) — The Point Where Data Starts…
You’ve already learned how to pull data using SQL.
Filtering, Sorting, and Aggregations in SQL (GROUP BY, HAVING) — The Point Where Data Starts Talking
You’ve already learned how to pull data using SQL.
But here’s the uncomfortable truth most beginners face next:
You can write queries… but you still can’t answer real questions.
Because real-world data problems are never about just selecting rows. They’re about:
- Finding patterns
- Comparing groups
- Identifying trends
- Making decisions
And this is exactly where SQL starts becoming powerful.
This article is Part of the Data Engineering series, and here’s where things shift. You’ll go from writing basic queries… to actually thinking like a data engineer.

This is where SQL starts becoming more than just queries
Why Filtering and Aggregation Matter More Than You Think
Imagine you’re working in a company.
Your manager doesn’t ask: “Show me all orders.”
They ask:
- “Which city generates the most revenue?”
- “How many users signed up this week?”
- “What’s the average order value?”
These are not simple queries. These require:
- Filtering (to narrow down data)
- Sorting (to organize it)
- Aggregation (to summarize it)
If you master these three, you unlock 70% of real-world SQL usage.
WHERE Revisited — Smarter Filtering
You’ve already used "WHERE". But now, we use it with intention.
Let’s say you have an "orders" table:

Example: Filter High-Value Orders
SELECT * FROM orders
WHERE amount > 700;
This gives only orders above 700.
Combine Conditions Like a Pro
SELECT * FROM orders
WHERE city = 'Karachi' AND amount > 600;
Now you’re narrowing data based on multiple conditions.
Useful Filtering Patterns
- Use "IN" for multiple values
WHERE city IN ('Karachi', 'Lahore')
- Use "BETWEEN" for ranges
WHERE amount BETWEEN 500 AND 1000
- Use "LIKE" for pattern matching
WHERE customer LIKE 'A%'
These small tricks make your queries flexible and powerful.
ORDER BY — Making Data Understandable
Raw data is messy. Sorting makes it readable.
Let’s say you want to find your top customers.
SELECT customer, amount
FROM orders
ORDER BY amount DESC;
Now your highest-value orders are at the top.
Sorting by Multiple Columns
SELECT customer, city, amount
FROM orders
ORDER BY city ASC, amount DESC;
This means:
- First sort by city
- Then within each city, sort by amount
This is how reports are built in real companies.
GROUP BY — The Real Game Changer
This is where SQL becomes truly interesting.
"GROUP BY" allows you to combine rows into groups and apply calculations.
Let’s go back to the same "orders" table.
Problem:
Find total revenue per city.
SELECT city, SUM(amount) AS total_revenue
FROM orders
GROUP BY city;
Output:
- Karachi → 1300
- Lahore → 1500
You just summarized data.
Common Aggregation Functions
- "SUM()" → total
- "COUNT()" → number of rows
- "AVG()" → average
- "MIN()" → smallest value
- "MAX()" → largest value
Example: Count Orders per City
SELECT city, COUNT(*) AS total_orders
FROM orders
GROUP BY city;

Grouping turns raw data into meaningful insights
HAVING — Filtering Groups (Advanced but Essential)
Here’s something that confuses almost everyone at first:
- "WHERE" filters rows
- "HAVING" filters groups
Let’s say:
Problem:
Show only cities with revenue greater than 1300.
SELECT city, SUM(amount) AS total_revenue
FROM orders
GROUP BY city
HAVING SUM(amount) > 1300;
This removes smaller groups.
Why Not Use WHERE Here?
Because "WHERE" works before grouping, and "HAVING" works after grouping.
This small difference is very important.
Mini Case Study — Real Data Engineering Thinking
Let’s simulate a real scenario.
You’re working at an e-commerce company.
Task:
Find the top 2 cities with the highest average order value.
Solution:
SELECT city, AVG(amount) AS avg_order_value
FROM orders
GROUP BY city
ORDER BY avg_order_value DESC
LIMIT 2;
What’s happening here:
- "GROUP BY city" → group data
- "AVG(amount)" → calculate average
- "ORDER BY" → sort results
- "LIMIT" → get top 2
This is exactly how business insights are generated.
Putting It All Together
Let’s write a complete query combining everything:
SELECT city, COUNT(*) AS total_orders, SUM(amount) AS revenue
FROM orders
WHERE amount > 300
GROUP BY city
HAVING COUNT(*) > 1
ORDER BY revenue DESC;
This query:
- Filters low-value orders
- Groups by city
- Removes small groups
- Sorts by revenue
This is not beginner SQL anymore. This is real-world SQL.
Common Mistakes Beginners Make
If you’re struggling, it’s normal. Most people get stuck here.
Watch out for these:
- Using "WHERE" instead of "HAVING"
- Forgetting to include columns in "GROUP BY"
- Mixing aggregated and non-aggregated columns incorrectly
- Not understanding execution order
How SQL Actually Executes (Simple Mental Model)
Think of SQL running like this:
- FROM
- WHERE
- GROUP BY
- HAVING
- SELECT
- ORDER BY
- LIMIT
If you understand this flow, most confusion disappears.
Practical Tips That Make You Better Instantly
Start doing these today:
- Always write queries step-by-step
- Test each part separately
- Use meaningful aliases (like "total_revenue")
- Avoid writing everything in one line
- Think in terms of business questions
Why This Skill Changes Everything
At this point, something important happens.
You stop being someone who just writes queries… and start becoming someone who extracts insights.
And that’s the difference between:
- A beginner
- And a real Data Engineer
Because companies don’t pay for syntax.
They pay for answers.

Real data engineers don’t just query data — they understand it
Final Thoughts
Filtering, sorting, and aggregation are not just SQL features.
They are the foundation of how companies:
- Understand users
- Track growth
- Make decisions
So here’s a question for you:
If someone gave you raw data today… could you turn it into something meaningful?
If not, don’t worry. You’re exactly where you should be.
Keep going.
This is Part of the Data Engineering series. In the next article, we’ll go deeper into joins and how data from multiple tables connects together — the real backbone of analytics systems.
Next steps in this series
- Previous Article
- Next Article
메타데이터
- post_id
- 4a6fc2b14bb5
- slug
- filtering-sorting-and-aggregations-in-sql-group-by-having-the-point-where-data-starts-4a6fc2b14bb5
- url
- https://medium.com/towards-data-engineering/filtering-sorting-and-aggregations-in-sql-group-by-having-the-point-where-data-starts-4a6fc2b14bb5
- canonical_url
- https://medium.com/towards-data-engineering/filtering-sorting-and-aggregations-in-sql-group-by-having-the-point-where-data-starts-4a6fc2b14bb5
- author_url
- https://medium.com/@anessulrehman
- status
- ok
- fetched_at
- 2026-06-09 15:37:30