Lesson 2 of 12
Structured learning draftFilter records with explicit conditions
A payment report should include only paid sales of at least 100 units. Translate that request into two conditions, combine them deliberately, and test records that match one condition while failing the other.
Learning objectives
- Translate a request into row conditions.
- Distinguish AND from OR.
- Test a threshold and non-matching status.
Filter records with explicit conditions: the working sequence
Trace the three stages of filter records with explicit conditions and compare them with the example.
Each record is tested against the paid-status predicate.
AND also requires the amount to meet the minimum.
Only sale 1 satisfies both checks in this fixture.
WHERE filters individual input rows. AND requires both conditions to be true. Parentheses matter when mixing AND and OR; write them to show your intended grouping rather than relying on the reader to remember precedence.
WITH sales(id, customer, amount, status) AS (
VALUES (1, 'Amina', 120, 'paid'),
(2, 'Amina', 80, 'pending'),
(3, 'Brian', 50, 'paid'),
(4, 'Chen', NULL, 'paid')
)
SELECT id, customer, amount
FROM sales
WHERE status = 'paid' AND amount >= 100
ORDER BY id;id | customer | amount
1 | Amina | 120Try it, then change it
- Change the minimum to 50 and predict the additional row.
- Replace AND with OR and explain each included record.
- Add a paid sale of exactly 100 and verify that >= includes it.
Common mistakes
- Using OR when every condition must hold.
- Omitting parentheses from a mixed logical expression.
- Forgetting to test a value exactly at the threshold.
Practice activity
Apply the lesson
Select pending sales below 100, then paid sales below 100. Explain which record changes when the status condition changes. Keep the changed example, its output and a short explanation of whether the result matched your prediction.
How to check your work
- Write down the expected result before running or submitting your work.
- Try a normal example, an empty or missing input, and a boundary case where the rule changes.
- Compare the actual result with your prediction. Record the smallest example that explains a difference.
- Explain one change you made and why it improved the result. Save your evidence in the lesson notes.
When you get stuck · databases
A join returns more rows than expected.
- Find the cause
- Count matching records for each join key in both tables. A key repeated on both sides creates multiple combinations.
- Try this
- Decide the intended row grain first: one row per customer, order or item. Aggregate at that grain or correct the join condition; do not hide the problem with DISTINCT.
- Check the fix
- Use two customers and three orders as a fixture. Predict every result row and check unmatched records separately.
Use this course-wide example when it fits your task. For a different failure, record the input, expected result, actual result and smallest steps that reproduce it.
Watch, pause, practise
Free video lessons
Choose the chapter that matches your task. Videos open on the original provider’s site; playback uses additional data.
CS50: databases with SQL
Watch Querying or Relating to see selection and joins explained using actual tables. Examples begin with SQLite; check syntax when using PostgreSQL.
Try it yourself: Draw two small tables, predict the joined rows, then compare your prediction with the query result.
Source checked 2026-10-07 · Free learning material; optional certificates may cost extra.Check your understanding
Which condition requires a sale to be both paid and at least 100?
Lesson summary
- WHERE decides which input rows participate.
- AND and OR express different selection rules.
- Boundary records reveal threshold mistakes.
Sources and further reading
- Value expressions and logical conditionsPostgreSQL Global Development Group - accessed 2026-10-07
- PostgreSQL SQL language guidePostgreSQL Global Development Group - accessed 2026-10-07
Personal study note
Your notes
Keep the input, expected result, actual result and what you changed. Save before leaving this lesson. Download a copy to keep it outside this browser.