Skillversitas

Importing Data (CSV & Parquet)

One of DuckDB's main strengths is its ability to read and query data directly from external files, without importing them manually first. The most commonly used formats are CSV and Parquet.

Reading CSV Files with read_csv()

The read_csv() function lets you query a CSV file directly as if it were a table:

SELECT * FROM read_csv('sales.csv');

DuckDB automatically detects:
- The first row's header as column names
- The data type of each column (number, text, date, etc.)
- The field separator (usually a comma)

Example sales.csv file:

product,quantity,price
Laptop,2,5.00
Pencil,3,1.50
Laptop,1,5.00

Query result:

product quantity price
Laptop 2 5.00
Pencil 3 1.50
Laptop 1 5.00

Storing CSV as a Table

To permanently store the CSV data in the database, combine read_csv() with CREATE TABLE AS:

CREATE TABLE sales AS SELECT * FROM read_csv('sales.csv');

Reading Parquet Files

Parquet is an efficient columnar format for large data. DuckDB supports it natively with read_parquet():

SELECT * FROM read_parquet('data.parquet');

Just like with CSV, you can store it directly into a table:

CREATE TABLE data_parquet AS SELECT * FROM read_parquet('data.parquet');

Using COPY for Large Imports

To import large amounts of data into an existing table, use the COPY command. It is optimized for bulk loading:

COPY students FROM 'students.csv';

Note: The file name for COPY must be available on the machine running the DuckDB process.

Filtering While Reading a CSV

You can apply filters directly while reading a CSV without importing it first:

SELECT *
FROM read_csv('sales.csv')
WHERE quantity > 1;

Summary

DuckDB makes data processing easy by reading CSV and Parquet files directly via SQL. The read_csv() and read_parquet() functions are great for quick analysis, while CREATE TABLE AS and COPY are used to store data permanently.

In the next chapter, we'll learn how to retrieve data from tables using the SELECT statement with clauses such as WHERE, ORDER BY, and DISTINCT.