SQL WHERE Clause

The SQL WHERE clause is a fundamental part of the SQL language, being used to filter records. It specifies a condition while retrieving the data from a single table or by joining with multiple tables. If the given condition holds true, then only the specific value(s) from the table will be included in the output.

Here's a basic example of a WHERE clause:

SELECT column1, column2,...
FROM table_name
WHERE condition;

In the code above, column1, column2,... represents the fields in the table that you wish to select. table_name stands for the name of the table from where you want to retrieve the data, and condition refers to the criteria that a record must meet for it to be selected.

For instance, if you have a "Customers" table and you want to find all customers who live in "New York", you would use the WHERE clause as follows:

SELECT * 
FROM Customers
WHERE City='New York';

The WHERE clause is not only limited to the SELECT statement, but it can also be used in UPDATE, DELETE, etc., where you need to specify certain conditions. Here's an example in the context of an UPDATE statement:

UPDATE table_name
SET column1 = value1, column2 = value2,...
WHERE condition;

Again, condition signifies the criteria that a record must meet for it to be updated. It's important to use the WHERE clause carefully in such situations because omitting the WHERE clause in an UPDATE statement will update all records in the table.

Overall, the WHERE clause is a powerful tool in SQL for data filtering and manipulation based on certain conditions.

Read next:

Order By

Read previous:

Select statement