The SQL WHERE
clause is used to filter records in a database query, allowing
you to retrieve only those rows that meet specific conditions. It is commonly
used with SELECT
, UPDATE
, and DELETE
statements to specify which records
should be affected or returned
Key Points about the SQL WHERE Clause
-
Purpose : Filters rows based on a condition or set of conditions.
-
Syntax :
sql SELECT column1, column2, ... FROM table_name WHERE condition;
-
Conditions : Can use comparison operators (
=
,<
,>
,<=
,>=
,<>
), logical operators (AND
,OR
,NOT
), and special operators likeLIKE
,IN
,BETWEEN
,IS NULL
- Text vs Numeric : Text values in conditions require single quotes, numeric values do not
- Multiple Conditions : Use
AND
to require all conditions to be true, orOR
to require any condition to be true
- Performance : Using
IN
with a list of values is generally faster and cleaner than multipleOR
conditions on the same field
Examples
-
Select customers from Mexico with salary above 2000:
sql SELECT ID, NAME, SALARY FROM CUSTOMERS WHERE Country = 'Mexico' AND SALARY > 2000;
-
Select customers whose name is either Khilan, Hardik, or Muffy:
sql SELECT * FROM CUSTOMERS WHERE NAME IN ('Khilan', 'Hardik', 'Muffy');
-
Select customers from Spain whose name starts with 'G':
sql SELECT * FROM Customers WHERE Country = 'Spain' AND CustomerName LIKE 'G%';
The WHERE
clause is supported by all major SQL databases such as MySQL,
PostgreSQL, Oracle, SQL Server, SQLite, and others, making it a fundamental
tool for querying and manipulating data efficiently
. In summary, the WHERE
clause is essential for filtering data in SQL
queries by specifying conditions that rows must satisfy to be included in the
results or affected by data modification statements