How to run SQL queries on a CSV file (no database needed)
Five ways to query CSV files with SQL, from a free browser tool to DuckDB, SQLite and Python, plus ready-to-use queries for totals, joins, duplicates and running totals.
If you know a little SQL, it’s often the fastest way to answer questions about a CSV file: totals by month, the top 10 customers, rows in one file but not another. The old assumption was that you first had to set up a database and import the file. You don’t any more. Several tools run SQL directly on the file.
Most of my day-to-day file checks start this way. Before building anything in a pipeline, I run three or four quick queries on the raw export to see what’s really in it: row counts, empty values, duplicates, odd dates. SQL makes that a two-minute job.
1. In your browser, with nothing to install
SQL on CSV turns each file you drop into a table named after the file, so sales 2026.csv becomes sales_2026. Then you write SQL in the page. It runs on DuckDB inside your browser, so the files are never uploaded, and files of several gigabytes work. It handles CSV, Excel, Parquet and JSON, and you can join several files in one query.
SELECT strftime(order_date, '%Y-%m') AS month,
count(*) AS orders,
sum(amount) AS revenue
FROM sales_2026
GROUP BY month
ORDER BY month;
2. DuckDB on the command line
DuckDB reads CSV files directly. Treat the file name as a table:
duckdb -c "SELECT city, count(*) FROM 'customers.csv' GROUP BY city ORDER BY 2 DESC"
It detects the separator, header and column types itself, and it’s very fast on large files. Install it with brew install duckdb on a Mac, or download it from duckdb.org for Windows and Linux. To save a result:
COPY (SELECT * FROM 'orders.csv' WHERE status = 'returned') TO 'returns.csv' (HEADER);
3. SQLite
SQLite is probably already on your computer: macOS and most Linux systems include sqlite3. Import the CSV into a table, then query it:
sqlite3 data.db
.import --csv orders.csv orders
SELECT status, count(*) FROM orders GROUP BY status;
Two things to know. SQLite imports every column as text unless the table already exists with types, so ORDER BY amount may sort "100" before "20". Use CAST(amount AS REAL) for numbers. And the import copies the data, so a 2 GB CSV becomes a database of a similar size.
4. Python (pandas or DuckDB)
If you already work in Python, DuckDB can query CSV files and pandas DataFrames in the same statement:
import duckdb
import pandas as pd
targets = pd.read_excel("targets.xlsx")
duckdb.sql("""
SELECT o.region, sum(o.amount) AS actual, t.target
FROM 'orders.csv' o
JOIN targets t USING (region)
GROUP BY o.region, t.target
""").df()
.df() returns a pandas DataFrame, ready for charts or to_excel.
5. csvkit, for quick one-liners
csvsql from the csvkit package runs SQL on CSV files without a separate import step:
csvsql --query "SELECT product, sum(qty) FROM orders GROUP BY product" orders.csv
It’s convenient for small files and shell scripts, but slower than DuckDB on large ones, because it loads the data into an in-memory SQLite database first.
Queries worth keeping
These work in SQL on CSV and DuckDB, and with minor changes in most databases.
Profile a file you’ve never seen:
SUMMARIZE orders;
One row per column: type, min, max, approximate distinct count and percentage of nulls. It’s the fastest sanity check there is.
Find duplicate keys:
SELECT order_id, count(*) AS copies
FROM orders
GROUP BY order_id
HAVING count(*) > 1;
Rows in one file but not the other (a reconciliation):
SELECT * FROM orders_new
EXCEPT
SELECT * FROM orders_old;
Join two files, like a VLOOKUP that brings every column at once:
SELECT o.*, c.name, c.segment
FROM orders o
LEFT JOIN customers c USING (customer_id);
Top N per group, here the 3 biggest orders per region:
SELECT *
FROM orders
QUALIFY row_number() OVER (PARTITION BY region ORDER BY amount DESC) <= 3;
Running total:
SELECT order_date, amount,
sum(amount) OVER (ORDER BY order_date) AS running_total
FROM orders;
Text dates to real dates, when a file stores 31/01/2026:
SELECT strptime(order_date, '%d/%m/%Y') AS order_date, amount FROM orders;
Common errors and what they mean
- “Could not convert string to INTEGER/DOUBLE”: a numeric-looking column has text in it somewhere, like
N/Aor1,450.00. UseTRY_CAST(column AS DOUBLE), which returns NULL instead of failing, or clean the column first. - Column names with spaces need double quotes:
SELECT "Order Date" FROM orders. - Wrong number of columns on line N: a value contains the separator but isn’t quoted, or a row is cut off. Open the file around that line, for example in the Large CSV viewer, to see what’s wrong.
Which to choose
| Situation | Best fit |
|---|---|
| Quick question, any computer, private data | SQL on CSV in the browser |
| Large files, repeated work, scripts | DuckDB (CLI or Python) |
| You need a persistent database file | SQLite |
| Already working in pandas | DuckDB from Python |
| Tiny files in a shell pipeline | csvkit |