LIMIT 子句
適用于: Databricks SQL Databricks Runtime
限制 查詢所傳回的資料列數目。 一般而言,這個子句會與 ORDER BY 搭配使用,以確保結果具決定性。
語法
LIMIT { ALL | integer_expression }
參數
所有
如果指定,查詢會傳回所有資料列。 換句話說,如果未指定此選項,則不會套用任何限制。
integer_expression
傳回整數的常值運算式。
例子
> CREATE TEMP VIEW person (name, age)
AS VALUES ('Zen Hui', 25),
('Anil B' , 18),
('Shone S', 16),
('Mike A' , 25),
('John A' , 18),
('Jack N' , 16);
-- Select the first two rows.
> SELECT name, age FROM person ORDER BY name LIMIT 2;
Anil B 18
Jack N 16
-- Select the 4th and 5th rows by alphabetical order.
> SELECT name, age FROM person ORDER BY name LIMIT 2 OFFSET 3;
Mike A 25
Shone S 16
-- Specifying ALL option on LIMIT returns all the rows.
> SELECT name, age FROM person ORDER BY name LIMIT ALL;
Anil B 18
Jack N 16
John A 18
Mike A 25
Shone S 16
Zen Hui 25
-- A function expression as an input to LIMIT.
> SELECT name, age FROM person ORDER BY name LIMIT length('SPARK');
Anil B 18
Jack N 16
John A 18
Mike A 25
Shone S 16
-- A non-literal expression as an input to LIMIT is not allowed.
SELECT name, age FROM person ORDER BY name LIMIT length(name);
Error: The limit expression must evaluate to a constant value