OUTFILE

OUTFILE in SQL is a command used within a SELECT statement to export the resultant data from a query into a file. The file is typically written directly to the server's file system.

Example

SELECT column1, column2
FROM table_name
INTO OUTFILE '/tmp/outfile.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';

Output

/path/to/yourfile.csv

Explanation

The code excerpt above shows how to output the result of a SQL query to a CSV file using MySQL’s OUTFILE function. The INTO OUTFILE clause specifies the destination of the exported CSV file, while FIELDS TERMINATED BY,ENCLOSED BY, and LINES TERMINATED BY define the CSV’s formatting. This snippet exports column1 and column2 from table_name into a CSV file named outfile.csv, located in the /tmp directory.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.