Menu
CoddyTech

Combine Conditions

Part of the First Queries section of Coddy's PostgreSQL journey. Lesson 7 of 20.

AND requires both conditions to be true. OR accepts either condition. Parentheses group alternatives so an extra condition applies to the whole group.

Input table products:

idnamecategorypricestockactivenote
1Maptravel125TrueNULL
2Notebookbooks80TrueRecycled
3Atlasbooks243FalseNULL
4Mughome128TrueBlue
5lamphome302FalseClearance
6Mini maptravel610TrueNULL
SELECT id FROM products WHERE (category = 'home' OR category = 'books') AND price >= 20 ORDER BY id;

The category group accepts home or books, then the price condition keeps ids 3 and 5. Without parentheses, AND binds more tightly than OR, which can change the selected rows.

Group alternatives with parentheses before combining them with AND.

challenge icon

Challenge

Easy

Return id and name for travel or books products that have stock greater than zero. Order by id.

The input tables are shown in the test panel. Each run starts from fresh data. Return the named columns in the stated order; do not hard-code the displayed rows.

Try it yourself

-- Write your SQL here.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in First Queries

Practice on your own: PostgreSQL playground