Skip to main content
DataToolsOnline

How to Query CSV Files with SQL

You don't need to import a CSV into a database to query it with SQL. DuckDB can read the file directly and expose it as a table, so ordinary SQL just works against it.

The file is a table, nothing more to set up

Open SQL on CSV and load a file: it's registered locally and exposed as a table named data, with its real column names and types shown before you write a single query. There's no schema to define, no CREATE TABLE statement, no import step.

A first query: group and count

SELECT country, COUNT(*) AS n
FROM data
GROUP BY country
ORDER BY n DESC;

Every value in the CSV is read as text unless a type can be inferred, so a numeric column is usable in arithmetic and comparisons right away, and a text column works with LIKE, ILIKE, string functions and everything else the SQL dialect supports.

Beyond SELECT: the full dialect

This isn't a restricted query builder — it's the real DuckDB SQL dialect. Window functions, common table expressions (WITH), joining the table against a second one you build with a CTE, date and string functions: all of it works, because the statement you type is run exactly as written. That also means an expensive query (a large self-join, an unbounded aggregation) is exactly as expensive as it would be in any other SQL tool — there's a Cancel button for exactly that case.

Preview vs. export

The result preview is capped at 1,000 rows so a large result never has to sit fully in the browser tab while you're still refining the query. Exporting re-runs your exact statement and streams the complete, uncapped result straight into a CSV file — with the same formula-injection neutralization every CSV export on this site applies, so a value like =HYPERLINK(...) in your data can't turn into a spreadsheet formula when you open the download.

Nothing leaves your browser

The SQL runs inside DuckDB compiled to WebAssembly, in your browser tab. Your file is never uploaded, and the page's Content-Security-Policy blocks any network request the query engine might attempt — verified directly, including against SQL specifically designed to try to fetch a remote URL.

Try it: SQL on CSV

Load a CSV and query it with SQL powered by DuckDB.

Related reading