Start filtering SQL results with WHERE clauses—an essential part of querying effectively.
The SQL WHERE
clause is a fundamental component of SQL (Structured Query Language) that allows you to filter records based on specific conditions. Whether you're retrieving data, updating records, or deleting entries, the WHERE
clause enables you to target only the rows that meet your criteria.
In SQL, the WHERE
clause is used to specify conditions that filter which rows are returned by a query. It is commonly used in SELECT
, UPDATE
, DELETE
, and INSERT
statements to narrow down the results to only those that meet certain conditions.
The general syntax of the WHERE
clause is:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
In this syntax:
SELECT
specifies the columns to retrieve.FROM
specifies the table to query.WHERE
specifies the condition that must be met for a row to be included in the result set.Comparison operators are used in the WHERE
clause to compare values. Common comparison operators include:
=
: Equal to<>
or !=
: Not equal to>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal toTo select all customers from the Customers
table who are from the country "Mexico":
SELECT * FROM Customers
WHERE Country = 'Mexico';
To select all products from the Products
table with a price greater than 100:
SELECT * FROM Products
WHERE Price > 100;
Logical operators allow you to combine multiple conditions in a WHERE
clause. The most common logical operators are:
AND
: All conditions must be true.OR
: At least one condition must be true.NOT
: The condition must not be true.SELECT * FROM Orders
WHERE Country = 'Mexico' AND OrderAmount > 1000;
SELECT * FROM Employees
WHERE Department = 'Sales' OR Department = 'Marketing';
SELECT * FROM Products
WHERE NOT Discontinued = 1;
The BETWEEN
operator is used to filter the result set within a certain range. The values can be numbers, text, or dates.
SELECT * FROM Products
WHERE Price BETWEEN 50 AND 100;
SELECT * FROM Orders
WHERE OrderDate BETWEEN '2021-01-01' AND '2021-12-31';
The IN
operator allows you to specify multiple values in a WHERE
clause. It is a shorthand for multiple OR
conditions.
SELECT * FROM Customers
WHERE Country IN ('Mexico', 'USA', 'Canada');
The LIKE
operator is used in a WHERE
clause to search for a specified pattern in a column. It is often used with wildcard characters:
%
: Represents zero or more characters._
: Represents a single character.SELECT * FROM Customers
WHERE CustomerName LIKE 'A%';
SELECT * FROM Customers
WHERE CustomerName LIKE '%son%';
In SQL, NULL
represents a missing or undefined value. To test for NULL
values, use the IS NULL
or IS NOT NULL
operators.
SELECT * FROM Customers
WHERE Email IS NULL;
SELECT * FROM Customers
WHERE Email IS NOT NULL;
The WHERE
clause is essential in various real-world scenarios:
WHERE
clauses for better performance.NULL
values explicitly.The WHERE
clause in SQL is your filter—it lets you retrieve just the data you need. By combining comparison and logical operators, pattern matching, and special operators like IN
and BETWEEN
, you can write powerful queries that serve your specific needs.
Want to practice writing WHERE
clauses in a browser-based SQL editor? Try Galaxy—the modern SQL workspace built for speed, privacy, and collaboration.