The WHERE clause is used to select a subset of rows from the data source based on a specified condition. Only the rows matching the condition are returned.
The WHERE clause is used to filter rows.
It is used to extract only those rows that fulfill a specified criterion.
123
SELECT PlateNumber, Brand, Color
FROM Car
WHERE Brand = 'BMW';
| PLATENUMBER | BRAND | COLOR |
|---|
| HD-JA 1972 | BMW | blue |
| HD-MT 507 | BMW | black |
| HD-MM 208 | BMW | green |
You can reference a column in the WHERE clause that is not included in the projection list.
123
SELECT Brand, Color
FROM Car
WHERE HP >= 170;
| BRAND | COLOR |
|---|
| BMW | blue |
| Mercedes | black |
| Audi | yellow |
| BMW | green |
| Mercedes | white |
| Audi | orange |
| Audi | green |
Condition expressions in SQL are very similar to condition expressions in programming languages such as ABAP, Java, and so on. The columns of the data source take over the roles of variables used in these languages.
A condition can refer to string-like or to numeric columns. The columns used in the condition do not have to be included in the projection list.
Atomic conditions can be combined to complex and nested conditions using the usual logical operators AND, OR, and NOT. Parentheses can be used to control the order of operations.
A condition can make use of functions, including nested functions.
An atomic condition typically contains a comparison operator. The usual comparison operators =, <>, <, <=, >, and >= are supported. However, SQL supports a few additional comparison operators.
Comparison Operator IS NULL
You can check for the presence of NULL values in the WHERE clause:
123
SELECT CarID, Brand, Color
FROM Car
WHERE PlateNumber IS NULL;
| CARID | BRAND | COLOR |
|---|
| F16 | Opel | green |
| F20 | Audi | green |
Comparison Operator IS NOT NULL
Likewise, you can check for the absence of NULL values in the WHERE clause:
123
SELECT CarID, Brand, Color
FROM Car
WHERE PlateNumber IS NOT NULL;
| CARID | BRAND | COLOR |
|---|
| F01 | Fiat | red |
| F02 | VW | black |
| F03 | BMW | blue |
| F04 | Mercedes | white |
| F05 | Mercedes | black |
| F06 | Audi | yellow |
| F07 | Audi | blue |
| F08 | VW | black |
| F09 | Skoda | red |
| F10 | BMW | black |
| F11 | BMW | green |
| F12 | Skoda | red |
| F13 | Renault | red |
| F14 | Mercedes | white |
| F15 | Skoda | black |
| F17 | Audi | orange |
| F18 | Renault | red |
| F19 | VW | black |
Comparison Operator IN
You can check if values are included in a value list.
123
SELECT Brand, Color
FROM Car
WHERE Color IN ('red', 'blue', 'orange');
| BRAND | COLOR |
|---|
| Fiat | red |
| BMW | blue |
| Audi | blue |
| Skoda | red |
| Skoda | red |
| Renault | red |
| Audi | orange |
| Renault | red |
Comparison Operator BETWEEN and AND
You can check if values are included in an interval.
123
SELECT PlateNumber, Brand, Color, HP
FROM Car
WHERE HP BETWEEN 140 AND 170;
Note
The lower bound of a BETWEEN comparison must come before the upper bound. The results are inclusive of the endpoints.
| PLATENUMBER | BRAND | COLOR | HP |
|---|
| HD-MM 3206 | Mercedes | black | 170 |
| HD-IK 1002 | VW | black | 160 |
| HD-MT 507 | BMW | black | 140 |
| HD-MM 1977 | Mercedes | white | 170 |
Comparison Operator LIKE
You can use search patterns in the WHERE clause.
123
SELECT PlateNumber, Brand, Color, HP
FROM Car
WHERE PlateNumber LIKE '%MM%';
| PLATENUMBER | BRAND | COLOR | HP |
|---|
| HD-MM 3206 | Mercedes | black | 170 |
| HD-MM 208 | BMW | green | 184 |
| HD-MM 1977 | Mercedes | white | 170 |
Wildcards in the LIKE Predicate
Wildcard searching is done with the LIKE predicate, not the equals (=) predicate. The wildcard character % (percentage sign) represents any string containing zero, one, or multiple characters. The wildcard character _ (underscore) represents any single character.
| WHERE MyColumn LIKE 'M%' | String starting with "M" |
| WHERE MyColumn LIKE 'M _' | String of two characters starting with "M" |
| WHERE MyColumn LIKE '%M' | String ending with "M" |
| WHERE MyColumn LIKE '%M%' | String containing "M" |
| WHERE MyColumn LIKE '_ _ _' | String of three characters |
| WHERE MyColumn LIKE '_ _ _ _T_M%' | String with "T" in the fifth and "M" in the seventh position |
Escaping Wildcards in the LIKE Predicate
If you want to search for the percentage sign (%) or underscore (_) itself, you have to place an ESCAPE character in front. You can choose the ESCAPE character (with some restrictions).
| LIKE '$%%' ESCAPE '$' | String starting with a percentage sign |
| LIKE '$_ _' ESCAPE '$' | String of two characters starting with an underscore |
| LIKE '%$%' ESCAPE '$' | String ending with a percentage sign |
| LIKE '%$%%' ESCAPE '$' | String containing a percentage sign |
| LIKE '%$_$%%' ESCAPE '$' | String containing an underscore followed by a percentage sign |
| LIKE '_ _ _ _ $%_ $ _%' ESCAPE '$' | "%" in the fifth and "_" in the seventh position |
A Complex WHERE Clause
- The license plate started with "HD" for Heidelberg.
- I'm sure I saw an "M".
- I clearly remember the digits "2" and "6".
- The "2" was definitely left of the "6".
- Between the "2" and the "6" was exactly one digit.
- It was neither Skoda, nor a VW.
- The car was blue or green.
123456
SELECT *
FROM Car
WHERE PlateNumber LIKE 'HD-%M% %2_6%'
AND Brand <> 'Skoda'
AND Brand <> 'VW'
AND (Color = 'blue' OR Color = 'green');
| CARID | PLATENUMBER | BRAND | COLOR | HP | OWNER |
|---|
| F07 | HD-ML 3206 | Audi | blue | 116 | H03 |
Operator Precedence
The operators have the precedence indicated by the table below:
| Precedence | Operator | Explanation |
|---|
| Highest | () | parentheses |
| | ‒ | unary minus |
| | *, / | multiplication, division |
| | +, ‒ | addition, subtraction |
| | =, <, <=, >, >=, <>, IS NULL, LIKE, BETWEEN | comparison |
| | NOT | logical negation |
| | AND | conjunction |
| Lowest | OR | disjunction |