Basic about OR Condition – using and example

This SQL tutorial explains how to use the SQL OR circumstance with syntax and examples.

Description

The SQL OR circumstance is used to check multiple prerequisites in a SELECT, INSERT, UPDATE, or DELETE statement. Any one of the stipulations must be met for a document to be selected.

Syntax

The syntax for the OR condition in SQL is:

WHERE condition1
OR condition2
...
OR condition_n;

Parameters or Arguments

condition1, condition2, … condition_n Multiple conditions that will be examined for each record. Any situation can be met to be protected in the result set.

DDL/DML for Examples

If you choose to follow along with this tutorial, get the DDL to create the tables and the DML to populate the data. Then try the examples in your own database!

Get DDL/DML

Example – Using “OR” Condition with the SELECT Statement

Let’s seem to be at an instance that suggests how to use the OR circumstance in a SELECT assertion to test for more than one conditions the place any situation must be met for the documents to be selected.

In this example, we have a desk called suppliers with the following data:

supplier_id supplier_name metropolis state 100 Microsoft Redmond Washington 200 Google Mountain View California 300 Oracle Redwood City California 400 Kimberly-Clark Irving Texas 500 Tyson Foods Springdale Arkansas 600 SC Johnson Racine Wisconsin 700 Dole Food Company Westlake Village California 800 Flowers Foods Thomasville Georgia 900 Electronic Arts Redwood City California

Now let’s exhibit how to use the OR situation to take a look at for 2 conditions. Enter the following SELECT statement:

Try It

SELECT *
FROM suppliers
WHERE city = 'Mountain View'
OR supplier_id = 100
ORDER BY supplier_name;

There will be two files selected. These are the results that you have to see:

supplier_id supplier_name city state 200 Google Mountain View California 100 Microsoft Redmond Washington

This instance would return all suppliers that are in the metropolis of Mountain View or have a supplier_id that is equal to a hundred Because the * is used in the SELECT statement, all fields from the suppliers desk would appear in the result set.

Example – Using “OR” Condition with the UPDATE Statement

The OR condition can be used in the SQL UPDATE announcement to test for a couple of conditions.

In this example, we have a desk called customers with the following data:

customer_id last_name first_name favorite_website 4000 Jackson Joe techonthenet.com 5000 Smith Jane digminecraft.com 6000 Ferguson Samantha bigactivities.com 7000 Reynolds Allen checkyourmath.com 8000 Anderson Paige NULL 9000 Johnson Derek techonthenet.com

Enter the following UPDATE statement:

Try It

UPDATE customers
SET favorite_website = 'techonthenet.com'
WHERE customer_id = 5000
OR last_name = 'Reynolds'
OR first_name = 'Paige';

There will be three files updated. Select the facts from the customers desk again:

SELECT * FROM customers;

These are the results that you should see:

customer_id last_name first_name favorite_website 4000 Jackson Joe techonthenet.com 5000 Smith Jane techonthenet.com 6000 Ferguson Samantha bigactivities.com 7000 Reynolds Allen techonthenet.com 8000 Anderson Paige techonthenet.com 9000 Johnson Derek techonthenet.com

This instance would replace all favorite_website values in the customers desk to techonthenet.com the place the customer_id is 5000 or the last_name is Reynolds or the first_name is Paige. As you can see, the favorite_website subject in the 2nd, 4th and 5th rows are updated.

Example – Using “OR” Condition with the DELETE Statement

Next, let’s see how to use the OR situation in the DELETE assertion to check for any of the prerequisites to be met before a record is deleted.

In this example, we have a table called products with the following data:

product_id product_name category_id 1 Pear 50 2 Banana 50 3 Orange 50 4 Apple 50 5 Bread 75 6 Sliced Ham 25 7 Kleenex NULL

Enter the following DELETE statement:

Try It

DELETE FROM products
WHERE product_name = 'Pear'
OR product_name = 'Apple'
OR category_id = 25;

There will be three archives deleted. Select the facts from the merchandise desk again:

SELECT * FROM products;

These are the results that you should see:

product_id product_name category_id 2 Banana 50 3 Orange 50 5 Bread 75 7 Kleenex NULL

This condition instance would delete all documents from the products table where the product_name is Pear, the product_name is Apple or the category_id = 25.