SQL WHERE Clause

SQL · Filtering Rows

Theory

WHERE filters rows. The engine checks the condition for each row coming from FROM and keeps only rows where it is TRUE. Conditions use comparison operators (=, <>, <, >, <=, >=), logical operators (AND, OR, NOT) and special tests such as BETWEEN, IN, LIKE and IS NULL. For example, WHERE dept = 'IT' keeps only IT employees.

SQL uses three-valued logic: a comparison can be TRUE, FALSE or UNKNOWN, and anything compared with NULL is UNKNOWN. WHERE keeps only TRUE rows, so rows with NULL in the tested column silently drop out. WHERE runs before GROUP BY and SELECT, so it cannot use aggregates like SUM() (that is HAVING's job) or column aliases defined in SELECT. In UPDATE and DELETE, WHERE decides which rows are changed.

Why we use it

Tables hold far more rows than any single question needs. WHERE narrows the data to just the relevant rows early, which makes results correct and lets the engine use indexes to read less.

When to use it

Use WHERE whenever you filter on individual row values. To filter on aggregates such as COUNT(*) > 5, use HAVING after GROUP BY. Always double-check WHERE in UPDATE and DELETE, since it controls what gets changed.

How it works

  1. Query starts: FROM employees
  2. Read the next row
  3. Evaluate the condition: TRUE, FALSE or UNKNOWN
  4. Is the result TRUE? Yes: Keep the row for the next step. No: Discard the row (FALSE or UNKNOWN).
  5. Repeat until all rows are read
  6. Pass kept rows to SELECT

Syntax

SELECT columns FROM table
WHERE condition;

Example

SELECT * FROM employees WHERE department = 'IT';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic