ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

SQL AND & OR Operators

sqltuts

We can link multiple WHERE statements by AND/OR statements.

AND means that all the conditions are TRUE.

OR means, at least, one condition is TRUE.

 

For example, consider the following table Employee:

 

Employee_id First_name Last_name sex
123 Mack Johnson male
124 Jack Obama male
125 Queen Khan female

 

Example of using AND:

  1. SELECT First_name
  2. FROM Employee
  3. WHERE sex = ‘male’
  4. AND Last_name = ‘Johnson’;

 

This will give the following result:

First_name
Mack

Example of using OR

 

  1. SELECT First_name
  2. FROM Employee
  3. WHERE sex = ‘male’
  4. OR Last_name = ‘Johnson’;

 

First_name
Mack
Jack

 

In this case , we see two names as one condition (sex=’male’) for them is true.

Example of using AND & OR

 

  1. SELECT First_name
  2. FROM Employee
  3. WHERE sex = ‘male’
  4. AND (Last_name = ‘Johnson’ OR  Last_name = ‘Obama’);

 

This will give the following output:

First_name
Mack
Jack

 

Uses of AND OR in different aspects

  1. SELECT *
  2. FROM customers
  3. WHERE (state = 'UTTAR PRADESH' AND last_name = 'PRADESH')
  4. OR (customer_id > 43433);
  1. SELECT customer_id, last_name, first_name
  2. FROM customers
  3. WHERE (last_name = 'sharma')
  4. OR (last_name = 'singh' AND state = 'UTTAR PRADESH')
  5. OR (last_name = 'goyal' AND status = 'Active' AND state = 'Himachhal pradesh');
  1. INSERT INTO consumers
  2. (consumers _id, consumers _name)
  3. SELECT consumers _id, consumers _name
  4. FROM consumers
  5. WHERE (consumers _name = 'HP' OR consumers _name = 'TTA')
  6. AND consumers _id >= 4343;
Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!