The MySQL WHERE clause is used to filter the rows that are returned by a SELECT statement. It can be used to filter the rows based on the values in the columns, or based on the results of expressions.
The basic syntax for the WHERE clause is as follows:
SELECT column_name(s)
FROM table_name
WHERE condition;
The condition
is an expression that evaluates to TRUE or FALSE. The rows that are returned by the SELECT statement are the rows where the condition
evaluates to TRUE.
Here is an example of a simple WHERE clause:
SELECT *
FROM customers
WHERE country = 'USA';
This statement will return all of the rows in the customers
table where the country
column is equal to 'USA'.
You can also use the WHERE clause to filter the rows based on the results of expressions. For example, the following statement will return all of the rows in the customers
table where the order_total
column is greater than or equal to 100:
SELECT *
FROM customers
WHERE order_total >= 100;
You can also use the WHERE clause to filter the rows based on the values in multiple columns. To do this, simply list the columns and their conditions in the WHERE clause, separated by AND or OR operators.
For example, the following statement will return all of the rows in the customers
table where the country
column is equal to 'USA' and the order_total
column is greater than or equal to 100:
SELECT *
FROM customers
WHERE country = 'USA' AND order_total >= 100;
The WHERE clause is a powerful tool for filtering the rows that are returned by a SELECT statement. By using the WHERE clause, you can easily return only the rows that you need.
Here are some additional tips for using the WHERE clause:
- You can use the WHERE clause with any type of SELECT statement, including SELECT statements that use the GROUP BY or ORDER BY clauses.
- You can use the WHERE clause with any type of expression, including expressions that use arithmetic operators, comparison operators, and logical operators.
- You can use the WHERE clause to filter the rows that are returned by a subquery.
Overall, the WHERE clause is a valuable tool for any database administrator or developer. By using the WHERE clause, you can easily filter the rows that are returned by your SELECT statements to return only the rows that you need.