Friday, September 17, 2010

SQL AND & OR Operators

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:



Now we want to select only the persons with the first name equal to "Nimish" AND the last name equal to "Prabhu":

We use the following SELECT statement:

SELECT * FROM Persons
WHERE FirstName='Nimish'
AND LastName='Prabhu'





OR Operator Example

Now we want to select only the persons with the first name equal to "Nimish" OR the first name equal to "Amogh":

We use the following SELECT statement:

SELECT * FROM Persons
WHERE FirstName='Nimish'
OR FirstName='Amogh'





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 "Prabhu" AND the first name equal to "Nimish" OR to "Amogh":

We use the following SELECT statement:

SELECT * FROM Persons WHERE
LastName='Prabhu'
AND (FirstName='Nimish' OR FirstName='Amogh')

0 comments:

Post a Comment