Hit enter after type your search item

What is SQL OR operator?

The “OR” is a logical operator in SQL that is used to combine two or more conditions in the WHERE clause. The WHERE clause with OR operator can be used with SELECT, UPDATE, INSERT and DELETE statements. For example:

WHERE ID = 1 OR ID = 15 OR ID = 20;

As using multiple conditions with OR operator, it returns TRUE if any of the conditions are TRUE. So, if OR operator is used with the SELECT statement, it returns records if any of the conditions are TRUE.

In the case of AND logical operator, all the conditions have to evaluate as TRUE, otherwise, it returns FALSE.

If you are using ‘OR’ and ‘AND’ operators in a single query then OR is evaluated after AND operator unless parenthesis is used. I will show you this and other examples of OR operator in the next section.

A basic example of using OR operator

In the first example of OR operator, I am using the sto_employees table. In the SELECT statement with WHERE clause, I used the ID column with OR operator for multiple conditions as follows:

Query:


SQL IN operator

From the recordset, this is evident that although the id=20 is not found. The query returned records for the IDs that are found in the table. If we used AND operator in above query; none of the records should have returned. That is:


The result:

SQL IN AND

The result is an empty recordset.

Using different columns example

In this query, I used different columns for defining the condition in the WHERE clause and combined those by OR operator.

The emp_name is, obviously, a text field while the emp_salary is a numeric field:


The resultset:

SQL IN text

The example of using AND / OR together

The following query shows using the AND & OR operators together. For seeing the difference, I am using almost the same queries except for the order of columns in the WHERE clause is changed.

See the queries and resultsets:


SQL IN and AND

Using parenthesis for changing the order of evaluation

For changing the order of evaluation of OR & “AND” operator, you may enclose the OR in parenthesis. The following query explains how it works.

The query is to fetch the records of employees whom salary must be greater than 5500 and employee name must be Mike or Johny. See how it worked:


SQL IN and order

The example of using IN operator with the DELETE command

The following query shows using the IN SQL operator with the DELETE command. As mentioned earlier, you may use IN operator with SELECT, UPDATE, INSERT and DELETE commands for retrieving or affecting only specific rows with multiple conditions.

This query removes records of employees whom id is equal to 11 or 9:


SQL IN DELETE

The second table graphic shows the table with removed rows.

Using IN operator with HAVING clause

The HAVING clause acts like the WHERE clause, however, this is used for filtering the aggregated results in the SELECT statement.

For combining multiple conditions with HAVING clause, you may also use IN operator just like the WHERE clause.

The following query shows how; by enquiring the sto_emp_salary_paid table that keeps the records of salary paid to employees every month. We get the total amount paid by using the GROUP BY clause and applied a filter by HAVING clause with IN operator:


You may learn more about the HAVING clause in its tutorial.
This div height required for enabling the sticky sidebar