SQL is the most widely used language for asking questions of tabular data, and it works just as well on a CSV file as on a database. If you have ever built a pivot table, written a VLOOKUP or filtered a spreadsheet on three conditions, SQL will feel familiar — and it handles larger files and more complex questions with far less clicking. This tutorial uses SQL Query on CSV, which runs in your browser, so you can follow along with your own files without installing anything or uploading them anywhere.
Load a file and look at it
Upload a CSV or Excel file. It becomes a table named after the file — sales_2025.csv becomes sales_2025 — and its columns appear in the sidebar with their detected types. Start by looking at the data:
SELECT *
FROM sales
LIMIT 10;
SELECT chooses columns (* means all), FROM names the table, and LIMIT keeps the result short. Keywords are not case-sensitive; writing them in capitals is just a convention that makes queries easier to read.
Choose columns and filter rows
SELECT month, region, revenue
FROM sales
WHERE region = 'North'
AND revenue > 50000
ORDER BY revenue DESC;
- Text values go in single quotes; column names with spaces go in double quotes:
"Order Date". - Combine conditions with
ANDandOR, using brackets when you mix them. - Useful filters:
IN ('North', 'East'),BETWEEN 100 AND 500,LIKE 'Lap%'(starts with “Lap”),IS NULLfor blanks. ORDER BY … DESCsorts from highest to lowest.
Summarise with GROUP BY
This is where SQL replaces pivot tables. GROUP BY collapses rows that share a value into one row, and aggregate functions summarise each group:
SELECT region,
COUNT(*) AS orders,
SUM(revenue) AS total_revenue,
AVG(revenue) AS avg_order,
MAX(revenue) AS biggest_order
FROM sales
GROUP BY region
ORDER BY total_revenue DESC;
Every column in the SELECT list must either be in the GROUP BY or be inside an aggregate. To filter on a summary — “only regions with more than 100 orders” — use HAVING, which works like WHERE but after grouping:
SELECT region, COUNT(*) AS orders
FROM sales
GROUP BY region
HAVING COUNT(*) > 100;
Work with dates
Grouping by month is one of the most common tasks. With dates stored as YYYY-MM-DD, either of these works:
SELECT strftime('%Y-%m', order_date) AS month, SUM(revenue) AS revenue
FROM orders
GROUP BY month
ORDER BY month;
SELECT DATE_TRUNC('month', order_date) AS month, SUM(revenue) AS revenue
FROM orders
GROUP BY 1
ORDER BY 1;
GROUP BY 1 means “group by the first selected column”. Other handy date functions include YEAR(), MONTH(), date(x, '+30 days') and DATEDIFF(end_date, start_date).
Combine files with JOIN
Upload two files — say orders and customers — and match rows on a shared column. This is the SQL equivalent of VLOOKUP, but it brings across every matching column at once:
SELECT o.order_id, o.amount, c.name, c.city
FROM orders AS o
JOIN customers AS c ON o.customer_id = c.customer_id;
JOIN(inner join) keeps only rows that match in both tables.LEFT JOINkeeps every row from the first table, with blanks where there is no match — use it to find orders whose customer is missing: addWHERE c.customer_id IS NULL.- The short names
oandcare aliases that save typing and make it clear which table each column comes from.
Watch for duplicates: if a customer appears twice in the customers file, each of their orders appears twice in the result. Check keys are unique with GROUP BY customer_id HAVING COUNT(*) > 1.
Build new columns with CASE
SELECT order_id, amount,
CASE
WHEN amount >= 10000 THEN 'large'
WHEN amount >= 1000 THEN 'medium'
ELSE 'small'
END AS size
FROM orders;
Wrap that in a GROUP BY size to count orders by band — a quick way to build a distribution table.
Rankings and running totals with window functions
Window functions calculate across related rows without collapsing them. Two of the most useful:
-- Rank products within each region
SELECT region, product, revenue,
RANK() OVER (PARTITION BY region ORDER BY revenue DESC) AS rank_in_region
FROM sales;
-- Running total over time
SELECT month, revenue,
SUM(revenue) OVER (ORDER BY month) AS cumulative_revenue
FROM monthly;
LAG(revenue) OVER (ORDER BY month) returns the previous month’s value, which makes month-on-month change a simple subtraction.
Organise long queries with WITH
A common table expression names an intermediate result so the final query stays readable:
WITH monthly AS (
SELECT strftime('%Y-%m', order_date) AS month, SUM(amount) AS revenue
FROM orders
GROUP BY month
)
SELECT month, revenue,
revenue - LAG(revenue) OVER (ORDER BY month) AS change
FROM monthly
ORDER BY month;
Gotchas specific to CSV data
- Types are inferred. If a numeric column contains a stray word, it may be read as text and sort alphabetically. Check the type shown in the sidebar.
- Blanks are NULL.
WHERE x = NULLnever matches; useIS NULL. Aggregates such asAVGignore NULLs, whileCOUNT(*)counts every row. - Text comparisons are case-sensitive with
=. UseLOWER(city) = 'pune'orILIKEfor case-insensitive matching. - Integer division: some databases return 0 for 1/2. SnipFerry returns 0.5, but if you move a query to another database, write
1.0 * a / bto be safe.
When the result looks right, download it as CSV or JSON, or send it straight to the chart builder. To combine multiple CSV tables safely, read SQL Joins Explained; if you want to load the CSV into MySQL or PostgreSQL next, see How to Import a CSV File into a SQL Database.