In this tutorial, you will learn everything about MySQL AND operator with the help of examples. AND operator in MySQL is used to combine one or two expressions together.
In the previous MySQL tutorial, we have seen all about, MySQL SELECT DISTINCT statement to eliminate duplicate rows from the result set.
Headings of Contents
MySQL AND Operator Examples
In MySQL, AND Operator is used to combining more than one expression or condition together. The only result will come, satisfying all the conditions separated by AND operator.
Syntax
The following is the syntax of AND operator.
SELECT select_list FROM table_name WHERE condition1 AND condition2....;
MySQL AND Operator Examples
Here we are going to use AND operator with the help of examples to specify more than one condition. We will use students table, Which contains the following records.
Example:
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). Let’s select all the records from the students table where last_name and course are Singh and Btech.
Example
SELECT * FROM students where last_name ='Singh' AND course = 'Btech';
Output
+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at |
+-------+------------+-----------+--------+---------------------+
| 4 | Pankaj | Singh | Btech | 2021-11-13 14:54:28 |
| 8 | Kartike | Singh | Btech | 2021-11-28 11:44:22 |
+-------+------------+-----------+--------+---------------------+
Note:- You can verify the result from the above table.
b). Here we are going to select students where last_name is Kumar and st_is is greater than 2.
Example
SELECT * FROM students where last_name ='Kumar' AND st_id > 2;
Output
+-------+------------+-----------+--------+---------------------+
| st_id | first_name | last_name | course | created_at |
+-------+------------+-----------+--------+---------------------+
| 3 | Shivam | Kumar | B.A. | 2021-11-13 14:26:39 |
| 7 | Abhi | Kumar | MCA | 2021-11-28 11:43:40 |
+-------+------------+-----------+--------+---------------------+
Summary
Here we have seen all about MySQL AND operator to apply more than one expression together with the help of the examples.
AND Operator perform the following:-
- MySQL AND operator combine one or more conditions together.
- MySQL Returns True if all the conditions are satisfied other its returns False.
- AND Operator used with MySQL WHERE Clause.
MySQL Tutorial:- Click Here
Ref:- Click Here
Thanks for reading …..