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.

Delimited Identifiers

Use ANSI double quotes when a table name, column name, or alias includes punctuation, spaces, reserved words, leading digits, or exact-case DynamoDB names:
SELECT
  "order-id",
  "Safety.Warning",
  total AS "Total Amount"
FROM east."prod-orders"
ORDER BY "order-id"
Double quotes delimit identifiers only. Single quotes still create string literals:
SELECT 'order-id' AS label, "order-id" FROM east."prod-orders"

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. Each sort key can be ASC (default) or DESC. Multiple sort keys are separated by commas:
SELECT id, name FROM myschema.products ORDER BY name
SELECT id, name FROM myschema.products ORDER BY name DESC
SELECT id, created_at, total FROM myschema.orders ORDER BY created_at ASC, total DESC
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 DESC
NULL ordering: NULL values sort before non-null values when ascending, and after non-null values when descending. NULLS FIRST and NULLS LAST are not supported.

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.