MySQL

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;

 

 

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                |
+----------+-----------+--------+-----------------------+
AND Operator in MySQLAND Operator in MySQLIn this tutorial, we are going to see how the AND operator combines multiple Boolean expressions to filter data. The AND operator is a logical…Read More
 

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                |
+----------+-----------+--------+-----------------------+
OR Operator in MySQLOR Operator in MySQLIn this tutorial, we are going to see how to use the OR operator in MySQL. The OR operator combines two Boolean expressions and returns…Read More mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *