Versions available for this page: CUBRID 8.2.1 | CUBRID 8.3.0 | CUBRID 8.3.1 | CUBRID 8.4.0 | CUBRID 8.4.1 | CUBRID 8.4.3 | CUBRID 9.0.0 |
In a query, a column can be processed based on conditions. The WHERE clause specifies a search condition for data.
WHERE search_condition
search_condition :
comparison_predicate
between_predicate
in_predicate
like_predicate
null_predicate
quantified predicate
exists_predicate
set_predicate
The WHERE clause specifies a condition that determines the data to be retrieved by search_condition or a query. Only data for which the condition is true is retrieved for the query results. (NULL value is not retrieved for the query results because it is evaluated as unknown value.)
The logical operator AND or OR can be used for multiple conditions. If AND is specified, all conditions must be true. If OR is specified, only one needs to be true. If the keyword NOT is preceded by a condition, the meaning of the condition is reserved. The following table shows the order in which logical operators are evaluated.
|
Priority |
Operator |
Function |
|---|---|---|
|
1 |
() |
Logical expressions in parentheses are evaluated first. |
|
2 |
NOT |
Negates the result of the logical expression. |
|
3 |
AND |
All conditions in the logical expression must be true. |
|
4 |
OR |
One of the conditions in the logical expression must be true. |
The BETWEEN predicate allows you to select instances of a table that fall within a range of values or outside a range of values. The column evaluated by BETWEEN must be default data types which are single values.
expression [ NOT ] BETWEEN expression AND expression
The following is an example of selecting the name, gender and number of players of sports whose number of players is between 5 and 10. In this query, 20 rows are selected in the event table. The players values of selected rows fall between the specified range.
SELECT name, gender, players FROM event WHERE players BETWEEN 5 AND 10;
=== <Result of SELECT Command in Line 2> ===
name gender players
=========================================================
'Synchronized Team' 'W' 8
'Indoor' 'M' 6
'Indoor' 'W' 6
'Water Polo' 'M' 7
'Water Polo' 'W' 7
...
20 rows selected.
You can use NOT in the BETWEEN clause. If you use NOT BETWEEN, data outside the specified range are selected. The players value in the event table is exactly 5 or 10 is not included in the query results of the NOT BETWEEN clause.
SELECT name, gender, players FROM event WHERE players NOT BETWEEN 5 AND 10;
=== <Result of SELECT Command in Line 2> ===
name gender players
=========================================================
'Freestyle 48kg' 'W' 1
'Freestyle -48kg' 'M' 1
'Freestyle 52kg' 'M' 1
'Freestyle -52kg' 'M' 1
'Freestyle 54kg' 'M' 1
...
402 rows selected.
A comparison_predicate evaluates an expression by comparing it against another or subquery. The first expression is often a column name or path expression that is compared against a second expression. The following table shows comparison operators that can be used in comparison_predicate.
|
Comparison Operator |
Meaning |
|---|---|
|
= |
Equal to |
|
<> |
Not equal to |
|
> |
Greater than |
|
< |
Less than |
|
>= |
Greater than or equal to |
|
<= |
Less than or equal to |
Comparison against floating point and double precision data types should be regarded as approximate. Because of the conversion that occurs internal to your system, the value you enter may be altered during query manipulation and argument passing. This loss of precision affects the actual value stored of float point or double precision number, even though the output value may look like the desired value.
SELECT host_year, host_nation, host_city FROM olympic WHERE host_nation='United States of America';
=== <Result of SELECT Command in Line 2> ===
host_year host_nation host_city
=========================================================
1996 'United States of America' 'Atlanta '
1904 'United States of America' 'St. Louis'
1984 'United States of America' 'Los Angeles'
1932 'United States of America' 'Los Angeles'
4 rows selected.
The following is an example of selecting the name, gender and number of players of sports for which the number of players is greater than 4 and less than 10. The following query selects the name, gender and players columns from the event table. The comparison predicates and logical operators in the WHERE clause specify the range of the players value to retrieve.
SELECT sports, name, gender, players FROM event WHERE players > 4 AND players < 10;
=== <Result of SELECT Command in Line 1> ===
sports name gender players
===============================================================================
'Swimming' 'Synchronized Team' 'W' 8
'Volleyball' 'Indoor' 'M' 6
'Volleyball' 'Indoor' 'W' 6
'Water Polo' 'Water Polo' 'M' 7
'Water Polo' 'Water Polo' 'W' 7
...
20 rows selected.
The EXISTS predicate is used to determine whether the result of a subquery is an empty set.
EXISTS expression
The following is an example of selecting the names of the countries that participated in the 1988 Olympic Games, and their continents. The EXISTS predicate is used to determine whether a certain value exists in the results of a subquery. If any countries participated in the Olympic Games, the results of the subquery exist; data for 156 countries and their continents is outputted.
SELECT n.name, n.continent FROM nation n
WHERE EXISTS (SELECT * FROM participant p WHERE p.host_year=1988 AND p.nation_code=n.code);
=== <Result of SELECT Command in Line 2> ===
name continent
============================================
'Somalia' 'Africa'
'Sri Lanka' 'Asia'
'Sudan' 'Africa'
'Switzerland' 'Europe'
'Surinam' 'Americas'
...
156 rows selected.
The IN predicate compares a single value against a set, multiset or sequence set of values.
expression [ NOT ] IN expression
SELECT name, gender, players FROM event WHERE players IN (5, 6);
=== <Result of SELECT Command in Line 2> ===
name gender players
=========================================================
'Indoor' 'M' 6
'Indoor' 'W' 6
'Hockey' 'M' 6
'Hockey' 'W' 6
'Rhythmic Group Competition' 'W' 6
'Rhythmic Team' 'M' 6
'Rhythmic Team' 'W' 6
'Basketball' 'M' 5
'Basketball' 'W' 5
9 rows selected.
SELECT continent, name FROM nation WHERE code IN (SELECT nation_code FROM participant WHERE gold>0);
=== <Result of SELECT Command in Line 2> ===
continent name
============================================
'Africa' 'Algeria'
'Americas' 'Argentina'
'Europe' 'Armenia'
'Oceania' 'Australia'
'Europe' 'Austria'...
81 rows selected.
The LIKE predicate retrieves the data that matches a specified text pattern from a table. Comparison using the LIKE predicate is supported only with columns that have STRING or CHAR domains. The LIKE predicate does not support NCHAR or BIT domains.
expression [ NOT ] LIKE expression [ ESCAPE char]
The following is an example of selecting the nation codes and names of the countries that include 'K' in their nation codes. This query selects codes whose string pattern is '%K_' from the nation table. In this case, the first character can be any string, the second character must be 'K', and the last character must be any single characters. Here, the code consists of three characters. Therefore, as shown in the result, only codes that include 'K' as the second character are retrieved.
SELECT code, name FROM nation WHERE code LIKE '%K_';
=== <Result of SELECT Command in Line 1> ===
code name
============================================
'TKM' 'Turmenistan'
'UKR' 'Ukraine'
'SKN' 'Saint Kitts & Nevis'
'MKD' 'Former Yugoslav Republic of Macedonia'
'HKG' 'Hong Kong'
5 rows selected.
The NULL predicate determines whether columns in a table have a value specified as NULL. You can also select those columns that have values by using the NOT NULL keyword in the predicate.
expression IS [ NOT ] NULL
The following is an example of selecting the year, host country, host city, mascot and slogan of all Olympic Games that had a mascot. The following query selects specified columns from the olympic table for instances in which the mascot column values are not NULL.
SELECT host_year, host_nation, host_city, mascot, slogan FROM olympic WHERE mascot is NOT NULL;
=== <Result of SELECT Command in Line 1> ===
host_year host_nation host_city mascot slogan
=====================================================================================================
1988 'Korea' 'Seoul' 'HODORI' 'Harmony and progress'
1992 'Spain' 'Barcelona ' 'Cobi' 'Friends Forever '
1996 'United States of America' 'Atlanta ' 'Izzy' 'The Celebration of the Century '
2000 'Australia' 'Sydney ' 'Olly Syd Millie ' 'Share the Spirit'
1976 'Canada' 'Montreal' 'Amik' NULL
1972 'Germany' 'Munich' 'Waldi' NULL
1980 'U.S.S.R.' 'Moscow' 'Misha' NULL
1984 'United States of America' 'Los Angeles' 'Sam' 'Play part in History'
2004 'Greece' 'Athens ' 'Athena Phevos' 'Welcome Home'
...
25 rows selected.
The ALL quantifier compares a single value against every value returned by another expression. The SOME quantifier compares a single value against each value returned by another expression. The single value must compare at least one of the values returned by the expression. The keyword ANY can be used instead of keyoword SOME which returns the same query results.
expression comp_op ALL expression
expression comp_op SOME expression
SELECT nation_code, gold, silver FROM participant
WHERE gold > ALL (SELECT silver FROM participant) AND host_year=1992;
=== <Result of SELECT Command in Line 1> ===
nation_code gold silver
================================================
'EUN' 45 38
1 rows selected.
SELECT nation_code, gold, silver FROM participant
WHERE gold > SOME(SELECT silver FROM participant) AND host_year=1992;
=== <Result of SELECT Command in Line 1> ===
nation_code gold silver
================================================
'ITA' 6 5
'GBR' 5 3
'ROU' 4 6
'TCH' 4 2
'PRK' 4 0
...
37 rows selected.