AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (SQLite 3.46.1); all outputs below are copied from that run, not written by hand.
Turn a CSV file into a database with one sqlite3 command, then answer real questions with SQL: totals per region, sales in a date range, monthly sums, and an exported report. Along the way, see why imported numbers sort wrong (they arrive as text) and how to fix it with a typed table.
The problem: You have a CSV file and a question. How much did each region sell? What happened in March? You don't need a spreadsheet or a database server for that.
The CSV file
Here's our data: nine sales, with a date, a region, a product and an amount. The first line holds the column names.
$ cat sales.csv date,region,product,amount 2026-01-14,north,keyboard,49.90 2026-01-20,south,monitor,189.00 2026-02-03,north,mouse,9.50 2026-02-11,west,monitor,179.00 2026-02-27,south,keyboard,52.00 2026-03-05,west,mouse,12.00 2026-03-09,north,monitor,199.00 2026-03-18,south,mouse,8.75 2026-03-30,west,keyboard,47.50 On Debian or Ubuntu, the command line tool comes from the package called sqlite three.
Import with one command
.import --csv reads the file into a table. If the table doesn't exist yet, SQLite creates it and takes the column names from the first line.
$ sqlite3 shop.db ".import --csv sales.csv sales" No output means it worked. The database is a single file, shop dot d b, right next to the CSV. Let's look at the table it created.
$ sqlite3 shop.db ".schema sales" CREATE TABLE IF NOT EXISTS "sales"( "date" TEXT, "region" TEXT, "product" TEXT, "amount" TEXT); Notice the column types. Every column is text, including amount. Keep that in mind.
The trap: numbers stored as text
Let's ask for the three biggest sales.
$ sqlite3 shop.db "SELECT product, amount FROM sales ORDER BY amount DESC LIMIT 3" mouse|9.50 mouse|8.75 keyboard|52.00 That's clearly wrong. A nine-euro mouse is not our biggest sale. Because the amounts are text, SQLite sorts them like words, character by character, and nine comes after one. A quick fix is to cast the value to a number inside the query.
$ sqlite3 shop.db "SELECT product, amount FROM sales ORDER BY CAST(amount AS REAL) DESC LIMIT 3" monitor|199.00 monitor|189.00 monitor|179.00 Correct now: the three monitors. But you'd have to remember the cast in every single query.
The real fix: a typed table
The better fix is to create the table yourself, with proper types, and then import into it. --skip 1 tells the import to skip the header line, since the table already has column names.
$ rm shop.db && sqlite3 shop.db "CREATE TABLE sales(date TEXT, region TEXT, product TEXT, amount REAL)" $ sqlite3 shop.db ".import --csv --skip 1 sales.csv sales" typeof shows how each value is actually stored. All nine amounts are real numbers now.
$ sqlite3 shop.db "SELECT typeof(amount), COUNT(*) FROM sales GROUP BY 1" real|9 Answering questions with GROUP BY
Now the first question: how much did each region sell? -header -column make the output readable.
$ sqlite3 -header -column shop.db "SELECT region, COUNT(*) AS orders, ROUND(SUM(amount), 2) AS total FROM sales GROUP BY region ORDER BY total DESC" region orders total ------ ------ ------ north 3 258.4 south 3 249.75 west 3 238.5 GROUP BY makes one row per region, COUNT counts the orders, and SUM adds the amounts. North sold the most.
Dates and months
Second question: what was sold in March? If dates are written year, month, day, like here, plain text comparison works.
$ sqlite3 -header -column shop.db "SELECT date, product, amount FROM sales WHERE date >= '2026-03-01' ORDER BY date" date product amount ---------- -------- ------ 2026-03-05 mouse 12.0 2026-03-09 monitor 199.0 2026-03-18 mouse 8.75 2026-03-30 keyboard 47.5 And a monthly summary. substr takes the first seven characters of the date: the year and the month.
$ sqlite3 -header -column shop.db "SELECT substr(date, 1, 7) AS month, ROUND(SUM(amount), 2) AS total FROM sales GROUP BY month" month total ------- ------ 2026-01 238.9 2026-02 240.5 2026-03 267.25 This only works because the dates use the year, month, day format. With dates like day, month, year, convert them first.
Export the result back to CSV
Finally, someone wants the result as a file. -csv prints CSV, and the shell redirects it into report dot csv.
$ sqlite3 -header -csv shop.db "SELECT product, ROUND(AVG(amount), 2) AS avg_price FROM sales GROUP BY product" > report.csv $ cat report.csv product,avg_price keyboard,49.8 monitor,189.0 mouse,10.08 A ready report you can open in any spreadsheet.
The trap, in one paragraph
My table already has column names. Which option stops .import from loading the header as a row? --skip 1 drops the first line, so you get nine sales and no header row.
Quick check
1. After a plain .import, ORDER BY amount puts a 9.50 mouse above a 189 monitor. Why?
- The CSV file is broken
- amount was imported as TEXT
- SQLite cannot sort decimals
amount was imported as TEXT: Text is sorted like words, character by character. Create a typed table, or cast the column to a number.
2. My table already has column names. Which option stops .import from loading the header as a row?
--csv--skip 1-header
--skip 1: --skip 1 drops the first line, so you get nine sales and no header row.Recap
.import --csv loads a file in one command. But check the schema: imported columns are text, and text numbers sort wrong. Create a typed table first and import with --skip 1. Then GROUP BY answers most questions, and -header -csv exports the result.
Source and reproducible setup
- Lesson page with the same quiz (and a video version): https://shellprovenai.github.io/lekcje/sqlite-csv/
- Commands, demo setup script and full test log: https://github.com/shellprovenai/terminal-tutorials/tree/main/sqlite-csv
This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.
