SQL SELECT Statement Deep Dive
Unlocking the full power of data retrieval with SQL
Hi! 👋 Welcome back to our next SQL series.
If you’ve ever worked with databases, the SELECT statement is likely the first SQL command you encountered — and for good reason! It's the core of querying data, helping you fetch, filter, and analyze exactly what you need.
Let’s walk through its features one step at a time, using a sample customers table. We’ll not only see how it works, but also why it’s so powerful in day-to-day data work.
Consider the following table customers
Basic Syntax
SELECT column1, column2
FROM table_name;SELECT name, city
FROM customers;To fetch specific columns from a table, use this structure.
Output:
Alice Mumbai
Bob Delhi
Charlie Mumbai
Diana Kolkata
Eric ChennaiYou can also retrieve all columns using *:
SELECT * FROM customers;Adding WHERE Clause
The WHERE clause lets you filter records based on conditions.
Example:
Find all customers in Mumbai:
SELECT name
FROM customers
WHERE city = 'Mumbai';Result:
Alice 28
Charlie 22We filtered the data to include only rows where the city equals 'Mumbai'. The WHERE clause supports operators like =, >, <, !=, BETWEEN, LIKE, etc.
Sorting with ORDER BY
You can sort results using the ORDER BY clause.
Example:
Sort customers by their total purchases in descending order:
SELECT name, total_purchases
FROM customers
ORDER BY total_purchases DESC;ASC= Ascending (default)DESC= Descending
Result:
Diana 3000.00
Bob 2300.00
Eric 1800.00
Alice 1500.50
Charlie 1100.75📊 Aggregate Example (SUM, AVG)
Aggregates let you perform calculations across rows.
Example:
Find the average age of all customers:
SELECT AVG(age) AS average_age
FROM customers;Output:
average_age
31.2Other common aggregate functions:
SUM(): Total sumCOUNT(): Total number of rowsMAX(): Highest valueMIN(): Lowest value
GROUP BY for Aggregated Insights
Want to know how many customers are from each city? Use GROUP BY.
SELECT city, COUNT(*) AS num_customers
FROM customers
GROUP BY city;Result:
Mumbai 2
Delhi 1
Kolkata 1
Chennai 1Explanation:
COUNT(*)counts the number of rows in each city group.GROUP BYclusters records based on city name.
You can also combine GROUP BY with aggregates like AVG(), SUM(), etc.
What is HAVING in SQL?
The HAVING clause is used to filter grouped records after aggregation. It's like WHERE, but works on aggregated/grouped data.
SELECT column, AGG_FUNC(column2)
FROM table
GROUP BY column
HAVING condition;Example:
SELECT city, COUNT(*) AS num_customers
FROM customers
GROUP BY city
HAVING COUNT(*) > 1;Result:
Mumbai 2Here, HAVING filters groups — it only shows cities with more than 1 customer.
You can't use WHERE here, because the data is already grouped.
💡🚨 Difference Between WHERE and HAVING(Interview Tips!!)
➤ WHERE Clause
- Filters rows before any grouping or aggregation happens.
- Used with
SELECT,UPDATE, orDELETEstatements. - Cannot be used with aggregate functions like
SUM(),COUNT(), etc.
➤HAVING Clause
- Filters grouped data after
GROUP BYis applied. - Must be used with
SELECTthat involves aggregation. - Can use aggregate functions like
AVG(),COUNT(),SUM().
💡🚨 Why is SELECT so Powerful?
The SELECT statement is powerful because it lets you:
Get srija karmakar’s stories in your inbox
Join Medium for free to get updates from this writer.
✅ Retrieve exactly the data you need — rows, columns, or entire tables
✅ Filter rows with WHERE
✅ Sort results with ORDER BY
✅ Aggregate data with functions like SUM, COUNT, AVG
✅ Group data using GROUP BY
✅ Filter groups with HAVING
✅ Limit output with LIMIT
✅ Join data from multiple tables
In short, SELECT is your entire window into the data — from raw exploration to advanced analytics.
⏳ Using LIMIT to Restrict Rows
LIMIT is used to restrict the number of rows returned by a query. It's especially useful during testing or exploring a large dataset.
✅ Example:
SELECT *
FROM customers
LIMIT 3;🔍 Result:
Returns only the first 3 rows from the customers table.
🔸
LIMITis supported in MySQL, PostgreSQL, SQLite.
🔸 In SQL Server, useTOPinstead:
SELECT TOP 3 * FROM customers;🚀 Up Next: JOINs — The Power of Combining Tables
Now that you’ve mastered SELECT, WHERE, GROUP BY, and more — it’s time to unlock the real magic of SQL.
In the next chapter, we’ll explore SQL JOINs, where you’ll learn how to combine data across multiple tables to answer more complex business questions.
Ever wondered how to fetch customer info along with their order purchases?
That’s where JOIN comes in. Stay tuned!
👉 See you in the next post!






