Skip to main content

Basic SELECT

Select one or more expressions from a table:
SELECT id, name, email FROM myschema.users

Column Aliases

Use AS to name an output column, or omit AS and place the alias directly after the expression:
SELECT id, total * 1.1 AS total_with_tax FROM myschema.orders
SELECT id, total * 1.1 total_with_tax FROM myschema.orders  -- bare alias
Aliases defined in the SELECT list can be referenced in ORDER BY.

SELECT *

Expand all columns from all referenced tables:
SELECT * FROM myschema.products
Expand all columns from a specific table when multiple tables are in scope:
SELECT o.*, c.name FROM myschema.orders AS o
INNER JOIN myschema.customers AS c ON o.customer_id = c.id

SELECT DISTINCT

Deduplicate rows in the result set:
SELECT DISTINCT status FROM myschema.orders

ORDER BY

Sort results by one or more columns. Multiple sort keys are separated by commas:
SELECT id, name FROM myschema.products ORDER BY name
SELECT id, created_at, total FROM myschema.orders ORDER BY created_at, total
Aliases from the SELECT list are usable in ORDER BY:
SELECT customer_id, SUM(total) AS lifetime_value
FROM myschema.orders
GROUP BY customer_id
ORDER BY lifetime_value
ORDER BY DESC is not supported. All sorts are ascending. NULLS FIRST and NULLS LAST are also not supported. See Limitations.

LIMIT

Cap the number of rows returned:
SELECT * FROM myschema.events LIMIT 100
ANSI FETCH FIRST n ROWS ONLY is equivalent:
SELECT * FROM myschema.events FETCH FIRST 100 ROWS ONLY
OFFSET skips a fixed number of rows before returning results:
SELECT * FROM myschema.products LIMIT 10 OFFSET 20
For paginating through a full result set, prefer the maxRows / resumeIdx API options over SQL OFFSET. See Pagination.