Search This Blog

SQL: Combining the AND and OR Conditions

 

SQL: Combining the AND and OR Conditions

This SQL tutorial explains how to use the AND condition and the OR condition together in a single query with syntax and examples.

Description

The SQL AND condition and OR condition can be combined to test for multiple conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

When combining these conditions, it is important to use parentheses so that the database knows what order to evaluate each condition. (Just like when you were learning the order of operations in Math class!)

Syntax

The syntax for the AND condition and OR condition together in SQL is:

WHERE condition1
AND condition2
...
OR condition_n;

Parameters or Arguments

condition1, condition2, ... condition_n
The conditions that are evaluated to determine if the records will be selected.

Note

  • The SQL AND & OR conditions allow you to test multiple conditions.
  • Don't forget the order of operation parentheses!

DDL/DML for Examples

If you want 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 the "AND" and "OR" Conditions with the SELECT Statement

Now, let's look at an example of how to use the AND condition and OR condition together in a SELECT statement.

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

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
400Kimberly-ClarkIrvingTexas
500Tyson FoodsSpringdaleArkansas
600SC JohnsonRacineWisconsin
700Dole Food CompanyWestlake VillageCalifornia
800Flowers FoodsThomasvilleGeorgia
900Electronic ArtsRedwood CityCalifornia

Enter the following SQL statement:


SELECT *
FROM suppliers
WHERE (state = 'California' AND supplier_id <> 900)
OR (supplier_id = 100);

There will be 4 records selected. These are the results that you should see:

supplier_idsupplier_namecitystate
100MicrosoftRedmondWashington
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
700Dole Food CompanyWestlake VillageCalifornia

This example would return all suppliers that are in the state of California but do not have a supplier_id equal to 900. The query will also return all suppliers whose supplier_id is equal to 100. The parentheses determine the order that the AND and OR conditions are evaluated. Just like you learned in the order of operations in Math class!

Example - Using the "AND" and "OR" Conditions with the UPDATE Statement

Next, let's look at how to use the AND and OR conditions in an UPDATE statement.

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

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerekgoogle.com

Now let's demonstrate how to use the AND and OR conditions to update records in a table. Enter the following UPDATE statement:


UPDATE customers
SET favorite_website = 'google.com'
WHERE customer_id = 6000
OR (customer_id > 7000 AND last_name <> 'Johnson');

There will be 2 records updated. Select the data from the customers table again:

SELECT * FROM customers;

These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
4000JacksonJoegoogle.com
5000SmithJanedigminecraft.com
6000FergusonSamanthagoogle.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigegoogle.com
9000JohnsonDerekgoogle.com

This example would update all favorite_website values in the customers table to 'google.com' where the customer_id is equal to 6000 as well as those records where the customer_id is greater than 7000 and the last_name is not equal to 'Johnson'. As you can see, the favorite_website value in the third row and fifth row have been updated.

Example - Using the "AND" and "OR" Conditions with the DELETE Statement

Next, let's look at how to combine the AND and OR conditions to delete records using the DELETE statement.

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

product_idproduct_namecategory_id
1Pear50
2Banana50
3Orange50
4Apple50
5Bread75
6Sliced Ham25
7KleenexNULL

Enter the following DELETE statement:


DELETE FROM products
WHERE category_id = 25
OR (product_id < 4 AND product_name <> 'Banana');

There will be 3 records deleted. Select the data from the products table again:

SELECT * FROM products;

These are the results that you should see:

product_idproduct_namecategory_id
2Banana50
4Apple50
5Bread75
7KleenexNULL

This example would delete all records from the products table where the category_id is equal to 25. It would also delete all records from the products table where the product_id is less than 4 and the product_name is not equal to 'Banana'.

No comments:

Post a Comment

Using SQL*Plus to Unlock Accounts and Reset Passwords

  Using SQL*Plus to Unlock Accounts and Reset Passwords Use this SQL*Plus procedure to unlock and reset user account passwords. Log in as th...