So far, you’ve learned how to retrieve and filter data.
Now it’s time to make that data useful.
In this article, you’ll learn how to:
This is Part 04 of the SQL from Zero to Pro series.
By default, SQL does not guarantee order.
If order matters, you must explicitly specify it.
SELECT *
FROM employees
ORDER BY last_name;
This sorts results in ascending order by default.
SELECT *
FROM employees
ORDER BY hire_date DESC;
SELECT *
FROM employees
ORDER BY department ASC, hire_date DESC;
What this does:
The LIMIT clause restricts how many rows are returned.
Example
SELECT *
FROM products
LIMIT 10;
Returns only the first 10 rows.
SELECT *
FROM products
ORDER BY price DESC
LIMIT 5;
Plain English:
“Show me the 5 most expensive products.”
Most real-world questions are summary questions, not row-level questions.
Examples:
COUNT – Count Rows
SELECT COUNT(*)
FROM users;
Counts total rows.
SUM – Add Values
SELECT SUM(total_amount)
FROM orders;
Adds up numeric values.
AVG – Average Value
SELECT AVG(price)
FROM products;
Calculates the mean.
MIN and MAX
SELECT MIN(price), MAX(price)
FROM products;
Finds smallest and largest values.
GROUP BY Explained
GROUP BY allows you to aggregate per category.
Example: Count Users by Country
SELECT country, COUNT(*) AS user_count
FROM users
GROUP BY country;
What happens:
Mental Model for GROUP BY
GROUP BY collapses many rows into fewer summary rows.
Filtering Aggregates with HAVING
You cannot use WHERE with aggregate results.
Use HAVING instead.
Example:
SELECT country, COUNT(*) AS user_count
FROM users
GROUP BY country
HAVING COUNT(*) > 100;
Plain English:
“Show only countries with more than 100 users.”
WHERE vs HAVING
| Clause | Filters |
|---|---|
| WHERE | Rows before grouping |
| HAVING | Groups after aggregation |
SQL for Developers
Developers use these features to:
Common patterns:
SELECT COUNT(*)
FROM orders
WHERE status = 'pending';
SQL for Data Analysts
Analysts use aggregations constantly to:
Common patterns:
SELECT department, AVG(salary)
FROM employees
GROUP BY department;
Exercise 1 – Sorting Data
Write a query to:
employeeshire_date from newest to oldestExercise 2 – Limiting Results
Write a query to:
productsExercise 3 – Basic Aggregation
Write a query to:
users tableExercise 4 – GROUP BY
Write a query to:
Bonus Practice
Try combining:
In a single query.
With ORDER BY, LIMIT, and aggregations, SQL stops being about rows and starts being about answers.
If you can:
You’re now doing real-world SQL.
In the next article (SQL 05), we’ll dive into GROUP BY patterns, HAVING in depth, and analytical thinking with SQL.
Happy querying 🚀
Exercise 1 – Sorting Data (Answer)
SELECT *
FROM employees
ORDER BY hire_date DESC;
Explanation
Exercise 2 – Limiting Results (Answer)
SELECT *
FROM products
ORDER BY price ASC
LIMIT 3;
Explanation
Exercise 3 – Basic Aggregation (Answer)
SELECT COUNT(*)
FROM users;
Explanation
Exercise 4 – GROUP BY (Answer)
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
Explanation
Bonus Practice – Combined Query (Example Answer)
SELECT department, COUNT(*) AS employee_count
FROM employees
WHERE active = true
GROUP BY department
HAVING COUNT(*) > 10
ORDER BY employee_count DESC
LIMIT 5;
Plain English Explanation
Key Takeaways
If you can read and write queries like these, you’re officially thinking analytically in SQL.
Next up: SQL 05 – GROUP BY Patterns, HAVING Deep Dive, and Analytical Thinking 🚀