REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
SQL condition OR
SQL condition OR is used to check several conditions in SELECT, INSERT, UPDATE or DELETE. Any of the conditions must be met to select a record.
Syntax for SQL condition OR
WHERE cond1
OR cond2
…
OR cond_n;
where:
- cond1, cond2, … cond_n – Several conditions to be checked for each record. Any condition can be met to be included in the result set
Using OR condition with SELECT operator
Let’s look at an example that shows how to use an OR condition in a SELECT statement to check multiple conditions when any condition must be met for the records to be selected.
In this example, we have a table with the following data:
suppl_id | suppl_name | city_id | state_id |
---|---|---|---|
100 | Yandex | Moscow | Russian |
200 | Lansing | Michigan | |
300 | Oracle | Redwood City | California |
400 | Bing | Redmond | Washington |
500 | Yahoo | Sunnyvale | Washington |
600 | DuckDuckGo | Paoli | Pennsylvania |
700 | Qwant | Paris | France |
800 | Menlo Park | California | |
900 | Electronic Arts | San Francisco | California |
Now let’s demonstrate how to use the OR condition to test two conditions. Enter the following SELECT operator.
SELECT *
FROM suppls
WHERE city_id = 'Lansing'.
OR suppl_id = 100
ORDER BY suppl_name;
Two entries will be selected. Here are the results that you should get.
suppl_id | suppl_name | city_id | state_id |
---|---|---|---|
100 | Yandex | Moscow | Russian |
200 | Lansing | Michigan |
In this example, all suppliers that are in the city of Lansing or have a suppl_id equal to 100 are returned. Since * is used in the SELECT operator, all fields from the supplier table will appear in the result set.
Using OR condition with UPDATE operator
SQL condition OR can be used in UPDATE SQL statement to check several conditions.
In this example, we have a table with the following data:
custom_id | f_name | l_name | fav_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | bing.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | oracle.com |
8000 | Johnny | Depp | NULL |
9000 | Russell | Crowe | google.com |
Enter the following UPDATE request.
UPDATE customs
SET fav_website = 'Google.com'.
WHERE custom_id = 5000
OR l_name = 'Cruise'.
OR f_name = 'Johnny';
3 records will be updated. Select data from the customers table again.
SELECT *
FROM customs;
Here are the results you should get.
custom_id | f_name | l_name | fav_website |
---|---|---|---|
4000 | Justin | Bieber | google.com |
5000 | Selena | Gomez | google.com |
6000 | Mila | Kunis | yahoo.com |
7000 | Tom | Cruise | google.com |
8000 | Johnny | Depp | google.com |
9000 | Russell | Crowe | google.com |
In this example, all favourite_website values in the customers table will be updated to google.com, where custom_id is 5000 or l_name is ‘Cruise’ or f_name is ‘Johnny’. As you can see, fav_website field on the 2nd, 4th and 5th lines has been updated.
Using condition OR with DELETE operator
Now let’s look at how to use the OR condition in a DELETE statement to check any of the conditions that must be met before a record is deleted.
In this example we have a product table with the following data:
prod_id | prod_name | cat_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 operator.
DELETE FROM prods
WHERE prod_name = 'Pear'
OR prod_name = 'Apple'
OR cat_id = 25;
Three entries will be deleted. Select the data from the products table again.
SELECT *
FROM prods;
here are the results you should get.
prod_id | prod_name | cat_id |
---|---|---|
2 | Banana | 50 |
3 | Orange | 50 |
5 | Bread | 75 |
7 | Kleenex | NULL |
In this example, all entries from the products table, where prod_name is Pear, prod_name is Apple or cat_id =25, will be removed.
How to use the AND Condition with the OR Condition in SQL
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...