How to use AND operator in SQ, explain with examples?

LAST READ: FEB 29 2024 | Q. BY: HIMANSHU GUJAR

T

Tuku

use of AND operator in SQL


Use of AND operator in SQL


AND is a logical operator.

AND operator is used to combining two boolean expressions, The complete combination returns TRUE only when both expressions are TRUE.


OR

AND operator is used to filtering records based on more than one condition. It returns TRUE when all conditions are TRUE.


OR

AND is a logical operator in SQL.

AND TRUE is all the conditions separated by AND is TRUE.



Syntax of AND Operator


select col1, col2, ... where condition1 AND condition2 AND condition...;

OR

select col1, col2, ... where expression1 AND expression2 AND expression...;


Note: In the above, both syntax condition and expression have the same meaning only the terms are different in calling i.e. some people prefer to call condition and some prefer expression. Let's see some examples to understand AND operator in a better way.


LET'S ASSUME


Let's assume that there is a table tbl_Employee, it has 6 columns that are Employee_id, Name, Age, City, Company, Salary. Suppose there are more than 1000 records in the table then let's filter records based on some conditions that are given below.


Example 1:

Find the list of all employees where the employee name is 'Tuku' and the city is 'New Delhi'.

SQL Query:

select * from tbl_Employee where Name='Tuku' AND City='New Delhi'


Example 2:

Find the list of all employee where employee Age>40, City is 'New Delhi' and Salary>50000

SQL Query:

select * from tbl_Employee where Age>40 AND City='New Delhi' AND Salary>50000


Example 3:

Find the list of all employees where employee Name is 'Tuku', Age<30, Company is not IBM and Salary>50000.

SQL Query:

select * from tbl_Employee where Name='Tuku' and Age<30 AND NOT Company='IBM'


This was all about the AND operator in SQL, I have tried my best to answer this question if you find this useful then press the below vote button and if you have further any query then kindly comment your query I will try to solve the query.

1 votes

Your Answer

Email Us: advertise@gdatamart.com

Donate Us: Support to GDATAMART

LifeStyle & Fun

© 2024 GDATAMART.COM (All Rights Reserved)