Mastering SQL SELECT Clauses: A Practical Guide for Beginners
When working with relational databases, the SELECT statement is the most frequently used SQL command. It allows you to retrieve data from tables and analyze it according to your needs.
But in real-world scenarios, simply selecting data is not enough. You often need to:
● Filter specific records
● Group related data
● Sort results
● Remove duplicates
● Limit the output
To perform these tasks, SQL provides several powerful clauses.
In this article, we’ll explore the most important SELECT clauses with clear explanations and practical examples.
1. WHERE Clause – Filtering Specific Rows
The WHERE clause is used to retrieve only the rows that match a given condition. It acts like a filter for your data.
|
1 2 3 4 5 6 7 8 9 |
Syntax SELECT column1, column2 FROM table_name WHERE condition; Example SELECT * FROM employees WHERE salary > 50000; |
Result: This query returns only the employees whose salary is greater than 50,000.
Common use cases:
● Finding employees in a specific department
● Filtering orders by date
● Selecting customers from a particular city
2. GROUP BY Clause – Grouping Similar Data
The GROUP BY clause is used to group rows that have the same values in specified columns.
It is typically used with aggregate functions such as:
● COUNT()
● SUM()
● AVG()
● MAX()
● MIN()
|
1 2 3 4 5 6 7 8 |
Syntax SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1; Example SELECT department, COUNT(*) FROM employees GROUP BY department; |
Result: This query shows the number of employees in each department.
Real-world example: A company can use this to calculate how many employees work in each department.
3. HAVING Clause – Filtering Grouped Results
The HAVING clause is used to filter the results after grouping.
It works together with the GROUP BY clause.
|
1 2 3 4 5 6 7 8 9 10 11 |
Syntax SELECT column1, aggregate_function(column2) FROM table_name GROUP BY column1 HAVING condition; Example SELECT department, COUNT(employee_id) FROM employees GROUP BY department HAVING COUNT(employee_id) > 5; |
Result:This query returns departments that have more than five employees.
Important difference:
Clause |
Filters |
WHERE |
Individual rows |
HAVING |
Groups of rows |
4. ORDER BY Clause – Sorting Results
The ORDER BY clause is used to sort the result set.
Sorting options:
● ASC → Ascending order (default)
● DESC → Descending order
|
1 2 3 4 5 6 7 8 9 |
Syntax SELECT column_list FROM table_name ORDER BY column_name ASC; Example SELECT name, salary FROM employees ORDER BY salary DESC; |
Result: Employees are displayed from highest salary to lowest.
Common use cases:
● Ranking students by marks
● Showing latest orders first
● Sorting products by price
5. LIMIT Clause – Restricting the Output
The LIMIT clause is used to control the number of rows returned.
|
1 2 3 4 5 6 7 8 9 10 11 |
Syntax SELECT column_list FROM table_name ORDER BY column_name LIMIT number; Example SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 5; |
Result: Returns the top five highest-paid employees.
|
1 2 3 4 5 |
Example with OFFSET SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 5 OFFSET 5; |
Result: Skips the first five rows and returns the next five.
Use case: Pagination in websites and applications.
6. FETCH Clause – Standard SQL Row Limiting
The FETCH clause performs the same function as LIMIT but follows the SQL standard.
|
1 2 3 4 5 6 7 8 9 10 11 |
Syntax SELECT column_list FROM table_name ORDER BY column_name FETCH FIRST row_count ROWS ONLY; Example SELECT name, salary FROM employees ORDER BY salary DESC FETCH FIRST 5 ROWS ONLY; |
Result: Returns the first five rows after sorting.
|
1 2 3 4 5 6 |
Example with OFFSET SELECT name, salary FROM employees ORDER BY salary DESC OFFSET 5 ROWS FETCH NEXT 5 ROWS ONLY; |
Result: Skips five rows and returns the next five.
7. DISTINCT Clause – Removing Duplicate Records
The DISTINCT clause is used to return only unique values.
|
1 2 3 4 5 6 7 |
Syntax SELECT DISTINCT column1 FROM table_name; Example SELECT DISTINCT department FROM employees; |
Result: Returns only unique department names.
DISTINCT with Multiple Columns: SELECT DISTINCT department, job_title FROM employees;
Result: Returns unique combinations of department and job title.
DISTINCT ON (PostgreSQL): In PostgreSQL, DISTINCT ON is used to keep the first row from each group of duplicates.
The selected row depends on the ORDER BY clause.
|
1 2 3 4 5 6 7 8 |
Syntax SELECT DISTINCT ON (column1) column1, column2 FROM table_name ORDER BY column1, column2; Example SELECT DISTINCT ON (department) department, name, salary FROM employees ORDER BY department, salary DESC; |
Result: Returns one employee per department—the one with the highest salary.
Summary of All Clauses
Clause |
Purpose |
WHERE |
Filters rows based on conditions |
GROUP BY |
Groups rows with similar values |
HAVING |
Filters grouped results |
ORDER BY |
Sorts the output |
LIMIT |
Restricts number of rows |
FETCH |
Standard way to limit rows |
DISTINCT |
Removes duplicate values |
DISTINCT ON |
Keeps first row per group (PostgreSQL) |
Final Thoughts
Understanding these SQL clauses is essential for:
● Writing efficient queries
● Analyzing large datasets
● Preparing for technical interviews
● Building real-world data applications
Once you master these clauses, you’ll be able to transform raw data into meaningful insights with ease.
Author : Geethika Bandaru
LinkedIn : http://linkedin.com/in/bandaru-geethika
Thank you for giving your valuable time to read the above information. Please click here to subscribe for further updates.
KTExperts is always active on social media platforms.
Facebook : https://www.facebook.com/ktexperts
LinkedIn : https://www.linkedin.com/company/ktexperts/
Twitter : https://twitter.com/ktexpertsadmin
YouTube : https://www.youtube.com/c/ktexperts
Instagram : https://www.instagram.com/knowledgesharingplatform
Note: Please test scripts in Non Prod before trying in Production.




