Back to home page

OSCL-LXR

 
 

    


0001 ---
0002 layout: global
0003 title: SELECT
0004 displayTitle: WHERE clause
0005 license: |
0006   Licensed to the Apache Software Foundation (ASF) under one or more
0007   contributor license agreements.  See the NOTICE file distributed with
0008   this work for additional information regarding copyright ownership.
0009   The ASF licenses this file to You under the Apache License, Version 2.0
0010   (the "License"); you may not use this file except in compliance with
0011   the License.  You may obtain a copy of the License at
0012 
0013      http://www.apache.org/licenses/LICENSE-2.0
0014 
0015   Unless required by applicable law or agreed to in writing, software
0016   distributed under the License is distributed on an "AS IS" BASIS,
0017   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0018   See the License for the specific language governing permissions and
0019   limitations under the License.
0020 ---
0021 
0022 ### Description
0023 
0024 The `WHERE` clause is used to limit the results of the `FROM`
0025 clause of a query or a subquery based on the specified condition.
0026 
0027 ### Syntax
0028 
0029 ```sql
0030 WHERE boolean_expression
0031 ```
0032 
0033 ### Parameters
0034 
0035 * **boolean_expression**
0036 
0037     Specifies any expression that evaluates to a result type `boolean`. Two or
0038     more expressions may be combined together using the logical
0039     operators ( `AND`, `OR` ).
0040 
0041 ### Examples
0042 
0043 ```sql
0044 CREATE TABLE person (id INT, name STRING, age INT);
0045 INSERT INTO person VALUES
0046     (100, 'John', 30),
0047     (200, 'Mary', NULL),
0048     (300, 'Mike', 80),
0049     (400, 'Dan',  50);
0050 
0051 -- Comparison operator in `WHERE` clause.
0052 SELECT * FROM person WHERE id > 200 ORDER BY id;
0053 +---+----+---+
0054 | id|name|age|
0055 +---+----+---+
0056 |300|Mike| 80|
0057 |400| Dan| 50|
0058 +---+----+---+
0059 
0060 -- Comparison and logical operators in `WHERE` clause.
0061 SELECT * FROM person WHERE id = 200 OR id = 300 ORDER BY id;
0062 +---+----+----+
0063 | id|name| age|
0064 +---+----+----+
0065 |200|Mary|null|
0066 |300|Mike|  80|
0067 +---+----+----+
0068 
0069 -- IS NULL expression in `WHERE` clause.
0070 SELECT * FROM person WHERE id > 300 OR age IS NULL ORDER BY id;
0071 +---+----+----+
0072 | id|name| age|
0073 +---+----+----+
0074 |200|Mary|null|
0075 |400| Dan|  50|
0076 +---+----+----+
0077 
0078 -- Function expression in `WHERE` clause.
0079 SELECT * FROM person WHERE length(name) > 3 ORDER BY id;
0080 +---+----+----+
0081 | id|name| age|
0082 +---+----+----+
0083 |100|John|  30|
0084 |200|Mary|null|
0085 |300|Mike|  80|
0086 +---+----+----+
0087 
0088 -- `BETWEEN` expression in `WHERE` clause.
0089 SELECT * FROM person WHERE id BETWEEN 200 AND 300 ORDER BY id;
0090 +---+----+----+
0091 | id|name| age|
0092 +---+----+----+
0093 |200|Mary|null|
0094 |300|Mike|  80|
0095 +---+----+----+
0096 
0097 -- Scalar Subquery in `WHERE` clause.
0098 SELECT * FROM person WHERE age > (SELECT avg(age) FROM person);
0099 +---+----+---+
0100 | id|name|age|
0101 +---+----+---+
0102 |300|Mike| 80|
0103 +---+----+---+
0104 
0105 -- Correlated Subquery in `WHERE` clause.
0106 SELECT * FROM person AS parent
0107     WHERE EXISTS (
0108         SELECT 1 FROM person AS child
0109         WHERE parent.id = child.id AND child.age IS NULL
0110     );
0111 +---+----+----+
0112 |id |name|age |
0113 +---+----+----+
0114 |200|Mary|null|
0115 +---+----+----+
0116 ```
0117 
0118 ### Related Statements
0119 
0120 * [SELECT Main](sql-ref-syntax-qry-select.html)
0121 * [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html)
0122 * [HAVING Clause](sql-ref-syntax-qry-select-having.html)
0123 * [ORDER BY Clause](sql-ref-syntax-qry-select-orderby.html)
0124 * [SORT BY Clause](sql-ref-syntax-qry-select-sortby.html)
0125 * [CLUSTER BY Clause](sql-ref-syntax-qry-select-clusterby.html)
0126 * [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html)
0127 * [LIMIT Clause](sql-ref-syntax-qry-select-limit.html)