Skillversitas

Quickstart (First Query)

In this chapter, we'll run our first query using the DuckDB CLI and try reading data directly from a CSV file — one of DuckDB's main strengths.

Opening a Database

Open your terminal and run:

duckdb practice.duckdb

This command creates a database file named practice.duckdb in the current folder, or opens it if the file already exists. All tables you create will be stored inside this file.

The initial output will look like this:

v2.0.0
Enter ".help" for usage hints.
Connected to a transitory in-memory database.

Tip: If you want to try DuckDB without installing anything, you can open DuckDB in the browser.

Running Your First Query

In the DuckDB prompt, type the following query:

SELECT 6 * 7 AS answer;

The result is:

42

The query above shows that DuckDB is ready to accept SQL commands. Statements are terminated with a semicolon (;).

Reading a CSV File Directly

One of DuckDB's flagship features is the ability to query CSV files directly without importing them into a table first.

Save the following text as a sales.csv file in the folder where you run DuckDB:

product,quantity,price
Laptop,2,5.00
Pencil,3,1.50
Laptop,1,5.00
Notebook,5,3.00

Then run this query in DuckDB:

SELECT product, sum(quantity * price) AS revenue
FROM read_csv('sales.csv')
GROUP BY product
ORDER BY revenue DESC;

The result will display revenue per product:

product revenue
Laptop 15.00
Notebook 15.00
Pencil 4.50

DuckDB automatically detects the header and the data types of the columns from the CSV file.

Storing Data in a Table

If you want to store the CSV data as a permanent table in the database, use CREATE TABLE ... AS SELECT:

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

After that, the sales table is stored inside the practice.duckdb file and can be accessed again at any time.

Exiting the CLI

To leave the DuckDB prompt, type:

.exit

Run duckdb practice.duckdb again to open the same database and query the sales table you created.

Summary

In this chapter, we opened a DuckDB database, ran our first SQL query, read a CSV file directly with read_csv(), and stored the results in a permanent table.

In the next chapter, we'll cover how to connect to a database — whether using a file or in-memory — and how to manage connections from Python.