The WHEREclause in SQL is used to filter data in a query based on specified conditions. It allows you to selectively retrieve rows from a table that meet specific criteria. When you include a
WHERE clause in your SQL query, only the rows that satisfy the given conditions are included in the result set. Here's how the
WHERE clause filters data in a query:
Basic Syntax:
The WHERE clause is typically used as part of a SELECT statement, but it can also be used in other SQL statements like
UPDATE and DELETE. The basic syntax for using the
WHERE clause in a SELECT statement is as follows:
SELECT column1, column2, ...
FROM table_name
WHERE condition;
column1, column2, ...: The columns you want to retrieve from the table.
table_name: The name of the table from which you're selecting data.
condition: The condition or set of conditions that define the filtering criteria. Rows that satisfy the condition are included in the result set.
Filtering Conditions:
Conditions in the WHERE clause are typically expressed using comparison operators (e.g.,
=, <, >, <=,
>=, !=) and logical operators (e.g., AND,
OR, NOT). You can use these operators to compare column values with constants, other column values, or expressions.
Example using a simple equality condition:
SELECT CustomerName, City
FROM Customers
WHERE City = 'New York';
This query retrieves all customers with the city 'New York'.
Compound Conditions:
You can create more complex conditions by combining multiple expressions using logical operators. For example, you can use
AND to specify that both conditions must be true, or OR to indicate that at least one condition should be true.
Example using a compound condition with AND:
SELECT ProductName, UnitPrice
FROM Products
WHERE CategoryID = 1 AND UnitsInStock > 0;
This query retrieves products in Category 1 that have units in stock greater than 0.
Negation with NOT:
You can use the NOT operator to negate a condition. For example,
NOT can be used to retrieve rows that do not satisfy a particular condition.
Example using NOT:
SELECT EmployeeName, Salary
FROM Employees
WHERE NOT Salary < 50000;
This query retrieves employees whose salary is not less than 50000.
Filtering by Multiple Conditions:
You can use multiple conditions within a WHERE clause to narrow down your result set further. When multiple conditions are used, all conditions must be satisfied for a row to be included in the result.
Example with multiple conditions:
SELECT ProductName, UnitPrice
FROM Products
WHERE CategoryID = 2 AND UnitsInStock > 0 AND Discontinued = 0;
This query retrieves products in Category 2 that are in stock and not discontinued.
In summary, the WHERE clause is a crucial component of SQL queries, allowing you to filter data based on specified conditions. It allows you to retrieve precisely the rows that meet your criteria, making SQL queries powerful and flexible for data retrieval and analysis.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The WHERE clause in SQL is used to filter data in a query based on specified conditions. It allows you to selectively retrieve rows from a table that meet specific criteria. When you include a WHERE clause in your SQL query, only the rows that satisfy the given conditions are included in the result set. Here's how the WHERE clause filters data in a query:
The WHERE clause is typically used as part of a SELECT statement, but it can also be used in other SQL statements like UPDATE and DELETE. The basic syntax for using the WHERE clause in a SELECT statement is as follows:
Conditions in the WHERE clause are typically expressed using comparison operators (e.g., =, <, >, <=, >=, !=) and logical operators (e.g., AND, OR, NOT). You can use these operators to compare column values with constants, other column values, or expressions.
Example using a simple equality condition:
This query retrieves all customers with the city 'New York'.
You can create more complex conditions by combining multiple expressions using logical operators. For example, you can use AND to specify that both conditions must be true, or OR to indicate that at least one condition should be true.
Example using a compound condition with AND:
This query retrieves products in Category 1 that have units in stock greater than 0.
You can use the NOT operator to negate a condition. For example, NOT can be used to retrieve rows that do not satisfy a particular condition.
Example using NOT:
This query retrieves employees whose salary is not less than 50000.
You can use multiple conditions within a WHERE clause to narrow down your result set further. When multiple conditions are used, all conditions must be satisfied for a row to be included in the result.
Example with multiple conditions:
This query retrieves products in Category 2 that are in stock and not discontinued.
In summary, the WHERE clause is a crucial component of SQL queries, allowing you to filter data based on specified conditions. It allows you to retrieve precisely the rows that meet your criteria, making SQL queries powerful and flexible for data retrieval and analysis.