SQL Basics | DML: Data Manipulation Language | AND, OR
The previous page explained how to load data that meets a condition from a table with the WHERE clause. A condition can be simple, as in the previous example, or complex. A complex condition combines two or more simple conditions with AND or OR. A single SQL statement can use any number of simple conditions.
AND | OR syntax
The AND | OR statement is as follows.
SELECT "field_name"
FROM "table_name"
WHERE "condition"
{[AND|OR] "condition"}+;
{}+ means that the condition inside {} can occur one or more times. Here, it shows that adding an AND condition or adding an OR condition can occur one or more times. You can also use parentheses to indicate the priority order of conditions.
AND | OR example
Suppose you need to retrieve data with sales of $1,000 or more from the following table.
Suppose you need to retrieve all data from the Store_Information table where Sales is $1,000 or more, or where Sales is between $500 and $275.
store_information table
| store_name | sales | txn_date |
|---|---|---|
| Los Angeles | 1500 | Jan-05-2018 |
| San Diego | 250 | Jan-07-2018 |
| San Francisco | 300 | Jan-08-2018 |
| Boston | 700 | Jan-08-2018 |
Enter the following command.
SELECT store_name
FROM store_information
WHERE Sales > 1000
OR (sales < 500 AND Sales > 275);
The result is as follows.
| store_name |
|---|
| Los Angeles |
| San Francisco |