MySQL AND Operator

MySQL | AND Operator: Learn about the MySQL AND Operator, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 12, 2022

AND Operator

We know how AND operator works, if both the conditions are true then only the result will be true else it will be false. In MySQL when we want to apply more than one condition in a single query such that both conditions should follow the result, in such case we use AND operator.

AND Operator Syntax

SELECT column_name
WHERE condition1 AND condition1;

Let us see some examples;

AND Operator Examples

Suppose we have a table named "student_details" and inside that table, we have the following columns;

MySQL AND Operator (Example 1)

Case 1: Suppose we want to know the student name whose department should be "CSE" gender should be male, in such case when the result is dependent on both the condition we'll use AND operator,

SELECT * FROM student_details
WHERE student_department='CSE' AND Gender='Male';
MySQL AND Operator (Example 2)

As you can see in the result, both conditions are followed.

Case 2: Suppose I want to know the student name whose department should be "CSE", year should be 1st and gender should be male in such case we will use two AND operators and we will separate the conditions with a comma,

SELECT * FROM student_details 
WHERE student_department='CSE' AND Gender='Male' AND year='1';
MySQL AND Operator (Example 3)

As you can see, we have only three boys who fulfill the condition.

Case 3: Suppose we want to arrange the names of all the girls of the "CSE" department in descending order in such case,

SELECT * FROM student_details
WHERE student_department='CSE' AND Gender='Female'
ORDER BY Student_name DESC;
MySQL AND Operator (Example 4)

Here to order the result in descending order, ORDER BY clause is used. Also, you can see the result fulfills our condition as all the girls from the "CSE" department are displayed in descending order.


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.