SQL SELECT, WHERE and ORDER BY: Filtering & Sorting Data
Master conditional operators, logical expressions, pattern matching, and sorting results.
Overview & Context
A database table often contains millions of rows. Retrieving every row is neither practical nor performant. The true power of SQL lies in filtering out irrelevant data and ordering the results. By mastering the `WHERE` clause for row filtering and `ORDER BY` for sorting, you can extract exactly the records your application needs with minimal database workload.
1. The WHERE Clause & Comparison Operators
Equality, inequality, ranges, and null checks
The `WHERE` clause appears immediately after the `FROM` clause. It evaluates an expression for every candidate row; only rows for which the expression evaluates to `TRUE` are returned.
Common operators include `=` (equals), `<>` or `!=` (not equals), `>`, `<`, `>=`, `<=`, and `BETWEEN ... AND ...`.
-- Filter students with high academic standing
SELECT first_name, last_name, cgpa
FROM students
WHERE cgpa >= 8.50;
-- Range filtering with BETWEEN (inclusive bounds)
SELECT first_name, cgpa
FROM students
WHERE cgpa BETWEEN 7.00 AND 8.49;2. Combining Conditions: AND, OR, NOT, and IN
`AND` requires both conditions to be true. `OR` requires at least one condition to be true. When combining `AND` and `OR` in the same query, always use parentheses `()` to enforce precedence.
The `IN` operator checks if a column's value matches any value in a designated list.
-- Combining boolean conditions
SELECT first_name, email, department, cgpa
FROM students
WHERE (department = 'Computer Science' OR department = 'Data Science')
AND cgpa >= 8.00;
-- Checking list membership
SELECT course_name, credits
FROM courses
WHERE course_code IN ('CS-101', 'CS-201', 'DS-301');- Using `= NULL` or `!= NULL`. In SQL, `NULL` represents an unknown value. Comparing anything to `NULL` with `=` yields `UNKNOWN`, not `TRUE`. Always write `WHERE column IS NULL` or `WHERE column IS NOT NULL`.
3. Pattern Matching with LIKE & Wildcards
Use the `LIKE` operator to search for text patterns:
- `%` matches zero, one, or multiple characters.
- `_` matches exactly one character.
-- Find students with university email domains
SELECT first_name, email
FROM students
WHERE email LIKE '%@university.edu';
-- Find course codes like 'CS-101', 'CS-201', etc.
SELECT course_code, course_name
FROM courses
WHERE course_code LIKE 'CS-___';4. Sorting Data with ORDER BY
Ascending vs Descending order, multi-column sorting, and LIMIT
Database rows are stored in arbitrary order unless you explicitly specify an `ORDER BY` clause.
Use `ASC` for ascending order (default: A to Z, smallest to largest) and `DESC` for descending order (Z to A, largest to smallest).
-- Top 5 highest performing students
SELECT first_name, last_name, cgpa
FROM students
WHERE cgpa > 0.00
ORDER BY cgpa DESC, last_name ASC
LIMIT 5;5. Internal Query Execution Order
Although SQL queries start with `SELECT`, SQL engines execute clauses in this specific logical order:
1. `FROM` (identify tables)
2. `WHERE` (filter individual rows)
3. `SELECT` (select and project requested columns)
4. `ORDER BY` (sort the final projected result set)
5. `LIMIT` (restrict row count)
Because `WHERE` executes before `SELECT`, you cannot reference column aliases defined in `SELECT` within your `WHERE` clause.
Summary & Key Takeaways
- 1`WHERE` filters records based on boolean criteria before projection.
- 2Check for nulls strictly using `IS NULL` and `IS NOT NULL`.
- 3`LIKE` uses `%` (any characters) and `_` (single character) for string searches.
- 4`ORDER BY` sorts output ascending (`ASC`) or descending (`DESC`).
- 5Logical execution order: FROM → WHERE → SELECT → ORDER BY → LIMIT.
Does ORDER BY affect database index performance?
Yes. Sorting large unindexed datasets forces the database engine to perform expensive in-memory or disk-based sorting operations. Adding an index on frequently sorted columns enables instant B-tree order traversal.
Can I filter on aggregate functions in WHERE?
No. Because WHERE filters individual rows before aggregation occurs, you cannot write `WHERE COUNT(*) > 5`. Filtering aggregated groups requires the `HAVING` clause after `GROUP BY`.
Recommended Practical Tools
Next Recommended Reads
A gentle, comprehensive introduction to Structured Query Language (SQL). Master database concepts, primary keys, and essential querying commands.
Master SQL table joins with practical visual analogies. Learn how and when to use INNER JOIN, LEFT OUTER JOIN, and multi-table joins without performance degradation.
Learn essential data hygiene practices. Master how to handle missing data with Pandas, detect statistical outliers with IQR, and standardize categorical values.