MySQL SELECT INTO OUTFILE headers SQL query database

How to include column headers with MySQL SELECT INTO OUTFILE

Codemia Knowledge Hub ·

SELECT ... INTO OUTFILE writes rows only, with no column headers. To get a header row, add one yourself with UNION ALL, or export with a client-side tool such as the mysql client, which prints column names.

What INTO OUTFILE writes by default

sql
1SELECT id, name, email
2INTO OUTFILE '/tmp/users.csv'
3FIELDS TERMINATED BY ','
4OPTIONALLY ENCLOSED BY '"'
5LINES TERMINATED BY '\n'
6FROM users;

The file contains data rows only, not a line such as:

text
id,name,email
...

There is no built-in "include headers" switch.

Adding a header row with UNION ALL

Union a row of literal column names with the real result set:

sql
1(SELECT 'id', 'name', 'email')
2UNION ALL
3(SELECT
4    CAST(id AS CHAR),
5    name,
6    email
7 FROM users)
8INTO OUTFILE '/tmp/users.csv'
9FIELDS TERMINATED BY ','
10OPTIONALLY ENCLOSED BY '"'
11LINES TERMINATED BY '\n';

The output is a header row followed by the data.

The CAST on id matters. A UNION combines its parts into one result shape, and the first row is text literals, so casting numeric or date columns to character types avoids implicit conversions and keeps the output predictable.

Formatting mixed column types

With integers, dates and nullable fields, format each value deliberately:

sql
1(SELECT 'order_id', 'customer', 'created_at', 'total')
2UNION ALL
3(SELECT
4    CAST(order_id AS CHAR),
5    customer_name,
6    DATE_FORMAT(created_at, '%Y-%m-%d %H:%i:%s'),
7    CAST(total AS CHAR)
8 FROM orders)
9INTO OUTFILE '/tmp/orders.csv'
10FIELDS TERMINATED BY ','
11OPTIONALLY ENCLOSED BY '"'
12LINES TERMINATED BY '\n';

This beats relying on MySQL's default text representation, especially when another system will read the CSV.

Exporting with headers from the mysql client

If the server does not have to write the file, a client-side export is simpler because headers come for free:

bash
mysql -u app -p --batch --raw \
  -e "SELECT id, name, email FROM users" mydb > users.tsv

The mysql client prints a header row unless you pass -N or --skip-column-names. The file is written by the client, so paths and permissions are those of the client machine.

Server-side restrictions on INTO OUTFILE

The file is written on the database server host, not on your laptop or application host unless they are the same machine. People often look for /tmp/users.csv on the wrong machine.

You also need:

  • the MySQL FILE privilege
  • a path allowed by secure_file_priv
  • a file name that does not exist yet, because MySQL will not overwrite an existing file (the query fails until you remove or rename it)

Check the allowed export directory:

sql
SHOW VARIABLES LIKE 'secure_file_priv';

If exports are restricted to one directory, the outfile path must be inside it.

Choosing CSV options

If values may contain commas, quotes or newlines, choose delimiter and quoting options that match what the consumer expects. A common CSV pattern:

sql
1FIELDS TERMINATED BY ','
2OPTIONALLY ENCLOSED BY '"'
3ESCAPED BY '"'
4LINES TERMINATED BY '\n'

No single set of options suits every tool. Some consumers expect tabs, some expect no quote escaping, and some are better served by TSV.


Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design