MySQL WHERE
In this tutorial, we are going to see how to use the clause WHERE in the SELECT statement to filter the result rows. The clause WHERE allows you to specify a condition for the rows returned by a query.
Syntax:
SELECT column1, column2, ..., column_n FROM tableX WHERE condition;
[st_adsense]
Example – With one condition
In the following example, we have used the clause WHERE to filter our results from the customers table. The SELECT statement above will return all rows in the customers table where the name is “Alex”. Since * is used in SELECT, all the columns in the customers table appear in the output.
SELECT * FROM Customers WHERE Name = 'Alex';
Output:
+----------+-----------+--------+-----------------------+ | ID | Name | age | Address | +----------+-----------+--------+-----------------------+ | 101 | Alex | 25 | San Francisco | +----------+-----------+--------+-----------------------+
Example – With the AND condition
The following query uses the AND operator to find customers located in Boston whose age is greater than 20:
SELECT * FROM Customers WHERE Address = 'Boston' AND Age > 20;
Output:
+----------+-----------+--------+-----------------------+ | ID | Name | age | Address | +----------+-----------+--------+-----------------------+ | 103 | Jean | 35 | Boston | | 104 | Bob | 40 | Boston | +----------+-----------+--------+-----------------------+[st_adsense]
Example – With the OR condition
For example, to get the customers located in San Francisco or Boston , use the OR operator in the clause WHERE as follows:
SELECT * FROM Customers WHERE Address = 'San Francisco' OR Address = 'Boston';
Output:
+----------+-----------+--------+-----------------------+ | ID | Name | age | Address | +----------+-----------+--------+-----------------------+ | 101 | Alex | 25 | San Francisco | | 102 | Emily | 15 | San Francisco | | 103 | Jean | 35 | Boston | | 104 | Bob | 40 | Boston | +----------+-----------+--------+-----------------------+[st_adsense]