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
The "Persons" table:
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
3
|
Christopher
|
Franklin
|
America
|
KK
|
Now we want to select only the persons with the first name equal
to "Bala" AND the last name equal to "Ji"
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Bala' AND LastName='Ji' |
The result-set will look like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
OR Operator Example
Now we want to select only the persons with the first name equal
to "Bala" OR the first name equal to "Anto"
We use the following SELECT statement:
SELECT * FROM Persons
WHERE FirstName='Bala' OR FirstName='Anto' |
The result-set will look like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
1
|
Navis
|
Anto
|
Madras
|
TVL
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
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
"Ji" AND the first name equal to "Bala" OR to "Anto"
We use the following SELECT statement:
SELECT * FROM Persons WHERE
LastName='Ji' AND (FirstName='Bala' OR FirstName='Anto') |
The result-set will look like this:
Id
|
LastName
|
FirstName
|
Address
|
City
|
2
|
Ji
|
Bala
|
Bombay
|
TVL
|
0 comments:
Post a Comment