Search This Blog

SQL WHERE Clause

 

SQL: WHERE Clause

This SQL tutorial explains how to use the SQL WHERE clause with syntax and examples.

Description

The SQL WHERE clause is used to filter the results and apply conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

Syntax

The syntax for the WHERE clause in SQL is:

WHERE conditions;

Parameters or Arguments

conditions
The conditions that must be met for records to be selected.

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 - One Condition in the WHERE Clause

It is difficult to explain the syntax for the SQL WHERE clause, so let's start with an example that uses the WHERE clause to apply 1 condition.

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';

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

supplier_idsupplier_namecitystate
200GoogleMountain ViewCalifornia
300OracleRedwood CityCalifornia
700Dole Food CompanyWestlake VillageCalifornia
900Electronic ArtsRedwood CityCalifornia

In this example, we've used the SQL WHERE clause to filter our results from the suppliers table. The SQL statement above would return all rows from the suppliers table where the state is California. Because the * is used in the select, all fields from the suppliers table would appear in the result set.

Example - Two Conditions in the WHERE Clause (AND Condition)

You can use the AND condition in the WHERE clause to specify more than 1 condition that must be met for the record to be selected. Let's explore how to do this.

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 enter the following SQL statement:


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

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

customer_idlast_namefirst_namefavorite_website
9000JohnsonDerektechonthenet.com

This example uses the WHERE clause to define multiple conditions. In this case, this SQL statement uses the AND condition to return all customers whose favorite_website is techonthenet.com and whose customer_id is greater than 6000.

Example - Two Conditions in the WHERE Clause (OR Condition)

You can use the O                                                   R condition in the WHERE clause to test multiple conditions where the record is returned if any one of the conditions are met.

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

Now enter the following SQL statement:


SELECT *
FROM products
WHERE product_name = 'Pear'
OR product_name = 'Apple';

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

product_idproduct_namecategory_id
1Pear50
4Apple50

This example uses the WHERE clause to define multiple conditions, but instead of using the AND condition, it uses the OR condition. In this case, this SQL statement would return all records from the products table where the product_name is either Pear or Apple.

Example - Combining AND & OR conditions

You can also combine the AND condition with the OR condition to test more complex conditions.

Let's use the products table again for this example.

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

Now enter the following SQL statement:


SELECT *
FROM products
WHERE (product_id > 3 AND category_id = 75)
OR (product_name = 'Pear');

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

product_idproduct_namecategory_id
1Pear50
5Bread75

This example would return all products whose product_id is greater than 3 and category_id is 75 as well as all products whose product_name is Pear.

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!

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