# Using SELECT INTO OUTFILE in MySQL

> Write MySQL query results to a file with SELECT INTO OUTFILE, and use the command-line alternative when it is not available on your account.

Source: https://helpcenter.ruachost.com/developers/databases/using-select-into-outfile-in-mysql/

The **SELECT INTO OUTFILE** statement in MySQL allows you to write the results of a query directly to a file. You can also customize column and row delimiters to format the output.

## **About SELECT INTO OUTFILE**

- Writes query results to a file on the server.
- Allows custom column and row terminators for formatting.

## **Support for SELECT INTO OUTFILE**

- **Unmanaged hosting packages:** Fully supported since you have control over MySQL privileges and configuration.
- **Shared hosting:** Not supported. The **FILE privilege** is required, which is usually disabled for security reasons.

## **Alternative to SELECT INTO OUTFILE**

If your hosting package does not allow **SELECT INTO OUTFILE**, you can still save query results to a file using the command line:

```
echo "sql_query;" | mysql --user=mysql_username --password=mysql_password mysql_database > /home/username/query.txt
```

### Explanation of parameters

- **sql_query:** The SQL statement you want to run (e.g., `SELECT * FROM employees;`).
- **mysql_username:** MySQL username for the database.
- **mysql_password:** Password for the MySQL user.
- **mysql_database:** Name of the database to access.
- **username:** Your hosting account username.

You can change the file path or query to suit your needs.

## **More Information**

Official documentation:\
<https://mariadb.com/kb/en/select-into-outfile>
