Overview
Introduction
Sometimes filtering or sorting a CSV by hand isn't enough, you want to GROUP BY a column, JOIN two datasets, or run an aggregate, the kind of question SQL answers naturally.
Query CSV with SQL lets you do exactly that: paste a CSV, write a real SQL query against it, and see the results as a table, all without installing a database.
What Is Query CSV with SQL?
An in-browser SQL query tool for CSV data, built on alasql, a JavaScript SQL engine that can query plain in-memory arrays of objects as if they were database tables.
Rather than naming your CSV as a table you have to CREATE first, it's passed to the query as a single positional parameter, referenced in your SQL text with a ? placeholder, similar to a parameterized query in any SQL client.
How Query CSV with SQL Works
The pasted CSV is parsed with this site's shared grid parser, then converted into an array of plain row objects keyed by the header row, exactly the shape alasql expects for querying an in-memory table.
Your SQL text and that row array are passed to alasql(sql, [rows]), which binds the array to the ? placeholder in your query and returns the result as an array of row objects, rendered below as a table.
When To Use Query CSV with SQL
Use this whenever a CSV question is naturally a SQL question, top N rows by some column, grouping and counting, filtering on multiple conditions, joining two small datasets together.
It's also a fast way to sanity-check a SQL query you're planning to run somewhere else, against a small CSV sample, before pointing it at a real database.
Often used alongside CSV Editor and CSV Viewer.
Features
Advantages
- Real SQL, not a simplified filter builder, GROUP BY, JOIN, ORDER BY, and aggregate functions all work.
- No setup: no database to install, no table schema to define, just paste and query.
- Runs entirely in the browser, so nothing about your CSV data is sent anywhere.
Limitations
- Every value from the CSV starts out as a string, so numeric comparisons in WHERE clauses may need an explicit cast depending on how alasql infers types for your query.
- Very large CSVs held as an in-memory JavaScript array can be slower than a real database for complex queries.
Examples
Best Practices & Notes
Best Practices
- Start with SELECT * FROM ? to confirm your CSV parsed the way you expect before adding WHERE or GROUP BY clauses.
- Use column aliases (SELECT name AS "Full Name") to make the results table's headers more readable.
- Remember every value from `parseCsvGrid` starts out as a string, wrap a column in a numeric cast if you need to sort or compare it numerically rather than lexically.
Developer Notes
queryCsvWithSql in query-csv-with-sql.ts parses the CSV with parseCsvGrid, converts the header-plus-rows grid into Record<string, string>[] objects, then calls alasql(sql, [rows]) inside a try/catch (alasql throws synchronously on malformed SQL). The result columns are read from Object.keys(rows[0]) when at least one row comes back, otherwise an empty column list is treated as a valid zero-row result rather than an error.
Query CSV with SQL Use Cases
- Filtering and sorting a CSV export with real WHERE and ORDER BY clauses instead of manual column tricks
- Grouping and aggregating CSV data, counts, sums, averages, per category with GROUP BY
- Sanity-checking a SQL query against a small CSV sample before running it against a production database
Common Mistakes
- Referencing the CSV by a table name instead of ?, alasql needs the positional placeholder here since the data is passed as a parameter, not a pre-declared table.
- Assuming numeric columns are already numbers, every CSV cell starts out as a string, so a WHERE clause comparing against a number may need an explicit CAST or conversion depending on your query.
Tips
- Use CSV Viewer first if you just want to sort and eyeball the data, save this tool for when you actually need a WHERE, GROUP BY, or JOIN.
- Pair with CSV Editor if a query reveals rows you want to fix directly, rather than just filter out.