SQL Keywords

SQL WHERE

What is the SQL WHERE clause?

Filters rows by evaluating a Boolean condition and returning only those that satisfy it.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL WHERE: Supported by PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, Redshift, DB2, and all ANSI-compliant databases.

SQL WHERE Full Explanation

The WHERE clause applies a Boolean expression to each row produced by the preceding FROM, JOIN, or sub-query step. Only rows for which the expression evaluates to TRUE are returned to the next stage of query processing. WHERE executes before GROUP BY, HAVING, WINDOW functions, and ORDER BY, making it the primary filter for row-level data. It supports comparison operators (=, <, >, <=, >=, <>), logical operators (AND, OR, NOT), pattern matching (LIKE, ILIKE, SIMILAR TO), set membership (IN, NOT IN), null checks (IS NULL, IS NOT NULL), and sub-queries. Numeric, string, date, and even JSON or array data types can be compared as long as the database supports the operator. A NULL comparison without IS NULL returns UNKNOWN, which behaves like FALSE in WHERE, effectively excluding the row. WHERE is available in SELECT, UPDATE, DELETE, and MERGE statements; for aggregated filters, use HAVING instead. Performance depends on indexes on columns referenced in the predicate.

SQL WHERE Syntax

SELECT column_list
FROM table_name
WHERE condition;

UPDATE table_name
SET column = value
WHERE condition;

DELETE FROM table_name
WHERE condition;

SQL WHERE Parameters

  • condition (Boolean expression) - Evaluated per row; rows that return TRUE pass through.

Example Queries Using SQL WHERE

--1. Simple filter
SELECT id, email
FROM users
WHERE active = TRUE;

--2. Compound condition
SELECT *
FROM orders
WHERE status = 'shipped' AND shipped_at >= CURRENT_DATE - INTERVAL '7 days';

--3. Pattern match
SELECT username
FROM accounts
WHERE username LIKE 'test%';

--4. Using IN and sub-query
DELETE FROM sessions
WHERE user_id IN (SELECT id FROM users WHERE banned = TRUE);

Expected Output Using SQL WHERE

  • Only rows that meet the specified condition are returned (SELECT) or affected (UPDATE, DELETE)
  • Non-matching rows remain untouched or invisible in the result set

Use Cases with SQL WHERE

  • Retrieve active customers for a dashboard.
  • Delete obsolete log entries older than 90 days.
  • Update pricing only for products in a specific category.
  • Join tables then filter rows before aggregation for performance gains.

Common Mistakes with SQL WHERE

  • Comparing to NULL with = instead of IS NULL.
  • Placing aggregate functions in WHERE instead of HAVING.
  • Forgetting parentheses in complex AND/OR conditions, leading to incorrect logic.
  • Expecting WHERE to see aliases defined in SELECT; it cannot.

Related Topics

HAVING, GROUP BY, JOIN, ORDER BY, DISTINCT, AND, OR, NOT, LIKE, IN

First Introduced In

SQL-86 (ANSI X3.135-1986)

Frequently Asked Questions

What operators can I use inside WHERE?

Comparison (=, <, >, <=, >=, <>), logical (AND, OR, NOT), pattern matching (LIKE, ILIKE), set membership (IN, NOT IN), NULL checks (IS NULL), and custom operators supported by the database.

Does WHERE filter before or after GROUP BY?

WHERE filters before rows are grouped. If you need to filter on aggregate results, use HAVING instead.

How do NULL values behave in WHERE conditions?

Any comparison to NULL using standard operators returns UNKNOWN, which is treated like FALSE. Use IS NULL or IS NOT NULL to correctly include or exclude NULLs.

Is the WHERE clause case sensitive?

It depends on the database and collation settings. In PostgreSQL with default collation, comparisons are case sensitive; in MySQL with a case-insensitive collation they are not. Use functions like LOWER() for deterministic behavior.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!