SQL and DatabasesBeginner8 min read read

SQL SELECT, WHERE and ORDER BY: Filtering & Sorting Data

Master conditional operators, logical expressions, pattern matching, and sorting results.

Coding Hub Technical Editorial Team(Software Engineering & Education)
•
Updated 2026-03-10

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.

What You Will Learn
Filter rows with precision using the WHERE clause and comparison operators
Combine multiple conditions using logical AND, OR, and NOT operators
Perform string pattern searches with LIKE and wildcard symbols (% and _)
Sort query results chronologically and alphabetically using ORDER BY ASC / DESC
Understand the internal SQL query execution pipeline

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 ...`.

sql
-- 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;
Key note: `BETWEEN` is inclusive: it matches values equal to the lower bound, the upper bound, and all points between.

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.

sql
-- 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');
Key note: Without parentheses around the `OR` clause, `AND` takes mathematical precedence, which often produces unintended results.
Common Mistake to Avoid
  • 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.

sql
-- 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-___';
Key note: `'CS-___'` matches 'CS-' followed by exactly three characters.

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).

sql
-- 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;
Key note: Rows are sorted first by `cgpa` descending. If two students share the exact same CGPA, they are sorted alphabetically by `last_name`.

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.
Frequently Asked Questions

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`.