CSV analysis

How to Analyze a CSV File with SQL: A Step-by-Step Walkthrough

From raw CSV export to answer: confirm the detected column types, filter and aggregate with SQL, join a lookup file, and export only the result you need.

Published 2026-05-02·Updated 2026-10-29

Why query CSV files with SQL?

CSV is easy to export but awkward to inspect once files grow beyond a few thousand rows. SQL gives you a direct way to filter, group, sort, and join CSV data without first importing it into a spreadsheet or database server.

  • Filter rows with WHERE instead of manual spreadsheet filters.
  • Summarize numeric columns with COUNT, SUM, AVG, MIN, and MAX.
  • Join multiple CSV files when IDs or names connect them.
  • Export a smaller result set after the query is finished.

Add a CSV file

  1. Open the SQL for Files editor.
  2. Drag a CSV file into the Add Data area, or choose it from your file picker.
  3. Review the detected table name in the Database sidebar.
  4. Expand the table to inspect column names before writing a query.

Advanced CSV options

If your CSV uses a custom delimiter, skipped rows, unusual quote character, or a regional decimal separator, use advanced add options before creating the table.

Check the detected column types before you trust them

CSV files only contain text, so DuckDB infers each column type while it reads the file. Most of the time the guess is right, but a date stored in an unusual format or a number with a thousands separator can arrive as VARCHAR. Checking types first saves confusing results later.

DESCRIBE sales;

DESCRIBE lists every column with its detected type. SUMMARIZE goes further and profiles each column with min, max, approximate distinct count, and null percentage, which is a fast way to spot empty columns or outliers.

SUMMARIZE sales;

When a type is wrong

If a date or number column shows up as VARCHAR, add the file again with the matching date format, decimal separator, or null string in the advanced options, or convert the column in SQL with CAST or TRY_CAST.

Start with simple queries

Begin with a small preview query so you can verify column names and row shape before running larger aggregations.

SELECT *
FROM sales
LIMIT 20;

Then filter, sort, or select only the columns that matter for your analysis.

SELECT order_date, region, revenue
FROM sales
WHERE revenue > 1000
ORDER BY revenue DESC;

Aggregate CSV rows

SQL is especially useful when you want totals by category, region, month, customer segment, or another repeated value in the CSV file.

SELECT
  region,
  COUNT(*) AS order_count,
  SUM(revenue) AS total_revenue,
  AVG(revenue) AS average_revenue
FROM sales
GROUP BY region
ORDER BY total_revenue DESC;

Join multiple CSV files

Add more than one CSV when your analysis needs lookup data, product names, account metadata, or another table that connects through a shared key.

SELECT
  orders.order_id,
  customers.customer_name,
  orders.revenue
FROM orders
JOIN customers
  ON orders.customer_id = customers.customer_id
ORDER BY orders.revenue DESC;

Keep results local and export only what you need

SQL for Files runs the CSV import and query execution in your browser with DuckDB WASM. The app does not upload your files, SQL, or result rows to a backend service.

After a query finishes, use CSV export for the result set, or save the local database in your browser if you want to keep working across sessions.

Continue in the editor

Open SQL for Files to add your own CSV, JSON, or Parquet files and try these examples locally in your browser.