# How to use SQL OR

The SQL `OR` operator can optimise your search of records by limiting results to those that **match at least one of your conditions**. You can combine it with other logical operators like `AND` and `NOT` to speed up and improve complex queries.

## What is SQL `OR`?

SQL’s AND, [NOT](https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-not/) and `OR` are some of the most important SQL operators for searching for relevant information. Whereas `AND` finds data that satisfies two or more conditions and `NOT` defines criteria to be omitted from results, `OR` allows for more lenient searches. It allows you to define several search criteria, **at least one of which** must be fulfilled by results. The advantage is that you can get relevant results that don’t necessarily fulfill all the listed criteria.

Since SQL `OR` is used to formulate search criteria, it’s often used together with SELECT and WHERE. `SELECT` specifies the **records and columns in question** and `WHERE` specifies the **search criteria**. To make your search query even more precise, you can combine `OR` with other logical operators like `AND` and `NOT` or comparison operators like `=`.

### What’s the difference between SQL `OR`, `AND` and `NOT`?

Here are the differences between SQL `OR`, `AND` and `NOT`:

<table>
  <thead>
    <tr>
      <th>SQL `OR`</th>
      <th>SQL `AND`</th>
      <th>SQL `NOT`</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Filters records that satisfy at least one of the conditions</td>
      <td>Filters data that satisfy all of the conditions</td>
      <td>Omits information that does not satisfy a condition</td>
    </tr>
    <tr>
      <td>Expands the search with different information</td>
      <td>Narrows the search to a combination of conditions, both of which are met</td>
      <td>Omits irrelevant information using criteria for exclusion</td>
    </tr>
  </tbody>
</table>

Tip No background in [SQL](https://www.ionos.com/en-ie/digitalguide/server/know-how/what-is-sql/)? No problem! Our [SQL introduction with examples](https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-introduction-with-examples/) provides a crash course on the basics.

## What is the syntax of SQL `OR`?

The basic syntax of `OR` looks as follows:

```sql
SELECT  *
FROM  Table
WHERE  condition1  OR  condition2  OR …
```

It uses the following parameters:

- `SELECT`: Selects the columns you want to retrieve data from, using primary and foreign keys. You can use an asterisk `*` to select all columns.
- `FROM`: Specifies which table you want to search in.
- `WHERE`: Defines search conditions using operators and other parameters.
- `OR`: Logical operator used with `WHERE` to define search criteria.

You can also combine `OR` with `NOT` and `AND` in your query to further narrow your search.

### SQL `OR` combined with `AND`

In this example, the WHERE clause filters for results where both condition 1 and at least one of conditions 2 or 3 are true.

```sql
SELECT  *
FROM  Table
WHERE  condition1 AND (condition2 OR condition3)
```

### SQL `OR` combined with SQL `NOT`

You can combine SQL `OR` with `NOT` to **exclude data** that fulfill at least one of the listed conditions.

```sql
SELECT  *
FROM  Table
WHERE  NOT  (condition1  OR  condition2)
```

## What can SQL `OR` be used for?

You can use the SQL `OR` operator to search complex datasets based on multiple criteria. Here are some use cases:

- Filtering customer, order and product data based on combined search criteria like product categories, product properties or order and complaint dates
- Searching for companies in certain industries or regions
- Filtering suspicious or invalid transactions using multiple conditions
- Searching for information on employees from a specific department or team
- Retrieving applicants that don’t have a degree or don’t have work experience

## Examples of SQL `OR`

There are countless **uses for the `OR` operator** when combined with `WHERE`, other SQL operators and other SQL functions. Below we’ll look at 3 examples, so you can get a better idea of how `OR` works.

### Searching for orders from certain categories

Let’s say you want to search a customer table for people who have ordered products from either or both of the categories ‘Electronics’ and ‘Hair’. The results will only include records that **meet at least one of the two conditions**. We’ll use the comparison operator `=` to specify the categories.

The syntax for that looks as follows:

```sql
SELECT  *
FROM  Customers
WHERE  (Category  =  'Electronics'  OR  Category =  'Hair')
```

### Filtering applicants

In this example, there’s a table called ‘Applicants’. We want to find the people in it that have a degree in electrical engineering or IT and have at least three years of work experience. To do this, we’ll combine `OR` and `AND`. The search looks as follows:

```sql
SELECT  *
FROM  Applicants
WHERE  (Degree  =  'Electrical engineering'  OR  Degree  =  'IT')
AND (Experience  >=  3)
```

### Searching based on region

In our final example, we want to search for customers from France or Germany in a table called ‘Customers’. The syntax for that looks as follows:

```sql
SELECT  *
FROM  Customers
WHERE  Country  =  'France'  OR   Country  =  'Germany'
```

## Are there alternatives to SQL `OR`?

The `OR` operator provides a simple and fast way to search based on **several conditions**. These SQL operators serve a similar purpose:

- `CASE`: SQL’s CASE expression can also be used with `WHERE`, `THEN` and `ELSE` clauses to check for conditions and narrow down complex analyses to relevant results. It’s evaluated using an ‘IF-THEN-ELSE’ statement and works its way through conditions from bottom to top. The analysis is stopped when one of the conditions is fulfilled.
- `IN`: The `IN` operator can be used in a `WHERE` clause to examine records. You can define values or strings using `IN` and then search for matches in tables or columns.
- `EXISTS`: SQL’s EXISTS can be used in a table to check whether at least one record from another table matches the first table. If there is at least one match, the result will be added to the target table. This operator is only useful if you’re working with two related tables.


This is a markdown version of: [https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-or/](https://www.ionos.com/en-ie/digitalguide/server/configuration/sql-or/) for AI/LLM consumption.