Skip to main content
Log inGet a demo
Back to SQL Dictionary
Logical operators

SQL OR

What is SQL OR?

The SQL OR operator is a logical operator used to combine multiple conditions in a WHERE clause to filter data from a database table. It allows you to retrieve rows that satisfy at least one of the specified conditions. The OR operator is used to broaden the scope of your query, selecting rows that meet any of the given criteria.

When you would use it

You would use the SQL OR operator when you want to filter data from a table based on multiple conditions, where you want to include rows that satisfy at least one of the conditions. It is used to create more flexible queries, where the result set includes rows that match any of the specified criteria.

Syntax

The syntax for using the SQL OR operator is as follows:

SELECT columns
FROM table_name
WHERE condition1 OR condition2;
  • columns: The columns you want to retrieve in the query.
  • table_name: The name of the table containing the data.
  • condition1 and condition2: The conditions that need to be met. If either condition is true, the row will be included in the result set.

Parameter values

  • columns: The columns you want to retrieve in your query.
  • table_name: The name of the table where the data is stored.
  • condition1 and condition2: The specific conditions that, if true, will include the row in the result set.

Example query

Suppose we have a table named "products" with columns "product_id," "product_name," and "price." We want to retrieve products that are either priced under $50 or have the word "Discount" in their name:

SELECT product_id, product_name, price
FROM products
WHERE price < 50 OR product_name LIKE '%Discount%';

In the above query, we use the OR operator to combine conditions for the price and product name.

Example table response

Assuming the "products" table contains the following data:

| product_id | product_name        | price |
|------------|--------------------- | ----- |
| 1          | Discounted Widget   | 45.00 |
| 2          | Premium Gadget      | 75.00 |
| 3          | Budget Gizmo        | 35.00 |
| 4          | Super Sale Widget   | 40.00 |
| 5          | High-End Gizmo      | 95.00 |

The query mentioned earlier would return the following result:

| product_id | product_name        | price |
|------------|--------------------- | ----- |
| 1          | Discounted Widget   | 45.00 |
| 3          | Budget Gizmo        | 35.00 |
| 4          | Super Sale Widget   | 40.00 |

This result includes products that are either priced under $50 or have the word "Discount" in their name, meeting at least one of the specified conditions.

Use cases

  • Creating complex filtering criteria in SQL queries by combining multiple conditions.
  • Broadening the scope of your query to retrieve rows that meet any of the specified criteria.
  • Constructing queries that accommodate multiple filter conditions.

SQL languages this is available for

The SQL OR operator is a standard SQL feature and is available in most relational database management systems (RDBMS) that support SQL. This includes popular RDBMS like MySQL, PostgreSQL, Oracle, SQL Server, and SQLite. The specific syntax and behavior may vary slightly between database systems, but the fundamental functionality remains the same.

Related

SQL AND

SQL NOT

SQL NVL

Ready to put your SQL knowledge to work?

Practice writing SQL to call data from the warehouse and sync it into Google Sheets in this 5 minute interactive demo.

Hightouch Audiences user interface.