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_id | supplier_name | city |
---|---|---|
100 | Microsoft | Redmond |
200 | RIM | Waterloo |
300 | Oracle | Redwood City |
400 | Mountain View | |
500 | Intel | Santa Clara |
600 | Samsung | Seoul |
Enter the following SQL statement:
Try ItSELECT * FROM suppliers WHERE supplier_name = 'Intel';
These are the results that you should see:
supplier_id | supplier_name | city |
---|---|---|
500 | Intel | Santa 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_id | last_name | first_name | favorite_website |
---|---|---|---|
4000 | Jackson | Joe | techonthenet.com |
5000 | Smith | Jane | digminecraft.com |
6000 | Ferguson | Samantha | bigactivities.com |
7000 | Reynolds | Allen | checkyourmath.com |
8000 | Anderson | Paige | NULL |
9000 | Johnson | Derek | techonthenet.com |
Now enter the following SQL statement:
Try ItSELECT * FROM customers WHERE favorite_website = 'techonthenet.com' AND customer_id > 6000;
These are the results that you should see:
customer_id | last_name | first_name | favorite_website |
---|---|---|---|
9000 | Johnson | Derek | techonthenet.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_id | product_name | category_id |
---|---|---|
1 | Pear | 50 |
2 | Banana | 50 |
3 | Orange | 50 |
4 | Apple | 50 |
5 | Bread | 75 |
6 | Sliced Ham | 25 |
7 | Kleenex | NULL |
Now enter the following SQL statement:
Try ItSELECT * FROM products WHERE product_name = 'Pear' OR product_name = 'Apple';
These are the results that you should see:
product_id | product_name | category_id |
---|---|---|
1 | Pear | 50 |
4 | Apple | 50 |
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_id | product_name | category_id |
---|---|---|
1 | Pear | 50 |
2 | Banana | 50 |
3 | Orange | 50 |
4 | Apple | 50 |
5 | Bread | 75 |
6 | Sliced Ham | 25 |
7 | Kleenex | NULL |
Now enter the following SQL statement:
Try ItSELECT * FROM products WHERE (product_id > 3 AND category_id = 75) OR (product_name = 'Pear');
These are the results that you should see:
product_id | product_name | category_id |
---|---|---|
1 | Pear | 50 |
5 | Bread | 75 |
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