Sub topic 11
Filtering and Sorting Results
Assume the books table already exists and contains the sample rows from the previous chapter. You should be comfortable running SQL statements in psql, reading a table definition, and inserting data.
A query can return every row, or it can narrow the result to rows that meet a condition. The WHERE clause performs this filtering. The ORDER BY clause controls the order of the rows in the result.
Filtering with WHERE
This query returns only books published after 1900:
SELECT title, author, published_year
FROM books
WHERE published_year > 1900;The comparison operator > means “greater than.” Common comparison operators include:
| Operator | Meaning |
|---|---|
| `=` | equal to |
| `<>` | not equal to |
| `>` | greater than |
| `>=` | greater than or equal to |
| `<` | less than |
| `<=` | less than or equal to |
Text values use single quotation marks:
SELECT title, author
FROM books
WHERE author = 'Jane Austen';SQL comparisons are usually not affected by capitalization in keywords, but text values can be case-sensitive. For example, 'jane austen' may not match 'Jane Austen'.
You can combine conditions with AND and OR:
SELECT title, published_year
FROM books
WHERE published_year >= 1900
AND published_year < 2000;AND requires both conditions to be true. OR requires at least one condition to be true. Use parentheses when mixing them so that your intention is clear:
SELECT title, author
FROM books
WHERE (author = 'Jane Austen' OR author = 'Frank Herbert')
AND published_year < 2000;A missing value is represented by NULL. NULL means “unknown” or “not provided,” not zero or an empty string. Use IS NULL or IS NOT NULL to test it:
SELECT title
FROM books
WHERE published_year IS NULL;Sorting with ORDER BY
Without ORDER BY, PostgreSQL does not promise a particular row order. Sort by one column like this:
SELECT title, published_year
FROM books
ORDER BY published_year;Ascending order is the default. Use DESC for descending order:
SELECT title, published_year
FROM books
WHERE published_year > 1900
ORDER BY published_year DESC;You can sort by multiple columns. PostgreSQL uses the next column when two rows have the same value in the first column:
SELECT author, title, published_year
FROM books
ORDER BY author ASC, published_year DESC;For predictable placement of missing values, specify NULLS FIRST or NULLS LAST:
SELECT title, published_year
FROM books
ORDER BY published_year DESC NULLS LAST;The normal processing order is shown below.
Common errors
A filter on the wrong column can return valid-looking but incorrect results. Check the column name and data type before writing the condition. Also remember that UPDATE and DELETE use the same WHERE rules. Test a condition with SELECT before using it to change data.
Exercise
Write a query that returns the title and author of books published from 1900 through 1999, with the newest books first.
Show answer
SELECT title, author FROM books WHERE publishedyear >= 1900 AND publishedyear < 2000 ORDER BY published_year DESC;