Skip to main content

SQL reference

D.Hub uses ClickHouse SQL in pipeline SQL nodes, dataset exploration, and dashboard widgets. All three interfaces use the same syntax and functions, but each identifies its input table differently.

Input tables by interface

InterfaceInput tableResult
Pipeline SQL nodeThe alias assigned to the input connection, or input for a single input without an aliasQuery result passed to the next node
Dataset Data tabThe current dataset's physical table nameQuery result displayed in the preview table
Dashboard widget SQL modeThe selected data source's database.tableQuery result passed to the widget

All three interfaces use SELECT statements to query data. input is not a shared table name across all SQL interfaces; it is only the default for a pipeline input.

SQL transformation node

Use a pipeline SQL node to filter or aggregate data.

Example

-- Keep only input rows whose 'status' is 'active'
SELECT
id,
name,
created_at
FROM
input
WHERE
status = 'active'

Dataset exploration

Run a query on the Data tab of a dataset details page to inspect its data.

  • Restriction: Only SELECT queries are allowed.
  • Table: Use the current dataset's table name.
SELECT * FROM my_dataset_table LIMIT 100

Use SQL in a dashboard

In a dashboard widget's SQL mode, query the selected data source's database.table. Structure the result columns to match the field names and data types expected by the widget.

SELECT
toStartOfMonth(event_date) AS month,
count() AS event_count
FROM analytics.events
GROUP BY month
ORDER BY month

To avoid writing SQL directly, select a data source and fields in the widget's Simple mode.

SQL functions

The following ClickHouse SQL functions are commonly used in dashboard SQL mode, SQL nodes, and dataset exploration.

Date and time functions

FunctionDescriptionExample
today()Today's dateWHERE date = today()
now()Current timeWHERE created_at > now() - INTERVAL 1 HOUR
toStartOfMonth(date)Start of the monthGROUP BY toStartOfMonth(date)
toStartOfWeek(date)Start of the weekGROUP BY toStartOfWeek(date)
toStartOfHour(datetime)Start of the hourGROUP BY toStartOfHour(ts)
toYYYYMM(date)Convert to a YYYYMM integerSELECT toYYYYMM(date)
dateDiff('day', d1, d2)Difference between datesdateDiff('day', start, end)
formatDateTime(dt, fmt)Format a date and timeformatDateTime(dt, '%Y-%m-%d')

Aggregate functions

FunctionDescriptionExample
count()Number of rowsCOUNT(*)
sum(col)SumSUM(amount)
avg(col)AverageAVG(price)
min(col) / max(col)Minimum or maximumMIN(temperature)
uniq(col)Approximate distinct countuniq(user_id)
uniqExact(col)Exact distinct countuniqExact(session_id)
quantile(0.95)(col)Quantilequantile(0.95)(latency)
groupArray(col)Collect values into an array by groupgroupArray(tag)
argMax(col, val)Value of col where val is greatestargMax(name, score)

String functions

FunctionDescriptionExample
lower(s) / upper(s)Convert to lowercase or uppercaselower(name)
trim(s)Remove leading and trailing spacestrim(input_str)
substring(s, offset, len)Extract a substringsubstring(code, 1, 3)
concat(s1, s2)Concatenate stringsconcat(first, ' ', last)
like(s, pattern)Match a patternWHERE name LIKE '%Seoul%'
match(s, regexp)Match a regular expressionWHERE match(url, '^/api/')
splitByChar(sep, s)Split on a charactersplitByChar(',', tags)
replaceAll(s, from, to)Replace substringsreplaceAll(text, '\n', ' ')

Array functions

FunctionDescriptionExample
length(arr)Array lengthlength(tags)
arrayJoin(arr)Expand an array into rowsSELECT arrayJoin(items)
has(arr, elem)Check whether an element is presentWHERE has(tags, 'urgent')
arrayMap(f, arr)Map an arrayarrayMap(x -> x * 2, values)
arrayFilter(f, arr)Filter an arrayarrayFilter(x -> x > 0, values)

JSON functions

FunctionDescriptionExample
JSONExtractString(json, key)Extract a stringJSONExtractString(data, 'name')
JSONExtractInt(json, key)Extract an integerJSONExtractInt(data, 'count')
JSONExtractFloat(json, key)Extract a floating-point valueJSONExtractFloat(data, 'score')
JSONExtractBool(json, key)Extract a BooleanJSONExtractBool(data, 'active')
JSONExtractArrayRaw(json, key)Extract an arrayJSONExtractArrayRaw(data, 'items')

See the ClickHouse SQL reference for complete syntax and function documentation. When using SQL in D.Hub, also apply the interface-specific input-table conventions and SELECT restriction described above.

Reduce the query scope

  • Select only the columns you need: List the required columns instead of using SELECT *.
  • Use LIMIT: Add LIMIT to exploratory queries.
  • Use a WHERE clause: Add filter conditions to reduce the queried range.
  • Choose an appropriate aggregate function: Use uniq instead of uniqExact when an exact distinct count is unnecessary.