OR
OR is an operator in SQL that is used within the WHERE clause to combine multiple conditions. It allows for the retrieval of a record if either of the specified conditions are met.
Example
SELECT * FROM CustomersWHERE Country='Germany' OR Country='France';
Output
| CustomerID | CustomerName | ContactName | Address | City | Country ||------------|--------------|--------------|-----------------|------------|----------|| 1 | Alfreds | Maria Anders | Obere Str. 57 | Berlin | Germany || 2 | Ana Trujillo | Ana Trujillo | Avda Montero 37 | México D.F | France || 3 | Antonio | Antonio Moreno|Mataderos 2312 | Mexico | France |
Explanation
The code returns all records from the Customers table where the country is either Germany or France. This demonstrated the OR logic, which returns a record if one or both of the conditions are true.
Example
SELECT *FROM employeesWHERE department = 'Sales'OR department = 'Marketing';
Output
+-----------+------+|employee_id|department|+-----------+------+|1 |Sales||2 |Marketing|+-----------+------+
Explanation
This SQL query selects all columns from the employees
table where the department
is either Sales
or Marketing
. The OR
clause is used to select records that fulfill any of the given conditions.
Example
SELECT * FROM EmployeesWHERE City = 'London' OR City = 'Paris';
Output
| EmployeeID | FirstName | LastName | City ||------------|-----------|----------|-------|| 1 | John | Doe | London|| 3 | Jane | Smith | London|| 5 | Pierre | Dubois | Paris |
Explanation
The OR operator is used in the SQL query to filter the records and return only the employees who are located in either ‘London’ or ‘Paris’. In the output, three records are returned: two from ‘London’ and one from ‘Paris’.
Example
SELECT employee_nameFROM employeesWHERE employee_department = 'Sales'OR employee_department = 'Marketing';
Output
EMPLOYEE_NAME-------------John DoAnn SmithSandra Moore... and so forth ...
Explanation
The SQL statement selects the names of employees in either the ‘Sales’ or ‘Marketing’ departments. The ‘OR’ clause ensures that any employee from either of these departments is included in the results.
Example
CREATE TABLE Players ( ID INT PRIMARY KEY, Name TEXT, Age INT, Team TEXT);
INSERT INTO Players (ID, Name, Age, Team)VALUES (1, 'David', 30, 'TeamA'), (2, 'John', 28, 'TeamB'), (3, 'Lucas', 32, 'TeamC'), (4, 'Mark', 31, 'TeamA');
SELECT * FROM PlayersWHERE Age = 30 OR Team = 'TeamB';
Output
ID | Name | Age | Team---|-------|-----|------1 | David | 30 | TeamA2 | John | 28 | TeamB
Explanation
The SELECT
statement with the OR
clause in the WHERE
condition returns records that meet either of the specified conditions. In the example, it returns records where the player’s Age
is 30 or the Team
is ‘TeamB’.