SQL AND & OR Operators
The AND & OR operators are used to filter records based on more than one condition.
The AND operator displays
a record if both the first condition and the second condition is true.
The OR operator displays a record if either the first condition
or the second condition is true.AND Operator Example
“Persons” TABLE:
P_Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Suhail
|
Osmani
|
Maharaj
Nagar
|
Sitapur
|
2
|
Maxwel
|
Tom
|
Biswan
|
Sitapur
|
3
|
Rahi
|
Ayon
|
Okhla
|
Delhi
|
We use the following SELECT statement:
SELECT
* FROM Persons WHERE FirstName='Tom' AND
LastName='Maxwel'
|
P_Id
|
LastName
|
FirstName
|
Address
|
City
|
2
|
Maxwel
|
Tom
|
Biswan
|
Sitapur
|
OR Operator Example
Now we want to select only the persons with the first name equal to "Tom" OR the first name equal to "Osmani": We use the following SELECT statement:
SELECT
* FROM Persons WHERE FirstName='Tom' OR FirstName='Osmani'
|
P_Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Suhail
|
Osmani
|
Maharaj
Nagar
|
Sitapur
|
2
|
Maxwel
|
Tom
|
Biswan
|
Sitapur
|
9.Combining AND & OR
You can also combine AND and OR (use parenthesis to form complex expressions).Now we want to select only the persons with the last name equal to "Maxwel" AND the first name equal to "Tom" OR to "Osmani": We use the following SELECT statement:
SELECT
* FROM Persons WHERE LastName='Maxwel'
AND (FirstName='Tom' OR
FirstName='Osmani')
|
P_Id
|
LastName
|
FirstName
|
Address
|
City
|
2
|
Maxwel
|
Tom
|
Biswan
|
Sitapur
|
SQL AND & OR Operators
Reviewed by Admin
on
05:15:00
Rating:
No comments: