What Is Where Clause in SQL?
The WHERE clause is a very powerful tool that can be used to filter data from a database. It can be used to select specific rows, columns, and values from a table. The WHERE clause is used in conjunction with the SELECT statement to specify the conditions that the rows in the result set must meet.
Watch this Where in SQL video
The syntax for the WHERE clause is as follows:
WHERE <condition>
The <condition>
is a Boolean expression that evaluates to either TRUE or FALSE. If the <condition>
evaluates to TRUE, then the row is included in the result set. If the <condition>
evaluates to FALSE, then the row is excluded from the result set.
The <condition>
can be any valid Boolean expression. This includes comparisons, logical operators, and functions. For example, the following are all valid WHERE clauses:
-
WHERE name = 'John'
-
WHERE age > 21
-
WHERE country IN ('USA', 'Canada', 'UK')
-
WHERE date_of_birth BETWEEN '1990-01-01' AND '2000-01-01'
The WHERE clause can be used to perform complex filtering operations. For example, you could use the WHERE clause to select all rows where the name column is equal to John and the age column is greater than 21. You could also use the WHERE clause to select all rows where the country column is in the list of countries USA, Canada, and UK, and the date_of_birth column is between 1990-01-01 and 2000-01-01.
The WHERE clause is a powerful tool that can be used to filter data from a database. By understanding how to use the WHERE clause, you can query your database more effectively and efficiently.
Here are some additional tips for using the WHERE
clause:
- Use the
LIKE
operator to match patterns in strings. For example, you could use the LIKE
operator to select all rows where the name column contains the letter “J”.
- Use the
IN
operator to match a value against a list of values. For example, you could use the IN
operator to select all rows where the country column is equal to USA, Canada, or UK.
- Use the
BETWEEN
operator to match a value between two other values. For example, you could use the BETWEEN
operator to select all rows where the date_of_birth column is between 1990-01-01 and 2000-01-01.
By following these tips, you can use the WHERE
clause to filter data from your database more effectively and efficiently.
Here is an example of how to use the WHERE
clause to select all rows where the name column is equal to John:
SELECT * FROM customers
WHERE name = 'John';
This query will return all rows from the customers
the table where the value in the name
the column is equal to John.
Here is another example of how to use the WHERE
clause to select all rows where the age column is greater than 21:
SELECT * FROM customers
WHERE age > 21;
This query will return all rows from the customers
table where the value in the age
column is greater than 21.