Select columns from a CSV file using SQL

How running SQL on a CSV file works for column selection, what it can change in your data, and when a simpler column-cutting tool is the better choice.

Running a SQL query against a CSV file lets you pick the columns you need without opening a spreadsheet or writing a script. This review covers when the approach fits, what it actually does to your data, and what to check before you share the result. It does not include benchmarks or tests of specific tool versions, so confirm details against the documentation for the tool you use.

Is SQL a good fit for picking CSV columns?

SQL is a good fit when column selection is one step in a larger task. If you also need to filter rows, rename columns, sort, join two files or compute values, a single SELECT statement expresses all of it in a form you can read, save and rerun.

The main tradeoff is that most SQL tools read the CSV into a typed table before querying it. That step can quietly reinterpret values, for example by treating codes with leading zeros as numbers. A pure column cut does not have this risk.

A simpler tool is often the better choice in three cases. The first is when you only need to keep or drop columns. The second is when values must pass through exactly as written. The third is when the person receiving the result will just open it in a spreadsheet. In these cases, a dedicated column-cutting command or your spreadsheet's column delete does the job with fewer moving parts.

What the query actually changes

Selecting columns in SQL is a projection: the query returns a new result containing only the named columns, in the order you list them. The source file on disk is not modified. A new file exists only if you explicitly write the output somewhere.

The details depend on how the tool reads the file. Some query the CSV directly, while others first import it into a table. Either way, the tool has to decide four things: whether the first row is a header, which delimiter and quote character to use, what text encoding the file uses, and what data type each column has. Each of these decisions can affect what comes out, so they matter more than the SELECT statement itself.

The examples below use this sample input. The second data row contains a comma inside a quoted field, which is a common source of broken column selection when text is split naively on commas.

id,name,city,plan
1,Ana,Lisbon,basic
2,"Lee, Jordan",Austin,pro
3,Sam,Oslo,basic

Selecting name and city should produce the result below. A CSV-aware tool keeps the quotes around the field that contains a comma, so the output still has exactly two columns per row.

name,city
Ana,Lisbon
"Lee, Jordan",Austin
Sam,Oslo

Common ways to run SQL on a CSV file

Three widely used command-line options are DuckDB, the SQLite shell and the csvsql command from csvkit. They differ in how they treat the file, as summarized below.

Tool | How it reads the CSV | Type handling | Note
DuckDB | Queries the file directly by path | Detects types automatically; can be told to read everything as text | Writes results to a new CSV with COPY
SQLite shell | Imports the file into a table first | A table created by import stores columns as text | Column names come from the header row when the table is new
csvsql (csvkit) | Loads the file into a temporary table named after the file | Infers types unless inference is turned off | Prints the query result as CSV

In DuckDB, you can name the file as if it were a table and write the selected columns to a new file:

COPY (
  SELECT name, city
  FROM 'customers.csv'
) TO 'subset.csv' (HEADER, DELIMITER ',');

In the SQLite shell, you import the file first, then send the query output to a file:

.mode csv
.import customers.csv customers
.headers on
.output subset.csv
SELECT name, city FROM customers;
.output stdout

With csvkit, the table name is the file name without its extension:

csvsql --query "SELECT name, city FROM customers" customers.csv > subset.csv

Where meaning can drift

Selecting columns should not alter values, but the import step can. Check for these issues before relying on the output:

  • Leading zeros: postal codes, account numbers and IDs such as 00731 can lose their zeros if a tool infers them as integers. DuckDB's all_varchar option and csvsql's no-inference flag keep values as text.
  • Numbers stored as text: when every column is text, as in a default SQLite import, sorting compares characters, so 10 sorts before 9. Cast the column in the query if order matters.
  • Header names: names with spaces or punctuation need double quotes in SQL, as in SELECT "Sign-up Date". Duplicate or blank headers may be renamed or rejected, depending on the tool.
  • Encoding: a file that is not UTF-8, or that starts with a byte order mark, can produce garbled characters or an odd first column name. Check the first header in the output.
  • Empty values: a tool may read empty fields as NULL or as empty strings, and on output may write either. That difference matters if a later step treats them differently.

Will the output stay usable after you share it?

The output is ordinary CSV text. It can be read in any text editor and opened in spreadsheet applications, and it carries no styling that could break when copied. Reading order is simply row by row, left to right. Assistive technology users will usually encounter it through whatever spreadsheet or editor they open it in, so accessibility depends on that application rather than on the query. No screen reader or destination app was tested for this review.

Most problems after sharing come from the receiving application, not from SQL. Spreadsheet programs commonly apply their own type detection when they open or paste CSV data. That can turn long digit strings into rounded numbers, reformat date-like text, or strip leading zeros that survived the query. If exact values matter, have recipients import the file with those columns set to text, or send a short note listing which columns must not be reformatted.

Pasting query results from a terminal is less reliable than sharing the written file. Many SQL shells print aligned tables by default, and those use spaces or box-drawing characters rather than delimiters. Write to a file in CSV mode, as in the examples above, whenever the result will be reused.

Bottom line

Use SQL on a CSV file when selecting columns is part of a query you would otherwise do by hand in several steps, and when you can check how the tool typed each column. If you only need to keep a few columns exactly as written, a plain column-cutting tool is simpler and less likely to change your values. With either approach, open the output and compare a few rows against the source before you pass it on.