0001 ---
0002 layout: global
0003 title: Sampling Queries
0004 displayTitle: Sampling Queries
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 `TABLESAMPLE` statement is used to sample the table. It supports the following sampling methods:
0025 * `TABLESAMPLE`(x `ROWS`): Sample the table down to the given number of rows.
0026 * `TABLESAMPLE`(x `PERCENT`): Sample the table down to the given percentage. Note that percentages are defined as a number between 0 and 100.
0027 * `TABLESAMPLE`(`BUCKET` x `OUT OF` y): Sample the table down to a `x` out of `y` fraction.
0028
0029 **Note:** `TABLESAMPLE` returns the approximate number of rows or fraction requested.
0030
0031 ### Syntax
0032
0033 ```sql
0034 TABLESAMPLE ({ integer_expression | decimal_expression } PERCENT)
0035 | TABLESAMPLE ( integer_expression ROWS )
0036 | TABLESAMPLE ( BUCKET integer_expression OUT OF integer_expression )
0037 ```
0038
0039 ### Examples
0040
0041 ```sql
0042 SELECT * FROM test;
0043 +--+----+
0044 |id|name|
0045 +--+----+
0046 | 5|Alex|
0047 | 8|Lucy|
0048 | 2|Mary|
0049 | 4|Fred|
0050 | 1|Lisa|
0051 | 9|Eric|
0052 |10|Adam|
0053 | 6|Mark|
0054 | 7|Lily|
0055 | 3|Evan|
0056 +--+----+
0057
0058 SELECT * FROM test TABLESAMPLE (50 PERCENT);
0059 +--+----+
0060 |id|name|
0061 +--+----+
0062 | 5|Alex|
0063 | 2|Mary|
0064 | 4|Fred|
0065 | 9|Eric|
0066 |10|Adam|
0067 | 3|Evan|
0068 +--+----+
0069
0070 SELECT * FROM test TABLESAMPLE (5 ROWS);
0071 +--+----+
0072 |id|name|
0073 +--+----+
0074 | 5|Alex|
0075 | 8|Lucy|
0076 | 2|Mary|
0077 | 4|Fred|
0078 | 1|Lisa|
0079 +--+----+
0080
0081 SELECT * FROM test TABLESAMPLE (BUCKET 4 OUT OF 10);
0082 +--+----+
0083 |id|name|
0084 +--+----+
0085 | 8|Lucy|
0086 | 2|Mary|
0087 | 9|Eric|
0088 | 6|Mark|
0089 +--+----+
0090 ```
0091
0092 ### Related Statements
0093
0094 * [SELECT](sql-ref-syntax-qry-select.html)