In SQL, the OR
operator is a logical operator used to combine multiple conditions in a WHERE
clause, such that at least one of the conditions must be true for the overall condition to be true. It is typically used when you want to retrieve rows that meet any of the specified conditions.
Here's the general syntax of using the OR
operator:
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
In this syntax:
column1, column2, ...
: Columns you want to retrieve data from.table_name
: The name of the table from which you want to retrieve data.condition1, condition2, condition3, ...
: Conditions that you want to combine using the OR
operator.Example:
Let's say we have a table named employees
with columns employee_id
, first_name
, and last_name
, and we want to retrieve all employees whose first name is 'John' or last name is 'Doe':
SELECT employee_id, first_name, last_name
FROM employees
WHERE first_name = 'John' OR last_name = 'Doe';
This query will return all rows from the employees
table where either the first_name
is 'John' or the last_name
is 'Doe'.
You can use the OR
operator to combine any number of conditions, and you can also use parentheses to control the order of evaluation if necessary. For example:
SELECT employee_id, first_name, last_name
FROM employees
WHERE (first_name = 'John' AND last_name = 'Doe') OR (first_name = 'Jane' AND last_name = 'Smith');
This query will return all rows from the employees
table where either the first_name
is 'John' and the last_name
is 'Doe', or the first_name
is 'Jane' and the last_name
is 'Smith'.
The OR
operator is useful for building complex conditional expressions in SQL queries, allowing you to express a wide range of logical conditions.