SQL Where
In SQL, the WHERE clause is used to specify conditions for filtering rows in a query result. It allows you to extract only the rows that meet certain criteria. The general syntax of the WHERE clause is as follows:
sql
SELECT column1, column2, ...
FROM table
WHERE condition;
Here, column1
, column2
, and so on represent the columns you want to retrieve from the table, and table
represents the name of the table you are querying. The condition
is the expression that specifies the criteria for filtering rows.
The condition can be constructed using various operators, such as:
- Comparison operators (e.g.,
=
,<>
,>
,<
,>=
,<=
) to compare values. - Logical operators (
AND
,OR
,NOT
) to combine multiple conditions. - Pattern matching operators (
LIKE
,NOT LIKE
) to match patterns in string values. - NULL-related operators (
IS NULL
,IS NOT NULL
) to check for null values.
Here are a few examples to illustrate the usage of the WHERE clause:
- Retrieve all rows from the “Customers” table where the “Country” column is set to ‘USA’:
sql
SELECT *
FROM Customers
WHERE Country = 'USA';
- Retrieve rows from the “Orders” table where the “TotalAmount” column is greater than or equal to 1000 and the “Status” column is not ‘Cancelled’:
sql
SELECT *
FROM Orders
WHERE TotalAmount >= 1000 AND Status <> 'Cancelled';
- Retrieve rows from the “Employees” table where the “LastName” column starts with ‘Smi’ and the “Age” column is less than 30:
sql
SELECT *
FROM Employees
WHERE LastName LIKE 'Smi%' AND Age < 30;
These are just a few examples, and you can construct more complex conditions using different operators as per your requirements.