Table of Contents
- Introduction
- What is the
SELECT
Statement? - Basic Syntax of
SELECT
- Selecting All Columns with
*
- Selecting Specific Columns
- Using Aliases with
AS
- Selecting Distinct Records
- Filtering Results with
WHERE
- Logical Operators: AND, OR, NOT
- Comparison Operators
- Working with
NULL
- Sorting Results with
ORDER BY
- Limiting Results with
LIMIT
- Selecting Based on Ranges:
BETWEEN
- Using
IN
to Match Multiple Values - Pattern Matching with
LIKE
- Combining Conditions
- Real-World Query Examples
- Best Practices for
SELECT
Queries - Summary and What’s Next
1. Introduction
The SELECT
statement is the foundation of SQL. It’s the command used to retrieve data from a database. Whether you’re pulling records from a table, filtering data, or aggregating it for analysis, SELECT
is your go-to tool.
In this module, we’ll explore everything you need to know to write your first SELECT queries confidently.
2. What is the SELECT
Statement?
The SELECT
statement retrieves rows from one or more tables or views. It allows you to specify which columns you want, apply conditions, sort, limit, and more.
3. Basic Syntax of SELECT
SELECT column1, column2
FROM table_name;
Example:
SELECT name, email FROM users;
4. Selecting All Columns with *
The asterisk (*
) selects all columns:
SELECT * FROM users;
Useful for quick debugging or exploration, but avoid it in production queries for performance reasons.
5. Selecting Specific Columns
You can specify only the columns you need:
SELECT name, age FROM employees;
This improves performance and makes results easier to understand.
6. Using Aliases with AS
Aliases give columns or tables temporary names:
SELECT name AS full_name, age AS years_old FROM users;
You can also alias tables:
SELECT u.name FROM users AS u;
7. Selecting Distinct Records
To eliminate duplicates:
SELECT DISTINCT city FROM customers;
DISTINCT
ensures only unique values are returned.
8. Filtering Results with WHERE
The WHERE
clause is used to filter records based on conditions.
SELECT * FROM orders WHERE total > 500;
You can use:
- Comparison operators:
=
,!=
,<
,>
,<=
,>=
- Logical operators:
AND
,OR
,NOT
- Special conditions:
IN
,BETWEEN
,LIKE
,IS NULL
9. Logical Operators: AND, OR, NOT
Combine multiple conditions:
sqlCopyEditSELECT * FROM employees
WHERE department = 'HR' AND age > 30;
sqlCopyEditSELECT * FROM employees
WHERE department = 'HR' OR department = 'Finance';
sqlCopyEditSELECT * FROM employees
WHERE NOT age < 18;
10. Comparison Operators
Operator | Meaning |
---|---|
= | Equal to |
!= or <> | Not equal to |
< | Less than |
> | Greater than |
<= | Less than or equal |
>= | Greater or equal |
Example:
sqlCopyEditSELECT * FROM products WHERE price <= 1000;
11. Working with NULL
NULL
represents missing or unknown data. You cannot use =
or !=
to compare NULL
.
Use:
sqlCopyEditSELECT * FROM users WHERE email IS NULL;
SELECT * FROM users WHERE email IS NOT NULL;
12. Sorting Results with ORDER BY
sqlCopyEditSELECT name, age FROM users ORDER BY age;
SELECT name, age FROM users ORDER BY age DESC;
You can sort by one or more columns.
13. Limiting Results with LIMIT
Restrict number of rows:
sqlCopyEditSELECT * FROM users LIMIT 5;
MySQL and PostgreSQL support:
sqlCopyEditSELECT * FROM users LIMIT 5 OFFSET 10;
SQLite also supports this syntax.
14. Selecting Based on Ranges: BETWEEN
Check if a value falls within a range:
sqlCopyEditSELECT * FROM products WHERE price BETWEEN 100 AND 500;
Inclusive: includes both 100 and 500.
15. Using IN
to Match Multiple Values
sqlCopyEditSELECT * FROM customers
WHERE country IN ('India', 'Nepal', 'Bangladesh');
IN
is cleaner and more efficient than multiple OR
conditions.
16. Pattern Matching with LIKE
Use LIKE
for partial matches with %
and _
.
sqlCopyEditSELECT * FROM users WHERE name LIKE 'A%'; -- Starts with A
SELECT * FROM users WHERE email LIKE '%@gmail.com'; -- Ends with
SELECT * FROM users WHERE name LIKE '_ohn'; -- Matches 4-letter name ending in "ohn"
17. Combining Conditions
sqlCopyEditSELECT * FROM employees
WHERE department = 'Sales'
AND (age > 30 OR city = 'Delhi');
Use parentheses to group complex logic for clarity and accuracy.
18. Real-World Query Examples
Find all active users above 25 years of age:
sqlCopyEditSELECT name, email FROM users
WHERE age > 25 AND is_active = TRUE;
Get top 3 highest-paying jobs:
sqlCopyEditSELECT title, salary FROM jobs
ORDER BY salary DESC
LIMIT 3;
Search for customers not in India or Nepal:
sqlCopyEditSELECT * FROM customers
WHERE country NOT IN ('India', 'Nepal');
19. Best Practices for SELECT
Queries
- Avoid using
SELECT *
in production - Use meaningful aliases for readability
- Filter early with
WHERE
to improve performance - Sort data after filtering, not before
- Use indexes wisely for large datasets
20. Summary and What’s Next
The SELECT
statement is your entry point to reading and analyzing data. Mastering it will help you query any relational database confidently. We’ve covered syntax, filtering, sorting, and limiting — all crucial to practical SQL use.