How to run SQL on an Excel spreadsheet
Most business data lives in Excel workbooks, and that’s also where analysis gets hardest to check. Run SQL over the workbook and the file stays the source of truth. Every question you ask of it is written down and easy to repeat.
Each sheet becomes a table
Open a workbook and every sheet turns into a table. The sheet name is the table name, and the first row becomes the column headers. Three sheets give you three tables, ready to query and join.
SELECT *
FROM Sales
LIMIT 20;Headers that aren’t on row one
Exported workbooks love to start with a title row, a blank row, and then the actual headers. That throws off every column name. After you open the file, glance at the column names. If they look like data, that’s what happened.
The fix is boring and reliable: delete the extra rows in the sheet and open it again. That beats burying a workaround in a query nobody will reread.
Numbers stored as text
This causes more wrong answers in spreadsheet data than anything else. A column holding "1,200" or " 340 " comes in as text, and a SUM over it either fails or skips rows.
Cast it yourself. Don’t count on the parser guessing right.
SELECT
SUM(CAST(REPLACE(TRIM(amount), ',', '') AS DOUBLE)) AS total_amount
FROM Sales;Replace VLOOKUP with a JOIN
A VLOOKUP chain across sheets is a join in disguise. It breaks when someone moves a column, and it’s painful to review. In SQL you state the relationship once.
SELECT
s.order_id,
s.amount,
p.product_name,
p.category
FROM Sales s
JOIN Products p
ON s.product_id = p.product_id;Pivot without a pivot table
Conditional sums give you the same result as a pivot table. Next month you just run the query again instead of rebuilding the pivot by hand.
SELECT
region,
SUM(CASE WHEN quarter = 'Q1' THEN revenue ELSE 0 END) AS q1,
SUM(CASE WHEN quarter = 'Q2' THEN revenue ELSE 0 END) AS q2,
SUM(CASE WHEN quarter = 'Q3' THEN revenue ELSE 0 END) AS q3,
SUM(CASE WHEN quarter = 'Q4' THEN revenue ELSE 0 END) AS q4
FROM Sales
GROUP BY region
ORDER BY region;Mix a workbook with other files
Every file you open lands in the same session. A sheet from a workbook joins straight to a CSV from another system, with no conversion needed.
Frequently asked
Can I query an .xlsx file with SQL without converting it to CSV?
Yes. The workbook is read directly and each sheet becomes a table. No conversion, no import.
What happens to formulas in the workbook?
You get the computed values, not the formula text. A column of formula results queries like any other column.
Can I join a spreadsheet sheet to a CSV file?
Yes. Open both and join them in one query.
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 query a CSV file with SQL
How to open large CSV files Excel can't handle
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