TIL you can run SQL queries directly against CSV files as a one-liner using the default sqlite3 command line utility
Posted by jpluimers on 2025/04/24
[Wayback/Archive] One-liner for running queries against CSV files with SQLite | Simon Willison’s TILs
I figured out how to run a SQL query directly against a CSV file using thesqlite3command-line utility:sqlite3 :memory: -cmd '.mode csv' -cmd '.import taxi.csv taxi' \ 'SELECT passenger_count, COUNT(*), AVG(total_amount) FROM taxi GROUP BY passenger_count'This uses the special:memory:filename to open an in-memory database. Then it uses two-cmdoptions to turn on CSV mode and import thetaxi.csvfile into a table calledtaxi. Then it runs the SQL query.Instead of setting the mode with.modeyou can use.import -csvlike this (thanks, [Wayback/Archive] Mark Lawrence):sqlite3 :memory: -cmd '.import -csv taxi.csv taxi' \ 'SELECT passenger_count, COUNT(*), AVG(total_amount) FROM taxi GROUP BY passenger_count'
Via [Wayback/Archive] Simon Willison on Twitter: “TIL you can run SQL queries directly against CSV files as a one-liner using the default sqlite3 command line utility”
Thanks Simon!
(yes, another post in what by now apparently is a SQLite sequence)
--jeroen






Leave a comment