The WHERE
clause in SQL is used to extract records that meet a specified condition from a given table.
The WHERE
clause is commonly used with the comparison operators. They include:
=
(Equal to operator)!=
(Not Equal to operator)>
( Greater than operator)<
( Less than operator)>=
(Greater than or Equal to operator)<=
(Less than or Equal to operator)Be that as it may, it is worth noting that:
WHERE
clause has the primary key with the =
operator, then only one record can be affected, if found.WHERE
clause has the non-primary key with the =
operator, then multiple records can be affected, if found.<
or >
, then multiple records are affected, if found.The syntax of the WHERE
clause alongside the SELECT
statement is given below:
SELECT column1, column2,...
FROM name_of_table
WHERE condition;
We’ll work with the following database schema:
CustomerID | CustomerName | Country |
1 | Theo David | U.S.A |
2 | Peter Mark | United Kingdom |
3 | Antonio Clark | Mexico |
4 | Benjamin Monday | U.S.A |
In the code below, we’ll use the WHERE
clause alongside the SELECT
statement to retrieve data from the Customers
table.
-- using the WHERE clause alongside the SELECCT statementSELECT * FROM CustomersWHERE CustomerID=1;
Customers
table and populate it with the values according to the defined database schema on our platform.WHERE
clause alongside the SELECT
statement with a condition of where the CustomerID
is 1
.