sql
  1. sql-or-clause

SQL OR Clause

The SQL OR clause is used to retrieve data based on multiple conditions. It is used to combine two or more conditions, and if either of the conditions is true, then the query returns the corresponding result. The OR clause is often used with the WHERE clause.

Syntax

SELECT column1, column2, ... 
FROM table_name 
WHERE condition1 OR condition2 OR condition3 ...;

Example

Suppose we have a table "employees" with columns "id", "name", "department", and "salary". We want to retrieve all employees who are either from the "HR" or "Marketing" department.

SELECT * FROM employees 
WHERE department='HR' OR department='Marketing';

Output

id name department salary
1 John HR 50000
2 Mary Marketing 60000
3 Tom HR 45000
4 Jane Marketing 55000

Explanation

The SQL query searches for employees whose department is either "HR" or "Marketing". The OR clause combines the two conditions and returns all employees who satisfy either one.

Use

The OR clause is used when multiple conditions need to be checked in a WHERE clause. It is useful when the result can satisfy any of the conditions provided.

Important Points

  • The OR clause is case-insensitive.
  • The conditions separated by the OR clause must be enclosed in parentheses.
  • If the OR clause is used with the WHERE clause, it returns all rows that satisfy either one of the conditions.
  • If the OR clause is used without the WHERE clause, it returns the result of all rows in a table.

Summary

  • The SQL OR clause is used to retrieve data based on multiple conditions.
  • It is used to combine two or more conditions using the logical operator OR.
  • It is commonly used with the WHERE clause.
  • The OR clause is case-insensitive and the conditions need to be enclosed in parentheses.
  • It returns all rows that satisfy either one of the conditions.
Published on: