SQL: Selecting, Filtering and Ordering Rows
Asking a database for the rows and columns you want with SELECT, WHERE and ORDER BY. The query says what is wanted, and the database decides how to fetch it.
What a learner can do afterwards
- Write a query that returns named columns for the rows matching a condition
- Combine conditions with AND, OR and NOT and predict which rows survive
- Sort results and limit them, and say what changes when the order is reversed
1 · Read
You have split facts into tables. SQL lets you request data from them. These examples use SQLite. The schema lists table and column names. SELECT names columns to return; FROM names the table. WHERE tests each row and returns it only when the condition is true. Requesting fewer columns does not delete stored data.
Books has title, author and borrowed_on columns. borrowed_on stores dates as YYYY-MM-DD text, with no NULLs. SELECT title, author FROM Books WHERE borrowed_on > '2025-05-01' ORDER BY title ASC LIMIT 3; returns up to three matching rows. The date itself is excluded by >. Write these clauses in this order: SELECT, FROM, WHERE, ORDER BY, LIMIT.
AND needs both conditions to be true. OR needs at least one to be true, including when both are true. NOT reverses true and false. Parentheses show which tests belong together. SQL uses = to compare values. Quote text values, as in borrowed = 'yes'. If returned stores only 0 or 1, NOT (returned = 1) keeps rows with 0.
ORDER BY sorts matching rows. ASC, the default, is ascending; DESC is descending. Text order follows the database's comparison rules. Add a unique book_id to break title ties. LIMIT n returns at most n rows. Without ORDER BY, you cannot guarantee which rows a limit will return. To predict results, find matching rows, sort, then limit. This describes the result, not the engine's physical execution plan.
A query that names a table needs that table and its columns to exist. For an unknown-column error, compare the name with the schema. An empty table can return zero rows. For an unexpected empty result, check the selected database, stored values and filter before changing it. NULL means a missing value. Comparisons with NULL usually give unknown. WHERE keeps only true results; NOT unknown remains unknown.
Name your columns, filter your rows, then sort and trim the answer.
SQL lesson, questions and study cards written for this dataset. The query examples use SQLite.
Readings and study cards written for this dataset. Linked videos are credited to Khan Academy: "Welcome to SQL" and "S-Q-L or SEQUEL?". Video reuse terms have not been verified.
More to read
Choose columns and test rows
Requesting title and author changes which columns appear in a query result. It does not remove the other facts stored about each book.
Consider SELECT title, author FROM Books WHERE pages > 180; in SQLite. FROM identifies Books. The condition compares each row's page count with 180. A row with 220 pages passes; a row with 180 pages does not. The result contains title and author for passing rows.
Check columns and rows separately when reading a query. The SELECT list answers which fields you will see. WHERE answers which records qualify. This distinction helps when a result contains the right books but the wrong fields, or the right fields but too many books.
Work through combined conditions
Test each comparison before combining its result with another. If pages > 180 is true and borrowed = 'yes' is false, their AND result is false. Their OR result is true. OR also passes when both comparisons are true.
Parentheses make a larger condition easier to read. In WHERE (pages > 180 OR author = 'A. Reed') AND borrowed = 'yes', evaluate the parenthesised part as one condition. The row must also have borrowed equal to 'yes'. This is a way to reason about the result, not a promise about which comparison the engine executes first.
NOT reverses true and false. If returned is NULL, returned = 1 is unknown. NOT does not turn that unknown result into true. WHERE returns rows only when the whole condition is true.
Predict a sorted, limited result
A limit chooses from the result's order, so specify the order when you need a predictable selection. In SQLite, ORDER BY pages DESC LIMIT 2 returns up to two rows from the highest page counts.
Suppose three books have 410, 260 and 190 pages. Descending order puts 410 first, then 260, then 190. The limit keeps the first two. Ascending order with the same limit instead keeps 190 and 260.
Equal page counts need a further ordering column if the choice between tied books matters. A unique book_id can break that tie. Text sorting follows the database's text comparison rules. Mixed case and accented titles need more care than these distinct numbers.
LIMIT never supplies missing rows. If only one row qualifies, LIMIT 2 returns one.
Check names before changing a query
An unknown-column error and an empty result are different problems. An unknown-column error means a named column cannot be resolved in that query. Compare its spelling and table with the schema. Changing the filter does not supply a missing column.
An empty result may be valid. The table might contain no rows, or none of its rows might satisfy the condition. Before changing the condition, check that you selected the intended database and that stored values have the format you expected.
A query that names Books needs that table to exist. It does not need Books to contain rows. Some SELECT statements do not read a table at all. In SQLite, SELECT 1; returns the value 1.
SQL describes the requested result. The engine chooses how to obtain it, so written clause order is not a physical execution plan.
Explore more
2 · Watch
به انگلیسیTake it off screen
Where it sits
8 questions wait behind this lesson, each with its answer explained. Every answer feeds the sky: stars light as they are learned, and dim when it is time to come back.