Conceito introdutório

~7 min de leitura

WHERE — Filtering Data

Learn how to filter database rows using conditions to get exactly the data you need.

Iniciante

What is WHERE?

The WHERE clause lets you filter rows based on specific conditions. Instead of retrieving all rows from a table, you only get the ones that match your criteria.

Think of it like using a search filter in an online store: "Show me only products under $50 in the Electronics category." WHERE does the same thing for database queries.

Basic Syntax

SQL Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition;

💡 Pro Tip: WHERE comes after FROM and before ORDER BY or GROUP BY. The condition must evaluate to true or false for each row.

Common Patterns

Filter rows using comparison operators

SQL Query
SELECT name, age 
FROM customers 
WHERE age >= 18;

Result: Returns only customers who are 18 or older

See It In Action

Watch how WHERE filters data step by step. Select an example to see the complete flow from input data to filtered results.

Filter by Age

Get customers who are 30 or older

1

Input Data

customers
idINTEGER
nameTEXT
emailTEXT
countryTEXT
ageINTEGER
1Alice Johnson[email protected]USA28
2Bob Smith[email protected]Canada35
3Carlos Silva[email protected]Brazil22
4Diana Chen[email protected]USA41
5Emma Davis[email protected]Canada19
5 rows displayed
2

SQL Query

SQL
SELECT name, age, country
FROM customers
WHERE age >= 30
3

Result

2 rows
result
nameTEXT
ageNUMBER
countryTEXT
Bob Smith35Canada
Diana Chen41USA
2 rows displayed

Comparison Operators

=Equal To

age = 25

Exact match. Use single quotes for text: country = 'USA'

!=Not Equal To

status != 'inactive'

Excludes rows with the specified value. Can also use <>

> <Greater/Less Than

price > 100

Compare numeric values. Also includes >= and <=

BETWEENRange

age BETWEEN 18 AND 65

Inclusive range check. Easier to read than age >= 18 AND age <= 65

INList Match

country IN ('USA', 'Canada')

Matches any value in the list. Cleaner than multiple OR conditions.

LIKEPattern Match

name LIKE 'John%'

Use % for any characters, _ for single character.

Combining Conditions

AND

AND - Both Must Be True

WHERE age >= 18 AND country = 'USA'

Narrows results. Only rows where ALL conditions are true are returned.

OR

OR - At Least One Must Be True

WHERE country = 'USA' OR country = 'Canada'

Broadens results. Rows matching ANY condition are returned.

NOT

NOT - Negates a Condition

WHERE NOT country = 'USA'

Reverses the condition. Can also use != or NOT IN

( )

Parentheses - Control Order

WHERE (country = 'USA' OR country = 'Canada') AND age >= 18

Group conditions to control evaluation order. Critical for complex filters!

Common Mistakes to Avoid

❌ Forgetting quotes for text values

WHERE country = USA

Text values must be in single quotes: country = 'USA'

❌ Using AND when you mean OR

WHERE country = 'USA' AND country = 'Canada'

A row can't be both! Use OR: country = 'USA' OR country = 'Canada'

❌ Incorrect LIKE wildcards

WHERE email LIKE 'gmail.com'

Use % wildcard: email LIKE '%gmail.com'

❌ Missing parentheses in complex conditions

WHERE age > 18 AND country = 'USA' OR country = 'Canada'

Ambiguous! Use parentheses: (country = 'USA' OR country = 'Canada') AND age > 18

✅ Correct syntax with proper grouping

WHERE (country IN ('USA', 'Canada')) AND age BETWEEN 18 AND 65

Clear, readable, and correct!

Pratique a seguirComeçar os desafios →