Skip to main content

Query Syntax

This page provides reference information for the query syntax in Databend. Each component can be used individually or combined to build powerful queries.

Core Query Components

ComponentDescription
SELECTRetrieve data from tables - the foundation of all queries
FROM / JOINSpecify data sources and combine multiple tables
WHEREFilter rows based on conditions
GROUP BYGroup rows and perform aggregations (SUM, COUNT, AVG, etc.)
HAVINGFilter grouped results
ORDER BYSort query results
LIMIT / TOPRestrict the number of rows returned

Advanced Features

ComponentDescription
WITH (CTE)Define reusable query blocks for complex logic
PIVOTConvert rows to columns (wide format)
UNPIVOTConvert columns to rows (long format)
QUALIFYFilter rows after window function calculations
VALUESCreate inline temporary data sets

Time Travel & Streaming

ComponentDescription
ATQuery data at a specific point in time
CHANGESTrack insertions, updates, and deletions
WITH CONSUMEProcess streaming data with offset management
WITH STREAM HINTSOptimize stream processing behavior

Query Execution

ComponentDescription
SettingsConfigure query optimization and execution parameters

Query Structure

A typical Databend query follows this structure:

[WITH cte_expressions]
SELECT [TOP n] columns
FROM table
[JOIN other_tables]
[WHERE conditions]
[GROUP BY columns]
[HAVING group_conditions]
[QUALIFY window_conditions]
[ORDER BY columns]
[LIMIT n]
Explore Databend Cloud for FREE
Low-cost
Fast Analytics
Easy Data Ingestion
Elastic Scaling
Try it today