Thursday, April 28, 2016

WHERE CLAUSE


This SQL tutorial explains how to use the SQL WHERE clause with syntax and examples. Click the "Try It" button next to an example to test the WHERE clause for yourself in our SQL Editor.

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 SQL WHERE Clause 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!

EXAMPLE - WITH SINGLE CONDITION

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_namecity
100MicrosoftRedmond
200RIMWaterloo
300OracleRedwood City
400GoogleMountain View
500IntelSanta Clara
600SamsungSeoul
Enter the following SQL statement:
Try It
SELECT *
FROM suppliers
WHERE supplier_name = 'Intel';
These are the results that you should see:
supplier_idsupplier_namecity
500IntelSanta Clara
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 supplier_name is Intel. Because the * is used in the select, all fields from the suppliers table would appear in the result set.

EXAMPLE - USING 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
4000JacksonJoetechonthenet.com
5000SmithJanedigminecraft.com
6000FergusonSamanthabigactivities.com
7000ReynoldsAllencheckyourmath.com
8000AndersonPaigeNULL
9000JohnsonDerektechonthenet.com
Now enter the following SQL statement:
Try It
SELECT *
FROM customers
WHERE favorite_website = 'techonthenet.com'
AND customer_id > 6000;
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 - USING OR CONDITION

You can use the OR 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:
Try It
SELECT *
FROM products
WHERE product_name = 'Pear'
OR product_name = 'Apple';
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:
Try It
SELECT *
FROM products
WHERE (product_id > 3 AND category_id = 75)
OR (product_name = 'Pear');
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 whoseproduct_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