SQL AND & OR


The SQL AND and OR operators are used to combine multiple conditions to narrow data in an SQL statement. These two operators are called conjunctive operators.

These operators provide a means to make multiple comparisons with different operators in the same SQL statement.


The AND Operator

The AND operator allows the existence of multiple conditions in an SQL statement's WHERE clause.

Syntax:

SELECT column1, column2, columnN 
FROM table_name
WHERE [condition1] AND [condition2]...AND [conditionN];

Example

Consider the following student table

SidNameFee
11004556Ashwani5000
11211855Sachin8000
11211401Sanjay12000
11211854Amit10000
SELECT * FROM student WHERE fee > 8000
Output
SidNameFee
11211401Sanjay12000
11211854Amit10000

The OR Operator

The OR operator is used to combine multiple conditions in an SQL statement's WHERE clause.

Syntax:

SELECT column1, column2, columnN 
FROM table_name
WHERE [condition1] OR [condition2]...OR [conditionN]

Example

Consider the following student table

sidnamefee
11004556Ashwani5000
11211855Sachin8000
11211401Sanjay12000
11211854Amit10000
SELECT * FROM student WHERE sid=11211855 OR name='Sachin'
Output
sidnamefee
11211855Sachin8000

Share this article on