Guide

How to query a CSV file with SQL

A CSV is already a table. It has rows, columns, and mostly consistent types. What stops people from querying it is the import step: set up a database, define a schema, write a loader. You can skip all of that and query the file where it sits.

Why not just use a spreadsheet?

Spreadsheets are great for small data you edit by hand. They’re bad at questions you ask more than once. A nested IF across 200,000 rows is slow to write, slow to recalculate, and nobody can review it next month. In SQL the same question is one statement you can read and run again when the file changes.

The usual pushback is setup. Installing Postgres to answer one question is overkill, and that’s fair. It stops being a problem once the engine can read the file directly.

Look at the schema before you write anything

Check what the parser decided about your columns. Trailing spaces in headers, numbers stored as text, and ambiguous dates cause more wrong answers than bad SQL ever does.

The Data Profile view in NoEgress shows each column’s type, null count, and distinct values. Look at it before you trust a total. If "revenue" came in as text, 9 sorts above 100 and nothing warns you.

Start with SELECT and a LIMIT

Look at a few real rows first. You’ll be typing these column names for the rest of the session, so get them right now.

SELECT *
FROM orders
LIMIT 20;

Aggregate with GROUP BY

Most questions about a CSV are totals per category, counts per month, or averages per region. They all take the same shape of query.

SELECT
  region,
  COUNT(*)          AS order_count,
  SUM(revenue)      AS total_revenue,
  AVG(revenue)      AS avg_order_value
FROM orders
WHERE order_date >= DATE '2026-01-01'
GROUP BY region
ORDER BY total_revenue DESC;

Combine two files with a JOIN

Open both files and each one becomes a table. The lookup you’d build from a fragile chain of VLOOKUPs turns into a plain join.

A LEFT JOIN keeps every row from the first file, matched or not. Filter on the empty side and you have the quickest answer to "which IDs are missing?"

SELECT
  o.order_id,
  o.revenue,
  c.company_name
FROM orders o
LEFT JOIN customers c
  ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL;

Clean as you query, not before

Do your casting and trimming inside the query and leave the original file alone. Anyone reading the SQL can see exactly what you changed. A cleaned copy of the file hides that.

SELECT
  TRIM(customer_name)             AS customer_name,
  CAST(revenue AS DOUBLE)         AS revenue,
  CAST(order_date AS DATE)        AS order_date
FROM orders
WHERE revenue IS NOT NULL;

Where the query runs

NoEgress runs DuckDB compiled to WebAssembly. The engine loads into your browser tab, and the browser reads the file straight from your disk. Nothing gets uploaded. That matters when the CSV holds customer records, salaries, or anything else you wouldn’t paste into a random website.

Frequently asked

Do I need to install a database to run SQL on a CSV?

No. DuckDB reads the file directly. In NoEgress it runs inside the browser tab, so there’s nothing to install and no import step.

Which SQL dialect does it use?

DuckDB SQL, which is very close to PostgreSQL. SELECT, GROUP BY, JOIN, window functions, and CTEs all work the way you’d expect.

Can I query two CSV files together?

Yes. Open both and join them in one query.

Try it on your own file

More guides

NoEgress vs Excel, Power Query, ChatGPT and DuckDB

How to use ChatGPT-style AI on sensitive spreadsheets without uploading them

An Excel alternative for files with millions of rows

How to open large CSV files Excel can't handle

How to run SQL on an Excel spreadsheet

How to analyze confidential data without uploading it

How to check if a website uploads your file

How to compare two CSV files and find what changed

Data quality checks to run before trusting a dataset

How to query a JSON file with SQL

DuckDB online: run DuckDB in your browser with nothing to install

How to ask a local AI model about your CSV or Excel data

How to remove duplicate rows from a CSV or Excel file

How to split a CSV or Excel file into multiple files by column

How to convert CSV to Excel, and Excel to CSV or Parquet

How to merge two CSV or Excel files