Sitemap
srija karmakar

srija karmakar

Lifelong learner sharing what I discover about databases, coding, and tech. Writing to learn, learning to grow.

SQL SELECT Statement Deep Dive

3 min readAug 1, 2025

--

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

Customer Table

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 Chennai

You 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 22

We 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.2

Other common aggregate functions:

  • SUM(): Total sum
  • COUNT(): Total number of rows
  • MAX(): Highest value
  • MIN(): 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 1

Explanation:

  • COUNT(*) counts the number of rows in each city group.
  • GROUP BY clusters 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    2

Here, 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, or DELETE statements.
  • Cannot be used with aggregate functions like SUM(), COUNT(), etc.

➤HAVING Clause

  • Filters grouped data after GROUP BY is applied.
  • Must be used with SELECT that 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.

🔸 LIMIT is supported in MySQL, PostgreSQL, SQLite.
🔸 In SQL Server, use
TOP instead:

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!

--

--

srija karmakar

Written by srija karmakar

0 followers

Lifelong learner sharing what I discover about databases, coding, and tech. Writing to learn, learning to grow.