How to use OR operator in SQL?
Syntax
The OR
is a conjunctive operator used to combine multiple conditions to narrow data in an SQL statement. OR
is typically used in a combination with WHERE
clause from the SELECT
, UPDATE
or DELETE
statements.
Here is the genenric syntax:
SELECT *
FROM table_name
WHERE condition_1 OR condition_2 ... OR condition_n
;
So the action will be initiated when at least one of the given conditions separated by the OR
operator is true.
Example
We have a table category
:
category_id | name | last_update |
---|---|---|
1 | Action | 2006-02-15 04:46:27 |
2 | Animation | 2006-02-15 04:46:27 |
3 | Children | 2006-02-15 04:46:27 |
4 | Classics | 2006-02-15 04:46:27 |
5 | Comedy | 2006-02-15 04:46:27 |
6 | Documentary | 2006-02-15 04:46:27 |
7 | Drama | 2006-02-15 04:46:27 |
8 | Family | 2006-02-15 04:46:27 |
9 | Foreign | 2006-02-15 04:46:27 |
10 | Games | 2006-02-15 04:46:27 |
11 | Horror | 2006-02-15 04:46:27 |
12 | Music | 2006-02-15 04:46:27 |
13 | New | 2006-02-15 04:46:27 |
14 | Sci-Fi | 2006-02-15 04:46:27 |
15 | Sports | 2006-02-15 04:46:27 |
16 | Travel | 2006-02-15 04:46:27 |
Now we want to view the details of the first 10 categories and category Sports:
SELECT *
FROM
category
WHERE
category_id <= 10
OR name LIKE '%sports%'
And it returns the results:
category_id | name | last_update |
---|---|---|
1 | Action | 2006-02-15 04:46:27 |
2 | Animation | 2006-02-15 04:46:27 |
3 | Children | 2006-02-15 04:46:27 |
4 | Classics | 2006-02-15 04:46:27 |
5 | Comedy | 2006-02-15 04:46:27 |
6 | Documentary | 2006-02-15 04:46:27 |
7 | Drama | 2006-02-15 04:46:27 |
8 | Family | 2006-02-15 04:46:27 |
9 | Foreign | 2006-02-15 04:46:27 |
10 | Games | 2006-02-15 04:46:27 |
15 | Sports | 2006-02-15 04:46:27 |
Need a good GUI tool for databases? TablePlus provides a native client that allows you to access and manage Oracle, MySQL, SQL Server, PostgreSQL and many other databases simultaneously using an intuitive and powerful graphical interface.
Not on Mac? Download TablePlus for Windows.
Need a quick edit on the go? Download for iOS