Menu Close

MySQL WHERE Clause

MySQL Where Clause

In this article, you will learn everything about the MySQL WHERE clause with the help of examples. Sometimes, when you want to search records from the table that fulfill the specific query, In that situation you can use MySQL where clause.

MySQL Where Clause Introduction

WHERE is a reversed keyword in MySQL that is used to specify the search condition for the rows returned by the query. You can specify any type of search condition as your requirement.

Syntax

The following shows the syntax of the where clause.


SELECT select_list 
FROM table_name
WHERE search_condition;

Let’s break down above where clause syntax.

  • select_list represents the name of the columns which you want to select.
  • table_name represents the name of the table.
  • The search condition is a combination of one or more expressions using logical operators AND, OR, and NOT.

MySQL Where Clause Examples

To use the Where clause in MySQL, we have just created a table students with the following records.

SELECT * FROM students;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     1 | Vishvajit  | Rao       | MCA    | 2021-11-13 14:26:39 |
|     2 | John       | Doe       | Mtech  | 2021-11-13 14:26:39 |
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-13 14:26:39 |
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
|     6 | Aysha      | Garima    | BCA    | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+

a). MySQL Where clause with equal to operator.

In this example, we will select all the columns from the students table whose st_id is 5.

Example

SELECT * from students WHERE st_id = 5;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
+-------+------------+-----------+--------+---------------------+

b). MySQL Where clause with AND operator.

Here we are going to select students from the table where last_name is Kumar and course is MCA.

Example:

SELECT * from students WHERE last_name = 'Kumar' AND course = 'MCA';

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
+-------+------------+-----------+--------+---------------------+

c). MySQL WHERE clause with OR Operator.

Here we are going to select all the students from the students whose last_name is Kumar or the course is Btech.

Example

SELECT * from students WHERE last_name = 'Kumar' OR course = 'Btech';

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-13 14:26:39 |
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+
4 rows in set (0.00 sec)

d). MySQL WHERE clause with BETWEEN Operator.

Here we are going to select all the students from the students table where st_id lies between 3 and 5.

Example

SELECT * from students WHERE st_id BETWEEN 3 AND 5;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-13 14:26:39 |
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
+-------+------------+-----------+--------+---------------------+
3 rows in set (0.05 sec)

e). MySQL WHERE Clause with greater than ( > ) operator.

Here we are going to select all the columns from the students table where st_id is greater than 3.

Example

SELECT * from students WHERE st_id > 3;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
|     6 | Aysha      | Garima    | BCA    | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+

f). MySQL WHERE Clause with less than ( < ) operator.

Here we are going to select all the columns from the students table where st_id is less than 3.

Example

SELECT * from students WHERE st_id < 3;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     1 | Vishvajit  | Rao       | MCA    | 2021-11-13 14:26:39 |
|     2 | John       | Doe       | Mtech  | 2021-11-13 14:26:39 |
+-------+------------+-----------+--------+---------------------+
2 rows in set (0.00 sec)

g). MySQL WHERE Clause with greater than or equal ( >= ) operator

Here we are going to select all the columns from the students table where st_id is greater than or equal to 5.

Example

SELECT * from students WHERE st_id >= 5;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
|     6 | Aysha      | Garima    | BCA    | 2021-11-13 14:54:28 |
|     7 | Abhi       | Kumar     | MCA    | 2021-11-28 11:43:40 |
|     8 | Kartike    | Singh     | Btech  | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+

h). MySQL WHERE Clause with less than or equal ( <= ) operator

Here we are going to select all the columns from the students table where st_id is less than or equal to 5.

Example

SELECT * from students WHERE st_id <= 5;

Output


+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at          |
+-------+------------+-----------+--------+---------------------+
|     1 | Vishvajit  | Rao       | MCA    | 2021-11-13 14:26:39 |
|     2 | John       | Doe       | Mtech  | 2021-11-13 14:26:39 |
|     3 | Shivam     | Kumar     | B.A.   | 2021-11-13 14:26:39 |
|     4 | Pankaj     | Singh     | Btech  | 2021-11-13 14:54:28 |
|     5 | Hayati     | Kaur      | LLB    | 2021-11-13 14:54:28 |
+-------+------------+-----------+--------+---------------------+
5 rows in set (0.00 sec)

Conclusion

So, in this article, we have seen all about MySQL WHERE clause with the help of different examples along with various operators. Where clause in MySQL is one of the most important clauses because it specifies the search condition. Using the MySQL WHERE keyword you can search relevant records from your table.

I hope this article will help you. if you like this MySQL where clause tutorial, please share and keep visiting for further MySQL tutorials.

Reference:- Click Here

Thanks for reading ….

MySQL BETWEEN Operator
MySQL OR Operator

Related Posts