SQL AND / OR Operator
Submitted by admin on Tuesday, March 8, 2011 - 08:45.
The AND/OR can be used to make a complex or multiple condition for the WHERE clause while selecting, updating or deleting some data.
The AND works as follows
Condition 1 AND condition 2 AND……… condition N
We can use as many conditions as we want with the AND. This condition’s result will only come out to be true if all the conditions are specifies, just like the and truth table
False | False | False |
False | True | False |
False | True | False |
True | True | True |
Similarly we can specify multiple conditions with the OR operator, in OR if one condition is true then the overall condition is accepted to be true no matter how false other conditions are as long as one condition is true
False | False | False |
False | True | True |
False | True | True |
True | True | True |
Now How to use the AND /OR in SQL
Syntax
SELECT * FROM TABLE WHERE condtion 1 AND condition 2
Consider the following table
Firstname | Lastname | Age | Maritalstatus | Country |
John | Smith | 40 | Married | Usa |
Mathew | Simon | 30 | Married | UK |
Bill | Steve | 20 | Single | Usa |
Amanda | Rogers | 28 | Married | Germany |
Steve | Hills | 30 | Single | france |
Example
SELECT * FROM Users WHERE country=”USA” AND Age=”20”
Firstname | Lastname | Age | Maritalstatus | Country |
Bill | Steve | 20 | Single | Usa |
Similarly
SELECT * FROM Users WHERE country=”USA” OR Age=”30”
Firstname | Lastname | Age | Maritalstatus | Country |
John | Smith | 40 | Married | Usa |
Mathew | Simon | 30 | Married | UK |
Bill | Steve | 20 | Single | Usa |
Steve | Hills | 30 | Single | france |
Comments
Add new comment
- Add new comment
- 112 views