Filtering and Sorting
How to select exactly the rows you need
After a basic SELECT, the next step is learning how to control which rows appear in the result and in what order. SQL provides filtering conditions, logical operators, special predicates, and sorting for this purpose.
In this section, we will not modify table data. All constructions work with the query result: some remove rows that do not match, others define the order, and others limit how many records are returned.
What this section contains
- WHERE. We will learn how to keep only rows that satisfy a condition.
- Comparison operators. We will cover equality, inequality, greater-than, and less-than checks.
- AND, OR, and NOT. We will learn how to combine several conditions and control their logic explicitly.
- IN, BETWEEN, and LIKE. We will look at convenient predicates for sets of values, ranges, and text patterns.
- IS NULL and IS NOT NULL. We will reinforce the correct way to test for missing values.
- ORDER BY. We will learn how to define the order of rows in the result.
- LIMIT and OFFSET. We will learn how to return only part of a result.
These constructions are constantly used together: first the query determines which rows match, then orders them, and, when necessary, keeps only the required part of the result.