AND & OR Operator in SQL Query

AND & OR Operator in SQL Query

AND Operator is used in which you must have to satisfy the both or all condition for executing the next steps or getting result from SQL Query. It will not work if any one condition is false. In AND it must satisfy all condition to run.

OR Operator is used in which you must satisfy any one condition or both condition then it will go to next steps or getting result from SQL Query. It will not work if all condition is false. In OR it must satisfy any one condition to run.

Syntax for AND:

Select * from table_name where columnname = 'value' and columnname2 = 'value2';

Syntax for OR:

Select * from table_name where columnname = 'value' or columnname2 = 'value2';

Example for AND operator:
Example will return the value of employee name from employees table if it satisfy both condition on where clause .

Select employee_name from employees where salary > 10000 and department_no = 10;

Example for OR Operator:
Example will return the value of employee name if any one condition is correct.

Select employee_name from employees where salary > 10000 or Department_no = 10;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.