SQL Server: OR Condition

OR condition

This SQL Server tutorial explains how to use the OR condition in SQL Server (Transact-SQL) with syntax and examples.

Description

The SQL Server (Transact-SQL) OR condition is used to test multiple conditions where records are returned when any one of the conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the OR condition in SQL Server (Transact-SQL) is:

WHERE condition1
OR condition2
...
OR condition_n;

Parameters or Arguments

condition1, condition2, … condition_n

Any of the conditions that must be met for the records to be selected.

Note

  • The SQL Server OR condition allows you to test 2 or more conditions.
  • The SQL Server OR condition requires that any of the conditions (ie: condition1condition2condition_n) must be met for the record to be included in the result set.

Example – With SELECT Statement

The first SQL Server OR condition example that we’ll take a look at involves a SELECT statement with 2 conditions.

For example:

SELECT *
FROM employees
WHERE first_name = 'Sarah'
OR last_name = 'Johnson';

This SQL Server OR example would return all employees whose first_name is ‘Sarah’ or last_name is ‘Johnson’. Because the * is used in the SELECT statement, all fields from the employees table would appear in the result set.

Example – With SELECT Statement (3 conditions)

The next SQL Server OR example looks at a SELECT statement with 3 conditions. If any of these conditions is met, the record will be included in the result set.

For example:

SELECT last_name, first_name
FROM employees
WHERE last_name = 'Anderson'
OR state = 'California'
OR employee_id = 50;

This SQL Server OR condition example would return all last_name and first_name values from the employees table where the last_name is ‘Anderson’ or the state is ‘California’ or the employee_id is equal to 50.

Example – With INSERT Statement

The SQL Server OR condition can be used in the INSERT statement.

For example:

INSERT INTO contacts
(contact_id, last_name, first_name)
SELECT employee_id, last_name, first_name
FROM employees
WHERE last_name = 'Smith'
OR employee_id < 10;

This SQL Server OR example would insert into the contacts table, all employee_idlast_name, and first_name records from the employees table where the last_name is ‘Smith’ or the employee_id is less than 10.

Example – With UPDATE Statement

The SQL Server OR condition can be used in the UPDATE statement.

For example:

UPDATE employees
SET state = 'Florida'
WHERE employee_id < 1000
OR city = 'Miami';

This SQL Server OR condition example would update all state values in the employees table to ‘Florida’ where the employee_id is less than 1000 or the city is ‘Miami’.

Example – With DELETE Statement

The SQL Server OR condition can be used in the DELETE statement.

For example:

DELETE FROM employees
WHERE first_name = 'Joanne'
OR first_name = 'Darlene';

This SQL Server OR condition example would delete all employees from the employees table whose first_name is either ‘Joanne’ or ‘Darlene’.

This Post Has One Comment

Leave a Reply