OUTFILE is a MySQL and MariaDB clause used with SELECT ... INTO OUTFILE and LOAD DATA OUTFILE statements. In SELECT ... INTO OUTFILE it streams the query's result set to a file that resides on the database server's filesystem. The file is created by the server process, so the path must be local to the server, writable by the server OS user, and not already exist (unless the OVERWRITE option in MySQL 8.0.22+ is used). Users must possess the FILE privilege. By default rows are written with tab-separated columns and newline-terminated rows, but export_options let you change field and line delimiters, quoting rules, and whether to generate an optional header row. The clause is frequently paired with CHARACTER SET to control encoding. The complementary LOAD DATA [LOCAL] INFILE clause can later re-import the produced file. OUTFILE happens entirely server-side, making it faster than client-side export for large datasets but requiring careful path and permission management. It is not part of the ANSI SQL standard and is unsupported in PostgreSQL, SQL Server, Oracle, or SQLite.
column_list
(list) - one or more columns or expressions to export'file_path'
(string) - destination file created on the server hostCHARACTER SET
(keyword) - optional target character set for the output fileexport_options
(list) - optional clauses such asSELECT INTO DUMPFILE, LOAD DATA INFILE, FILE privilege, LOCAL keyword, secure_file_priv system variable
MySQL 3.23
It always writes on the server host. To obtain the file locally you must copy or download it after creation.
The executing account needs the global FILE privilege, otherwise MySQL returns an access denied error.
Yes if you append the OVERWRITE keyword (MySQL 8.0.22+). Older versions require manually deleting the file first.
Either UNION ALL a literal header row with the result set or, in MySQL 8.0.20+, use the HEADER export option.