Query files directly
SELECT * FROM 'sales.csv'; -- no import step
SELECT * FROM read_csv('sales_*.csv', union_by_name = true);
SELECT * FROM 'sales/*.parquet';
SELECT * FROM read_json('events.json');
DESCRIBE SELECT * FROM 'sales.parquet'; -- column names and typesTables
CREATE TABLE sales AS FROM 'sales.csv';
CREATE OR REPLACE TABLE sales AS FROM read_csv('sales_*.csv');
INSERT INTO sales BY NAME SELECT 5 AS order_id, 'East' AS region;
SUMMARIZE sales; -- min, max, nulls, distinct, quartilesFriendly SQL
FROM sales; -- = SELECT * FROM sales
FROM sales SELECT region, amount WHERE amount > 100;
SELECT * EXCLUDE (notes) FROM sales;
SELECT * REPLACE (round(amount) AS amount) FROM sales;
SELECT region, sum(amount) FROM sales GROUP BY ALL ORDER BY ALL;
SELECT max(COLUMNS('amount|tax')) FROM sales; -- regex on namesWindows and QUALIFY
SELECT region, date, amount,
sum(amount) OVER (PARTITION BY region ORDER BY date) AS running
FROM sales;
-- top order per region, no subquery
SELECT * FROM sales
QUALIFY row_number() OVER (PARTITION BY region ORDER BY amount DESC) = 1;ASOF join
-- the latest price valid at each order date
SELECT s.order_id, p.rate
FROM sales s
ASOF JOIN prices p
ON s.region = p.region AND s.date >= p.valid_from;
-- ASOF LEFT JOIN keeps orders with no matchPivot and unpivot
CREATE TABLE wide AS
PIVOT (SELECT region, amount, month(date) AS month FROM sales)
ON region USING sum(amount) GROUP BY month;
UNPIVOT wide ON COLUMNS(* EXCLUDE (month))
INTO NAME region VALUE total;Text and dates
SELECT date_trunc('month', date) AS month,
strftime(date, '%d/%m/%Y') AS uk_date,
date_diff('day', date, DATE '2026-12-31') AS days_left,
regexp_extract(code, '\d+') AS number,
region ILIKE 'n%' AS starts_with_n
FROM sales;Lists and lambdas (2.0)
SELECT list_transform([1, 2, 3], lambda x: x * 10); -- [10, 20, 30]
SELECT list_filter([1, 2, 3], lambda x: x > 1); -- [2, 3]
SELECT [x * 10 FOR x IN [1, 2, 3]]; -- comprehension
-- 2.0 disables the old x -> x + 1 arrow form by defaultJSON and VARIANT (2.0)
SELECT payload ->> '$.user.id' AS user_id FROM raw_events;
SELECT json_set('{"a": 1}', '$.b', '2'); -- {"a":1,"b":2}
CREATE TABLE events (payload VARIANT); -- fast semi-structured
INSERT INTO events VALUES ('{"event": "click", "x": 10}'::JSON::VARIANT);
SELECT variant_type(payload), variant_keys(payload) FROM events;Variables
SET VARIABLE min_amount = 100;
SELECT * FROM sales WHERE amount > getvariable('min_amount');Export
COPY (FROM sales) TO 'out.parquet' (FORMAT parquet);
COPY sales TO 'out' (FORMAT parquet, PARTITION_BY (region));
COPY sales TO 'out.csv' (HEADER);
SELECT count(*) FROM 'out/**/*.parquet'; -- read it backCloud and other databases
CREATE SECRET (TYPE s3, PROVIDER credential_chain); -- uses your AWS login
SELECT * FROM 's3://my-bucket/sales/*.parquet';
ATTACH 'shop.duckdb' AS shop; -- another DuckDB file
ATTACH 'dbname=shop host=localhost' AS pg (TYPE postgres, READ_ONLY);
CREATE TABLE shop.sales_copy AS FROM sales;Triggers (2.0)
CREATE TRIGGER trg_audit
AFTER UPDATE ON target
REFERENCING OLD TABLE AS o NEW TABLE AS n
FOR EACH STATEMENT
INSERT INTO audit
SELECT n.id, o.val, n.val FROM o JOIN n ON o.id = n.id;Server mode (2.0)
-- on the server
CALL quack_serve(token = 'my_token');
-- on a client
ATTACH 'quack:localhost' AS qk (TOKEN 'my_token');
CONNECT qk;
SELECT * FROM events;
-- listens on localhost only; use a TLS proxy for remote accessPython
import duckdb
duckdb.sql("SELECT region, sum(amount) FROM 'sales.csv' GROUP BY ALL").df()
df = duckdb.sql("FROM 'sales.parquet'").df() # to pandas
duckdb.sql("SELECT count(*) FROM df") # query a DataFrame by name
duckdb.sql("FROM df").pl() # to Polars
con = duckdb.connect("shop.duckdb") # a file on disk
con.sql("CREATE OR REPLACE TABLE t AS FROM df")Good habits
Query Parquet instead of CSV whenever you can: it is smaller and much faster. Filter and pick columns early, because DuckDB only reads what you ask for. DuckDB 2.0 files cannot be opened by older versions, so upgrade everyone who shares a database file at the same time. Use ORDER BY when the order matters; without it, row order is not guaranteed.
