Druid translates SQL queries into its native query language for execution. To learn about translation and optimize performance, see SQL query translation.
Overview
You can query data in Druid datasources using Druid SQL. SQL planning occurs on the Broker, so configure Broker runtime properties to adjust query planning and JDBC settings. For information on permissions needed to make SQL queries, see Defining SQL permissions.SQL Query Syntax
Druid SQL supports SELECT queries with the following structure:FROM Clause
The FROM clause can refer to:- Table datasources from the
druidschema (default). Reference asdruid.dataSourceNameor simplydataSourceName - Lookups from the
lookupschema, for examplelookup.countries - Subqueries enclosed in parentheses
- Joins between datasources (except between native datasources and system tables)
- Metadata tables from
INFORMATION_SCHEMAorsysschemas
Example Queries
Basic SELECT
PIVOT Example
Transform row values into columns:UNPIVOT Example
Transform columns into rows:UNNEST
The UNNEST clause unnests ARRAY typed values. UseMV_TO_ARRAY for multi-value dimensions:
- Works directly on ARRAY columns
- Use
MV_TO_ARRAY(dimension)for multi-value VARCHAR columns - CROSS JOIN is required (except for inline arrays)
- Preserves array ordering
- Does not remove duplicates or nulls
WHERE Clause
The WHERE clause filters rows and is translated to native filters. You can reference subqueries:- Strings and numbers can be compared through implicit conversion
- For best performance, explicitly cast numbers to strings when comparing against string dimensions:
GROUP BY
The GROUP BY clause aggregates data. You can:- Group by expressions or column ordinals:
GROUP BY 2 - Use GROUPING SETS for multiple grouping levels:
- Use ROLLUP for hierarchical aggregation:
- Use CUBE for all combinations:
UNION ALL
Combine results from multiple queries:Top-level UNION ALL
Table-level UNION ALL
EXPLAIN PLAN
View how queries are translated to native queries:SET Statement
Configure query context parameters:Dynamic Parameters
Use? placeholders for parameterized queries:
Array Parameters
Type Casting
Explicitly cast parameters when needed:Identifiers and Literals
Identifiers:- Optionally quote with double quotes:
"columnName" - Escape quotes by doubling:
"My ""column"" name" - Case-sensitive with no implicit conversions
- Strings:
'foo' - Unicode strings:
U&'fo\00F6' - Numbers:
100,100.0,1.0e5 - Timestamps:
TIMESTAMP '2000-01-01 00:00:00' - Intervals:
INTERVAL '1' HOUR,INTERVAL '1-2' YEAR TO MONTH
Reserved Keywords
Druid inherits reserved keywords from Apache Calcite, plus:CLUSTEREDPARTITIONED
Related Pages
- Data types - Supported data types
- Aggregation functions - Available aggregations
- Scalar functions - String, numeric, and other functions
- Multi-value string functions - Operations on multi-value dimensions
- Query translation - How SQL maps to native queries
- SQL API - HTTP API reference
- JDBC driver - JDBC driver documentation