SQL AND & OR Operators



SQL AND & OR Operators

The AND and OR Operator are used to returns records match with more than one condition in WHERE condition.

The AND Operator – The AND operator is used to returns those records whose value must be match with all the condition WHERE.

The OR  Operator – The OR Operator is used to returns those records whose value match with any value in WHERE condition.

In last sql post we leaned LIKE Operator and IN Operator and BETWEEN Operator. In this sql tutorial we will learn SQL AND & OR Operator with syntax and example.

The AND Operator Syntax :

SELECT col1, col2,col3 FROM table_name WHERE col1 = val1 AND col2 = val2 AND col3=val3

In above sql statement returns records, if they satisfy all the three WHERE condition.

The AND Operator Example  :

Here, we have table Student and we want to select records whose id is 1 and name is ‘meera’

Here, we have table Student with below records.

The SQL Student Table :

ID Name City Email
1 Meera Bombay meera@yahoo.com
2 Ram Surat ram@yahoo.com
3 Manoj Bombay manoj@yahoo.com
4 Vaidehi Ahmedabad meera@yahoo.com

SELECT * FROM Student WHERE ID=1 AND Name=’Meera’

The SQL AND Operator Example Result is :

ID Name City Email
1 Meera Bombay meera@yahoo.com

 

The SQL OR Operator  Syntax:

SELECT col1, col2,col3 FROM table_name WHERE col1 = val1 OR col2 = val2 OR col3=val3

In above sql statement returns records, if they satisfy any WHERE condition.

The OR Operator Example  :

Here, we have table Student and we want to select records whose id is 1 or city is ‘Surat’

SELECT * FROM Student WHERE ID=1 OR City = ‘Surat’

The Result of SQL OR Operator is :

ID Name City Email
1 Meera Bombay meera@yahoo.com
2 Ram Surat ram@yahoo.com

Leave a Reply

Your email address will not be published. Required fields are marked *