Search This Blog

SQL: AND Condition

 

SQL: AND Condition

This SQL tutorial explains how to use the SQL AND condition with syntax and examples.

Description

The SQL AND condition (also known as the AND operator) is used to test for two or more conditions in a SELECT, INSERT, UPDATE, or DELETE statement. All conditions must be met for a record to be selected.

Syntax

The syntax for the AND condition in SQL is:

WHERE condition1
AND condition2
...
AND condition_n;

Parameters or Arguments

condition1, condition2, ... condition_n
Multiple conditions that will be tested for each record. All conditions must be met to be included in the result set.

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 "AND" Condition with the SELECT Statement

Let's look at an example that shows how to use the AND condition in a SELECT statement to test for 2 conditions that must be met for the records to be selected.

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 condition. Enter the following SELECT statement:


SELECT *
FROM customers
WHERE favorite_website = 'google.com'
AND customer_id > 6000
ORDER BY last_name;

There will be 1 record selected. These are the results that you should see:

customer_idlast_namefirst_namefavorite_website
9000JohnsonDerekgoogle.com

This example would return all customers whose favorite_website is google.com and the customer_id is greater than 6000. Because the * is used in the SQL SELECT statement, all fields from the customers table would appear in the result set.

Example - Using "AND" Condition with the UPDATE Statement

Now, let's look at an example of how to use the AND condition in an UPDATE statement. This will test for multiple conditions to be met before a record is updated.

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 UPDATE statement:


UPDATE suppliers
SET supplier_name = 'TBD'
WHERE city = 'Redwood City'
AND supplier_id <> 900;

There will be 1 record updated. Select the data from the suppliers table again:

SELECT * FROM suppliers;

These are the results that you should see:

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

This example would update all supplier_name values in the suppliers table to TBD where the city was Redwood City and the supplier_id was not equal to 900. As you can see, the supplier_name in the third row has been updated.

Example - Using "AND" Condition with the DELETE Statement

Next, let's see how to use the AND condition in the DELETE statement to test for 2 conditions to be met before a record is deleted.

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 = 50
AND product_name <> 'Pear';

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
1Pear50
5Bread75
6Sliced Ham25
7KleenexNULL

This example would delete all records from the products table whose category_id is 50 and whose product_name is not Pear.

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...