Skip to main content

DuckDB CLI Basics

Learning Focus

Use this lesson to become productive with the DuckDB CLI — your primary tool for interactive SQL work.

Launching the DuckDB CLI

# In-memory session (nothing persisted after exit)
duckdb

# Persistent database file
duckdb mydb.db

# Run a single query and exit
duckdb mydb.db "SELECT COUNT(*) FROM employees;"

# Run a SQL file
duckdb mydb.db < queries.sql

Essential CLI Commands

CommandDescription
.helpShow all dot commands
.tablesList all tables
.schemaShow CREATE statements for all tables
.schema tablenameShow CREATE for a specific table
.mode tableSwitch output to table format
.mode csvSwitch output to CSV format
.output file.csvRedirect output to a file
.quit or .exitExit the CLI
.read file.sqlExecute a SQL file
-- List all tables
SHOW TABLES;

-- Describe a table's columns and types
DESCRIBE employees;

-- Get database and schema info
PRAGMA database_list;

-- Show DuckDB version
SELECT version();

-- Show current settings
PRAGMA all_pragmas;

Running Your First Queries

-- Create a table
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR,
dept VARCHAR,
salary DECIMAL(10,2),
hired DATE
);

-- Insert rows
INSERT INTO employees VALUES
(1, 'Alice', 'Engineering', 95000.00, '2021-03-15'),
(2, 'Bob', 'Marketing', 72000.00, '2019-06-01'),
(3, 'Charlie', 'Engineering', 88000.00, '2022-01-20'),
(4, 'Diana', 'HR', 65000.00, '2020-09-10');

-- Query data
SELECT * FROM employees;
SELECT dept, AVG(salary) AS avg_salary FROM employees GROUP BY dept;

Output Formatting

-- Default: table format
SELECT name, salary FROM employees;

-- Pipe-separated
.mode line
SELECT name, salary FROM employees;

-- Export query to CSV
.output results.csv
SELECT * FROM employees;
.output -- reset to stdout

Reading Files Directly from CLI

-- Read a CSV without importing
SELECT * FROM read_csv_auto('employees.csv') LIMIT 5;

-- Read a Parquet file
SELECT * FROM 'data.parquet' LIMIT 10;

-- Read JSON
SELECT * FROM read_json_auto('records.json') LIMIT 5;

CLI Configuration Tips

# Enable multi-line editing
.prompt '> '

# Set null display
.nullvalue NULL

# Enable timing for each query
.timer on

Common Pitfalls

PitfallConsequencePrevention
Forgetting to use a .db fileAll work lost after exitAlways use duckdb mydb.db for persistent work
Using MySQL-style backtick quotingSyntax error in DuckDBUse double quotes "column" for identifiers
Mixing ; and dot commandsCLI confusionDot commands (.tables) have no semicolon; SQL does

Quick Reference

duckdb          # in-memory
duckdb mydb.db # persistent

# Inside CLI:
SHOW TABLES;
DESCRIBE tablename;
SELECT version();
.quit

What's Next