SQL
[Basic Grammar] SQL OR Operator
grace21110
2023. 10. 13. 18:00
반응형
The WHERE clause can contatin one or more OR operators which are used to filter records based on more than one condition.
Example
Select all students from South Korea to China:
SELECT * FROM Students
WHERE Country = 'South Korea' OR Country = 'China';
- Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
- At Least One Condition Must be True
Example
Either City is "Berlin", CustomerName starts with the letter "G" or Country is "Norway":
SELECT * FROM Customers
WHERE City = 'Berlin' OR CustomerName LIKE 'G%' OR Country = 'Norway';
- Combining AND and OR
Example
Select all Korean students that starts with either "K" or "L":
SELECT * FROM Students
WHERE Country = "South Korea' AND StudentName LIKE 'K%' OR StudentName LIKE 'L%';
Exercises
1. Retrieve all customers where the "Country" column is either 'USA' or 'Canada.'
SELECT * FROM Customers
WHERE Country = 'USA' OR 'Canada';
2. Fetch all products from the "Electronics" category or the "Appliances" category.
SELECT * FROM Products
WHERE Category = 'Electronics' OR Category = 'Appliances';