Filtering rows with WHERE

Return only the rows that match a condition.

Beginner7 min readLesson 2 of 38
In this lesson 2 sections
  1. Combining conditions
  2. Common operators
Jump to practice

SELECT picks columns. WHERE picks rows.

SELECT opportunity_id, name, amount
FROM opportunities
WHERE stage = 'Negotiation';

Text values go in single quotes. Numbers don’t.

Combining conditions

Use AND when both must be true, OR when either can be:

SELECT opportunity_id, name, amount
FROM opportunities
WHERE stage = 'Negotiation'
  AND amount >= 50000;

When you mix AND and OR, add parentheses. Without them, AND runs first, which is rarely what you meant:

-- Read as: Proposal deals of any size, OR big Negotiation deals
WHERE stage = 'Proposal' OR stage = 'Negotiation' AND amount > 50000
 
-- What you probably meant
WHERE (stage = 'Proposal' OR stage = 'Negotiation') AND amount > 50000

Common operators

Operator Meaning Example
= / <> equal / not equal stage <> 'Closed Lost'
> < >= <= comparisons amount >= 50000
IN matches any in a list lead_source IN ('Inbound', 'Partner')
LIKE pattern match name LIKE 'North%'
IS NULL value is missing closed_at IS NULL

The next few lessons cover IN, LIKE and IS NULL in detail.

Try it

Return the name and amount of every opportunity in the Proposal stage worth more than $25,000.

Write it in the sandbox below, then press Check answer.

SQL sandbox

Run queries against the sample CRM tables (accounts, opportunities,users, contacts and opportunity_history), right in your browser. Nothing you run leaves your computer.

Back to the top of the lesson