COPY TO
Overview
The COPY TO
statement is used to export tables, specific columns, or results of select queries into .csv files. It allows you to copy data from a table or query result and save it to a specified file.
Syntax
The syntax for COPY TO
is as follows:
Parameters in the syntax include:
-
table_name
: Table with the data to export. -
column_name
: Optional. Specify columns for export. -
query
: ASELECT
statement for exporting specific results. -
filename
: File name for saving the exported data. -
option
: Optional parameters for customization.
Example
Step #1: Create a Table
- Before creating The table, check for duplicate tables using the following statement:
- You will receive a list of existing tables in Oxla:
- Now, let’s create a table for exporting data to a CSV file. Here, we’ll create a “salary” table:
- The table and data were created successfully.
Step #2: Copy the Table into the CSV File
- By default, the
COPY TO
command overwrites the CSV file if it already exists. - Please ensure that the directory where you save the file has the necessary write permissions.
Option 1: Exporting all columns from a table
Copy all columns in the table to the specified CSV file:
You will get the following successful result:
The data from the table will be exported to the CSV file.
Option 2: Exporting specific columns from a table
Copy only specific columns by specifying the column names in the query:
You will get the following successful result:
The data from the specified columns will be exported to the CSV file.
Option 3: Exporting results of a SELECT statement
In the example below, copy data only from the Marketing department using the SELECT
statement and WHERE
clause:
You will get the following successful result:
Data exported to CSV file is only from the Marketing department.