SQL > SQL Commands > WHERE Clause
We can use the WHERE clause to filter the result set based on certain conditions. The syntax for using WHERE in the SELECT statement is as follows:
SELECT "column_name"
FROM "table_name"
WHERE "condition";
FROM "table_name"
WHERE "condition";
"Condition" can include a single comparison clause (called simple condition) or multiple comparison clauses combined together using AND or OR operators (compound condition).
Example 1: WHERE Clause With Simple Condition
To select all stores with sales above $1,000 in Table Store_Information,
Table Store_Information
Store_Name | Sales | Txn_Date |
Los Angeles | 1500 | Jan-05-1999 |
San Diego | 250 | Jan-07-1999 |
Los Angeles | 300 | Jan-08-1999 |
Boston | 700 | Jan-08-1999 |
we key in,
SELECT Store_Name
FROM Store_Information
WHERE Sales > 1000;
FROM Store_Information
WHERE Sales > 1000;
Result:
Store_Name |
Los Angeles |
Example 2: WHERE Clause With OR Operator
To view all data with sales greater than $1,000 or with transaction date of 'Jan-08-1999', we use the following SQL,
SELECT *
FROM Store_Information
WHERE Sales > 1000 OR Txn_Date = 'Jan-08-1999';
FROM Store_Information
WHERE Sales > 1000 OR Txn_Date = 'Jan-08-1999';
Result:
Store_Name | Sales | Txn_Date |
Los Angeles | 1500 | Jan-05-1999 |
Los Angeles | 300 | Jan-08-1999 |
Boston | 700 | Jan-08-1999 |
No comments:
Post a Comment