SQL AND & OR Operators
- Previous Page SQL where
- Next Page SQL Order By
AND and OR operators are used to filter records based on more than one condition.
AND and OR Operators
AND and OR can combine two or more conditions in the WHERE clause.
If both the first and second conditions are met, the AND operator displays a record.
If either of the first and second conditions is met, the OR operator displays a record.
Original Table (used in examples):
LastName | FirstName | Address | City |
---|---|---|---|
Adams | John | Oxford Street | London |
Bush | George | Fifth Avenue | New York |
Carter | Thomas | Changan Street | Beijing |
Carter | William | Xuanwumen 10 | Beijing |
AND Operator Example
Use AND to display all people with the last name "Carter" and the name "Thomas":
SELECT * FROM Persons WHERE FirstName='Thomas' AND LastName='Carter'
Result:
LastName | FirstName | Address | City |
---|---|---|---|
Carter | Thomas | Changan Street | Beijing |
OR Operator Example
Use OR to display all people with the last name "Carter" or the name "Thomas":
SELECT * FROM Persons WHERE firstname='Thomas' OR lastname='Carter'
Result:
LastName | FirstName | Address | City |
---|---|---|---|
Carter | Thomas | Changan Street | Beijing |
Carter | William | Xuanwumen 10 | Beijing |
Combine AND and OR Operators
We can also combine AND and OR (using parentheses to form complex expressions):
SELECT * FROM Persons WHERE (FirstName='Thomas' OR FirstName='William') AND LastName='Carter'
Result:
LastName | FirstName | Address | City |
---|---|---|---|
Carter | Thomas | Changan Street | Beijing |
Carter | William | Xuanwumen 10 | Beijing |
- Previous Page SQL where
- Next Page SQL Order By