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
The file contains data rows only, not a line such as:
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:
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:
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:
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
FILEprivilege - 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:
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:
No single set of options suits every tool. Some consumers expect tabs, some expect no quote escaping, and some are better served by TSV.
Related reading
- .Include vs .Load performance in EntityFramework
- incorrect resource manager data checksum in record at 2/XYZ terminating walreceiver process due to administrator command
- Incorrect string value when trying to insert UTF-8 into MySQL via JDBC?
- Increment a database field by 1
- Increment value in MySQL update query
- Index all except one item in python
- Index of a maximum element in TensorFlow tensor
- Index of Currently Selected Row in DataGridView
System design interview problems on this topic
- Design a Distributed OLTP Database: solution and walkthrough
- Design a Wide Column Database: solution and walkthrough
- Design a Key Value Store: solution and walkthrough
- Design a Web Cache: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.