| --- |
| layout: global |
| title: OFFSET Clause |
| displayTitle: OFFSET Clause |
| license: | |
| Licensed to the Apache Software Foundation (ASF) under one or more |
| contributor license agreements. See the NOTICE file distributed with |
| this work for additional information regarding copyright ownership. |
| The ASF licenses this file to You under the Apache License, Version 2.0 |
| (the "License"); you may not use this file except in compliance with |
| the License. You may obtain a copy of the License at |
| |
| http://www.apache.org/licenses/LICENSE-2.0 |
| |
| Unless required by applicable law or agreed to in writing, software |
| distributed under the License is distributed on an "AS IS" BASIS, |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| See the License for the specific language governing permissions and |
| limitations under the License. |
| --- |
| |
| ### Description |
| |
| The `OFFSET` clause is used to specify the number of rows to skip before beginning to return rows |
| returned by the [SELECT](sql-ref-syntax-qry-select.html) statement. In general, this clause |
| is used in conjunction with [ORDER BY](sql-ref-syntax-qry-select-orderby.html) to |
| ensure that the results are deterministic. |
| |
| ### Syntax |
| |
| ```sql |
| OFFSET integer_expression |
| ``` |
| |
| ### Parameters |
| |
| * **integer_expression** |
| |
| Specifies a foldable expression that returns an integer. |
| |
| ### Examples |
| |
| ```sql |
| CREATE TABLE person (name STRING, age INT); |
| INSERT INTO person VALUES |
| ('Zen Hui', 25), |
| ('Anil B', 18), |
| ('Shone S', 16), |
| ('Mike A', 25), |
| ('John A', 18), |
| ('Jack N', 16); |
| |
| -- Skip the first two rows. |
| SELECT name, age FROM person ORDER BY name OFFSET 2; |
| +-------+---+ |
| | name|age| |
| +-------+---+ |
| | John A| 18| |
| | Mike A| 25| |
| |Shone S| 16| |
| |Zen Hui| 25| |
| +-------+---+ |
| |
| -- Skip the first two rows and returns the next three rows. |
| SELECT name, age FROM person ORDER BY name LIMIT 3 OFFSET 2; |
| +-------+---+ |
| | name|age| |
| +-------+---+ |
| | John A| 18| |
| | Mike A| 25| |
| |Shone S| 16| |
| +-------+---+ |
| |
| -- A function expression as an input to OFFSET. |
| SELECT name, age FROM person ORDER BY name OFFSET length('SPARK'); |
| +-------+---+ |
| | name|age| |
| +-------+---+ |
| |Zen Hui| 25| |
| +-------+---+ |
| |
| -- A non-foldable expression as an input to OFFSET is not allowed. |
| SELECT name, age FROM person ORDER BY name OFFSET length(name); |
| org.apache.spark.sql.AnalysisException: The offset expression must evaluate to a constant value ... |
| ``` |
| |
| ### Related Statements |
| |
| * [SELECT Main](sql-ref-syntax-qry-select.html) |
| * [WHERE Clause](sql-ref-syntax-qry-select-where.html) |
| * [GROUP BY Clause](sql-ref-syntax-qry-select-groupby.html) |
| * [HAVING Clause](sql-ref-syntax-qry-select-having.html) |
| * [ORDER BY Clause](sql-ref-syntax-qry-select-orderby.html) |
| * [SORT BY Clause](sql-ref-syntax-qry-select-sortby.html) |
| * [CLUSTER BY Clause](sql-ref-syntax-qry-select-clusterby.html) |
| * [DISTRIBUTE BY Clause](sql-ref-syntax-qry-select-distribute-by.html) |
| * [LIMIT Clause](sql-ref-syntax-qry-select-limit.html) |
| * [CASE Clause](sql-ref-syntax-qry-select-case.html) |
| * [PIVOT Clause](sql-ref-syntax-qry-select-pivot.html) |
| * [UNPIVOT Clause](sql-ref-syntax-qry-select-unpivot.html) |
| * [LATERAL VIEW Clause](sql-ref-syntax-qry-select-lateral-view.html) |