In this post explains how to use the OR condition in SQL Server (Transact-SQL) with syntax and examples.
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.
The syntax for the OR condition in SQL Server (Transact-SQL) is:
Any of the conditions that must be met for the records to be selected.
The first SQL Server OR condition example that we'll take a look at involves a SELECT statement with 2 conditions.
For example:
This SQL Server OR example would return all employees whose first_name is 'Julie' 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.
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:
This SQL Server OR condition example would return all last_name and first_name values from the employees table where the last_name is 'Mark' or the state is 'California' or the employee_id is equal to 50.
The SQL Server OR condition can be used in the INSERT statement.
For example:
This SQL Server OR example would insert into the contacts table, all employee_id, last_name, and first_name records from the employees table where the last_name is 'Smith' or the employee_id is less than 10.
The SQL Server OR condition can be used in the UPDATE statement.
For example:
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'.
The SQL Server OR condition can be used in the DELETE statement.
For example:
This SQL Server OR condition example would delete all employees from the employees table whose first_name is either 'Joanne' or 'Darlene'.