---
title: Query
description: Retrieve rows from one or more tables with SQL query syntax in Databricks SQL and Databricks Runtime, using clauses such as WHERE, GROUP BY, and ORDER BY.
canonical_url: https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-query
---

# Query

Retrieves result sets from one or more tables.

**Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime

## Syntax

```
[ common_table_expression ]
  subquery
  [ set_operator ]
  [ ORDER BY clause | { [ DISTRIBUTE BY clause ] [ SORT BY clause ] } | CLUSTER BY clause ]
  [ WINDOW clause ]
  [ LIMIT clause  ]
  [ OFFSET clause ]
  [ { |> piped_operation } [ ...] ]

subquery
{ subselect |
  VALUES clause |
  ( query ) |
  TABLE [ table_name | view_name ] |
  FROM table_reference [, ...] }
```

## Parameters

* **[common table expression](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-cte.md)**

  Common table expressions (CTE) are one or more named queries which can be reused multiple times within the main
  query block to avoid repeated computations or to improve readability of complex, nested queries.

* **subquery**

  One of several constructs producing an intermediate result set.

  * **[subselect](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select.md)**

    A subquery consisting of a `SELECT FROM WHERE` pattern.

  * **[VALUES](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-values.md)**

    Specified an inline temporary table.

  * **( [query](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-query.md#) )**

    A nested invocation of a query which may contain set operators or common table expressions.

  * **TABLE**

    Returns the entire table or view.

    * **[table\_name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#table-name)**

      Identifies the table to be returned.

    * **[view\_name](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-names.md#view-name)**

      Identifies the view to be returned.

    If the table or view cannot be found Databricks raises a [TABLE\_OR\_VIEW\_NOT\_FOUND](https://docs.databricks.com/aws/en/error-messages/table-or-view-not-found-error-class.md) error.

  * **FROM**

    Applies to: ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 16.2 and above

    Returns the cross join of all table-references.
    This clause is typically used as the root of a SQL pipeline.

    * [table\_reference](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md)

      A source of input for the `SELECT`. This input reference can be turned into a streaming reference by using the `STREAM` keyword prior to the reference.

  A source of input for the `SELECT`. This input reference can be turned into a streaming reference by using the `STREAM` keyword prior to the reference.

* **[set\_operator](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-setops.md)**

  A construct combining subqueries using `UNION`, `EXCEPT`, or `INTERSECT` operators.

* **[ORDER BY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-orderby.md)**

  An ordering of the rows of the complete result set of the query. The output rows are ordered
  across the partitions. This parameter is mutually exclusive with `SORT BY`,
  `CLUSTER BY`, and `DISTRIBUTE BY` and cannot be specified together.

* **[DISTRIBUTE BY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-distributeby.md)**

  A set of expressions by which the result rows are repartitioned. This parameter is mutually
  exclusive with `ORDER BY` and `CLUSTER BY` and cannot be specified together.

* **[SORT BY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sortby.md)**

  An ordering by which the rows are ordered within each partition. This parameter is mutually
  exclusive with `ORDER BY` and `CLUSTER BY` and cannot be specified together.

* **[CLUSTER BY](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-clusterby.md)**

  A set of expressions that is used to repartition and sort the rows. Using this clause has
  the same effect of using `DISTRIBUTE BY` and `SORT BY` together.

* **[LIMIT](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-limit.md)**

  The maximum number of rows that can be returned by a statement or subquery. This clause
  is mostly used in the conjunction with `ORDER BY` to produce a deterministic result.

* **[OFFSET](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-offset.md)**

  **Applies to:** ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 11.3 LTS and above

  Skips a number of rows returned by a statement or subquery.
  This clause is mostly used in the conjunction with `LIMIT` to *page* through a result set,
  and `ORDER BY` to produce a deterministic result.

  > [!NOTE]
  >
  > When paging through a result set using `LIMIT` and `OFFSET` the skipped rows still get processed.
  > These rows merely get suppressed from the result set.
  > Pagination with this technique is not advised for resource-intensive queries.

* **[WINDOW](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-named-window.md)**

  Defines named window specifications that can be shared by multiple [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md) in the `select_query`.

* **`|>` [piped\_operation](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pipeop.md)**

  Applies to: ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/aws/en/images/icons/check.png) Databricks Runtime 16.2 and above

  Specifies a pipelined operation to be performed on the result set of the subquery and the other preceding clauses.

## Common error conditions

* [TABLE\_OR\_VIEW\_NOT\_FOUND](https://docs.databricks.com/aws/en/error-messages/table-or-view-not-found-error-class.md)

## Related articles

* [CLUSTER BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-clusterby.md)
* [Common table expression (CTE)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-cte.md)
* [DISTRIBUTE BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-distributeby.md)
* [GROUP BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-groupby.md)
* [HAVING clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-having.md)
* [Hints](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-hints.md)
* [VALUES clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-values.md)
* [JOIN](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-join.md)
* [LATERAL VIEW clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-lateral-view.md)
* [LIMIT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-limit.md)
* [ORDER BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-orderby.md)
* [PIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pivot.md)
* [Set operator](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-setops.md)
* [SORT BY clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sortby.md)
* [SQL Pipeline Syntax](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-pipeline.md)
* [SQL Pipeline Operator](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-pipeop.md)
* [Table reference](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-table-reference.md)
* [TABLESAMPLE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-sampling.md)
* [Table-valued function (TVF)](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-tvf.md)
* [UNPIVOT clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-unpivot.md)
* [WATERMARK clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-watermark.md)
* [WHERE clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-where.md)
* [WINDOW clause](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-syntax-qry-select-named-window.md)
* [Window functions](https://docs.databricks.com/aws/en/sql/language-manual/sql-ref-window-functions.md)
