SQL Keywords

SQL OR

What is the SQL OR operator?

Returns TRUE when at least one of two or more Boolean expressions is TRUE.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL OR: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, Redshift, DB2, SAP HANA

SQL OR Full Explanation

SQL OR is a logical operator used to combine two or more Boolean expressions in a WHERE, HAVING, JOIN, or CASE clause. If any of the expressions evaluates to TRUE, the entire OR condition is TRUE. When all expressions are FALSE or NULL, the result is FALSE. NULL behaves like UNKNOWN; if one operand is TRUE and another is NULL, the overall result is TRUE because OR only needs one TRUE. OR has lower precedence than AND, so parenthesis are recommended when mixing operators. OR supports any comparison operator ( =, <>, IN, LIKE, BETWEEN, EXISTS ) and subqueries. Excessive OR chains can hurt performance because most query planners cannot use indexes efficiently across multiple OR predicates; consider UNION ALL or derived tables when many ORs reference the same indexed column.

SQL OR Syntax

expression1 OR expression2 [OR expressionN]

SQL OR Parameters

Example Queries Using SQL OR

-- Return users who signed up in 2023 OR have admin role
SELECT id, email
FROM users
WHERE EXTRACT(year FROM signup_date) = 2023
  OR role = 'admin';

-- Fetch orders that are either pending delivery OR placed by VIP customers
SELECT order_id, status, customer_id
FROM orders
WHERE status = 'pending'
   OR customer_id IN (
        SELECT id FROM customers WHERE vip = TRUE
   );

-- Combine OR with AND using parentheses
SELECT *
FROM events
WHERE (event_type = 'login' OR event_type = 'logout')
  AND occurred_at >= CURRENT_DATE - INTERVAL '7 day';

Expected Output Using SQL OR

  • Each query returns rows where at least one specified condition evaluates to TRUE
  • Rows failing all OR conditions are excluded

Use Cases with SQL OR

  • Filter records that match any value in a list without using IN.
  • Evaluate alternate business rules, such as free shipping for orders over $100 OR for VIP customers.
  • Combine multiple pattern matches in text searches.
  • Write fallback logic in CASE statements to map several conditions to one result.

Common Mistakes with SQL OR

  • Forgetting parentheses when mixing AND and OR, leading to unintended logic.
  • Assuming OR short-circuits in all databases; some engines may still evaluate every operand.
  • Chaining many OR predicates on the same column instead of using IN or UNION, which can be faster.
  • Expecting NULL OR TRUE to return NULL instead of TRUE.

Related Topics

AND, NOT, XOR, WHERE, HAVING, CASE, IN, BETWEEN

First Introduced In

SQL-86 (ANSI X3.135-1986)

Frequently Asked Questions

What is SQL OR used for?

OR tests multiple conditions and returns TRUE when any single condition is TRUE, enabling flexible filtering in SELECT, UPDATE, DELETE, and JOIN clauses.

Does OR short-circuit in SQL?

Some engines evaluate operands left to right and stop on the first TRUE, but the SQL standard does not require short-circuiting. Rely on documentation for your database.

How can I avoid performance issues with many ORs?

Replace long OR chains on the same column with IN, UNION ALL, or a derived table to leverage indexes more effectively.

What is the difference between OR and XOR?

OR returns TRUE when any condition is TRUE. XOR (exclusive OR) returns TRUE only when exactly one condition is TRUE; support for XOR is vendor-specific.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!