DQL Basics
This tutorial walks through core DQL concepts using a live in-browser compiler. Each step has an editable query -- press Run (or Ctrl+Enter) to compile it to SQL and execute it against the demo database.
Feel free to modify any query and re-run it.
Select All
The simplest DQL query names a table and uses (*) to select all columns.
Projection
The pipe operator |> sends rows to the next stage. Parentheses after a pipe
select which columns to keep.
Filtering
Add a comma after (*) to filter rows. This is like SQL's WHERE clause, but
written inline with the table expression.
Filter + Project
Stages compose left-to-right. Filter first, then project -- the pipe carries the filtered rows into the column selection.
Sorting
#(column) sorts ascending, #(column desc) sorts descending.
Try changing #(age) to #(age desc) for descending order.
Sort + Limit
#<N limits the result to N rows. Combine with #() for top-N queries.
Aggregation
The %( ) operator groups rows. Inside it, ~> separates the grouping
key from the aggregate expressions.
Multiple Aggregates
You can compute several aggregates in the same group. Here we filter to
completed orders, group by user, and compute both sum and avg.
Distinct
Distinct is just group-by without the aggregate functions.
Joins
List multiple tables separated by commas, give them aliases with as,
and state the join condition. DQL infers an inner join.