> ## Documentation Index
> Fetch the complete documentation index at: https://docs.e6data.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Queries

> SELECT returns expressions from a source. A source can be a table, view, subquery, inline VALUES relation, or a supported table function. A SELECT without FROM evaluates scalar exp

ReferenceApplies to: Unified engine deployments

SELECT returns expressions from a source. A source can be a table, view, subquery, inline VALUES relation, or a supported table function. A SELECT without FROM evaluates scalar expressions once.

## Syntax

The common clause order is:

```text theme={"theme":{"light":"github-light","dark":"github-dark"}}
[WITH name AS (query), ...]
SELECT [DISTINCT] expression [AS alias], ...
[FROM source [JOIN source ON condition] ...]
[WHERE condition]
[GROUP BY expression, ...]
[HAVING condition]
[ORDER BY expression [ASC | DESC] [NULLS FIRST | NULLS LAST], ...]
[LIMIT count [OFFSET count]]
```

WHERE filters input rows. GROUP BY forms groups; HAVING filters those groups. Window expressions retain individual rows. ORDER BY determines result order; LIMIT restricts returned rows.

## Projection and aliases

Select named columns to keep the result focused. `*` selects all visible columns; `alias.*` selects columns from one source. Give calculated outputs explicit names.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT i_item_sk AS item_key, i_category AS category,
       CAST(i_current_price AS DOUBLE) AS price
FROM demo.tpcds.item
ORDER BY item_key
LIMIT 10;
```

## Filtering

Combine predicates with AND, OR, and NOT. Parenthesize mixed logic. A WHERE predicate retains only rows for which its result is TRUE; FALSE and NULL are excluded.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT i_item_sk, i_category, i_current_price
FROM demo.tpcds.item
WHERE i_category IS NOT NULL
  AND i_current_price BETWEEN 10 AND 30
ORDER BY i_current_price DESC, i_item_sk
LIMIT 20;
```

## DISTINCT

DISTINCT removes duplicate complete projected rows. It applies to the whole projection, not just the first column. DISTINCT ON is unavailable.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT DISTINCT ca_state
FROM demo.tpcds.customer_address
WHERE ca_state IS NOT NULL
ORDER BY ca_state
LIMIT 20;
```

## Ordering, limits, and offsets

Ascending order is the default. State NULLS FIRST or NULLS LAST when null placement matters. Add a unique tie-breaker for repeatable limited results. Without ORDER BY, a returned row order is not guaranteed. LIMIT controls returned rows and does not guarantee that the engine reads only that many source rows.

```sql theme={"theme":{"light":"github-light","dark":"github-dark"}}
SELECT i_item_sk, i_current_price
FROM demo.tpcds.item
ORDER BY i_current_price DESC NULLS LAST, i_item_sk
LIMIT 5 OFFSET 5;
```

SORT BY, DISTRIBUTE BY, and CLUSTER BY have dialect-specific planning behavior. Use ORDER BY when the result must have a defined global order. QUALIFY, TOP, FETCH, SELECT INTO, and LIMIT BY are unavailable.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.