SQL Basics
- What is SQL?
- Data Definition Language
- Data Manipulation Language
- Data Query Language
- Data Control Language
- Transaction Control Language
- Tables and Schemas
- Data Types
- Constraints
Querying and Filtering
DQL in SQL: the SELECT
Statement to Query Data Like a Pro
When working with databases, you need a way to extract information from them. That’s where DQL (Data Query Language) comes in. Unlike DDL or DML, which are used to create or change data, DQL is used purely for fetching data — and it’s one of the most common tasks in SQL.
At the heart of DQL is a single, powerful command: SELECT
.
In this article, we’ll take a deep dive into the SELECT
statement — the workhorse of SQL — and walk through its features, variations, and best practices. Whether you’re a complete beginner or just brushing up, this guide will help you become confident with querying data.
What is DQL (Data Query Language)?
Data Query Language (DQL) is a subset of SQL used to query information from a database. It does not alter the structure or the data — it only retrieves data based on specific criteria.
Key DQL Command:
SELECT
– fetches data from one or more tables
While SELECT
is technically a DML command in some databases, it’s often treated separately under DQL due to its read-only nature.
The SELECT Statement Explained
The basic purpose of SELECT
is to retrieve data from a table. Here’s the simplest form:
SELECT column1, column2 FROM table_name;
Or to fetch everything:
SELECT * FROM table_name;
Let’s explore SELECT
in depth, step by step.
1. Selecting All Data
Using the asterisk (*
) allows you to select all columns:
SELECT * FROM Employees;
This fetches every row and column from the Employees
table. While useful during exploration, it’s best to specify only the columns you need to keep queries fast and efficient.
2. Selecting Specific Columns
You can narrow down your results by specifying column names:
SELECT name, email FROM Employees;
This only retrieves the name
and email
fields.
3. Using WHERE to Filter Data
The WHERE
clause lets you apply conditions:
SELECT * FROM EmployeesWHERE department = 'Sales';
You can combine multiple conditions:
SELECT * FROM EmployeesWHERE department = 'Sales' AND age > 30;
Common operators used in WHERE
:
=
Equal to!=
or<>
Not equal to<
,>
,<=
,>=
AND
,OR
,NOT
LIKE
(for pattern matching)IN
(for multiple values)BETWEEN
(for range)
4. Using Aliases with AS
Aliases rename columns or tables in your results:
SELECT name AS FullName, salary AS Income FROM Employees;
Or for tables:
SELECT e.name, e.salaryFROM Employees AS e;
This makes queries easier to read, especially when joining tables.
5. Sorting Results with ORDER BY
You can sort query results using ORDER BY
:
SELECT * FROM EmployeesORDER BY salary DESC;
Use ASC
for ascending (default) or DESC
for descending.
6. Limiting Results with LIMIT or TOP
When your table has thousands of rows, you might want to limit the results.
In MySQL or PostgreSQL:
SELECT * FROM EmployeesLIMIT 5;
In SQL Server:
SELECT TOP 5 * FROM Employees;
7. Aggregate Functions and GROUP BY
SQL provides powerful aggregate functions:
COUNT()
– total number of rowsSUM()
– total valueAVG()
– averageMAX()
– highest valueMIN()
– lowest value
Example:
SELECT department, COUNT(*) AS total_employeesFROM EmployeesGROUP BY department;
This shows how many employees are in each department.
8. Filtering Groups with HAVING
To filter the results of a GROUP BY
, use HAVING
:
SELECT department, COUNT(*) AS totalFROM EmployeesGROUP BY departmentHAVING total > 5;
This only shows departments with more than 5 employees.
9. Joining Multiple Tables
The SELECT
statement can also pull data from multiple tables using JOINs.
Example:
SELECT Employees.name, Departments.name AS dept_nameFROM EmployeesJOIN Departments ON Employees.department_id = Departments.id;
This connects data from two related tables.
10. Subqueries in SELECT
A subquery is a query inside another query:
SELECT name, salaryFROM EmployeesWHERE salary > (SELECT AVG(salary) FROM Employees);
This fetches employees who earn more than the average salary.
Best Practices for SELECT Queries
✅ Always specify columns instead of using *
for better performance
✅ Use WHERE wisely to avoid returning too much data
✅ Index frequently queried columns to speed up performance
✅ Try to avoid nested subqueries if a JOIN
will do
✅ Use table aliases for cleaner queries
✅ Format and indent your SQL code for readability
Common Mistakes to Avoid
❌ Forgetting the WHERE
clause and accidentally fetching huge result sets
❌ Misusing GROUP BY
without understanding aggregation
❌ Using *
in production queries — it’s lazy and slow
❌ Overusing subqueries when a simple JOIN would work
❌ Ignoring NULLs in conditions — remember NULL != NULL
Use Cases of SELECT in Real Life
- 📊 Business reports: Monthly sales by region
- 🛍️ E-commerce: Find all orders over $500
- 📅 HR: List employees hired after a certain date
- 📚 Education: Retrieve student grades above a threshold
- 🧾 Accounting: Total invoices per client
No matter the industry, the SELECT
command powers most of the dashboards, reports, and decision-making tools we rely on every day.
The SELECT
statement is the foundation of working with data in SQL. It allows you to retrieve the exact data you need, whether from a simple table or across complex joins. With clauses like WHERE
, ORDER BY
, GROUP BY
, and JOIN
, SELECT
becomes a powerful tool for analyzing and understanding your data.
By mastering SELECT
, you’re taking the first big step toward becoming confident in SQL and ready to explore deeper concepts like analytics, performance tuning, and data modeling.