Ira SQL ProIra SQL Pro

SQL WHERE Clause

beginnerbasics
2 min read

What is the WHERE Clause?

The WHERE clause filters rows returned by a SELECT statement. Only rows that satisfy the specified condition appear in the result set.

Without WHERE, a query returns every row in the table. Adding WHERE lets you narrow the results to exactly the data you need.

Syntax

SELECT column1, column2
FROM table_name
WHERE condition;

Conditions use comparison operators such as =, !=, <, >, <=, and >=. You can combine multiple conditions with AND, OR, and NOT.

When to Use

  • Fetching a specific record by its ID
  • Filtering products above a certain price
  • Finding students in a particular grade
  • Limiting results to a date range

Key Points

  1. Evaluated Per Row — The database checks the WHERE condition against every row and only includes rows where the condition is true.
  2. NULL Handling — Comparisons with NULL using = return unknown, not true or false. Use IS NULL instead.
  3. Index Usage — Filtering on indexed columns makes queries faster because the database can skip irrelevant rows.
  4. Execution OrderWHERE is processed before SELECT, so you cannot reference column aliases defined in the SELECT list.
  5. String Comparisons — Wrap string literals in single quotes: WHERE first_name = 'Alice'.

The WHERE clause is essential for every real-world query. Combine it with other clauses like ORDER BY and LIMIT to build powerful, targeted queries.

Guided Practice

Solve the challenge below. Use hints when stuck and check your answer for instant feedback.

Practice challengeGuided learning mode

SQL WHERE Clause Challenge

Write a query that find all students who are 20 years old.

Expected result

Only rows where the age column equals 20.

Hidden checks

  • Returned rows and values
  • Output columns and result shape
  • Final database state after the query runs

Lesson guidance

What is the WHERE Clause?

Initializing database...Each run starts from fresh sample data.

More Examples

Filter with comparison operator

Find orders where the total is greater than 100.

Initializing database...Each run starts from fresh sample data.

Combine conditions with AND

Find students older than 18 in grade A.

Initializing database...Each run starts from fresh sample data.

Frequently Asked Questions

Can I use column aliases in the WHERE clause?
No. The WHERE clause is evaluated before the SELECT list, so aliases defined in SELECT are not available in WHERE. Repeat the expression or use a subquery.
How do I filter for NULL values in WHERE?
Use IS NULL or IS NOT NULL. The equality operator (=) does not work with NULL because any comparison to NULL yields unknown.

Related Topics