OR Operator

SQL · Filtering Rows

Theory

OR combines two conditions and is TRUE when at least one of them is TRUE. WHERE dept = 'IT' OR dept = 'HR' keeps employees from either department. Each extra OR can only widen the result, which is the opposite of AND.

OR has lower precedence than AND, so WHERE dept = 'HR' OR dept = 'IT' AND salary > 80000 means HR employees plus high-paid IT employees, which may not be what you meant. Write (dept = 'HR' OR dept = 'IT') AND salary > 80000 to apply the salary rule to both. With NULLs, TRUE OR UNKNOWN is TRUE, but FALSE OR UNKNOWN is UNKNOWN and the row is dropped. Several ORs on the same column are clearer as IN ('IT', 'HR').

Why we use it

Many questions accept alternatives: either of two cities, any of several statuses. OR lets one query match rows meeting any of the rules instead of running separate queries and combining them.

When to use it

Use OR when any one condition is enough. For many values of the same column, IN is shorter and easier to read. Wrap OR groups in parentheses whenever AND is also present.

How it works

  1. Read a row
  2. Evaluate the first condition
  3. Is it TRUE? Yes: Keep the row (one TRUE is enough). No: Evaluate the next condition.
  4. Any remaining condition TRUE? Yes: Keep the row. No: Discard the row.
  5. Move to the next row

Syntax

SELECT columns FROM table
WHERE cond1 OR cond2;

Example

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

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic