query
A query is a request made to a database to retrieve, modify, or analyze data.
Definition
A query is a request made to a database to retrieve, modify, or analyze data. In data analytics, queries are most commonly written in SQL and allow you to ask specific questions of your data to get meaningful answers. Think of a query like asking a librarian to find all books published after 2020 that are about data science - you're defining specific criteria to get exactly the information you need.
Structure
A typical SQL query consists of several key components that work together:
- The operation you want to perform (like
SELECTto retrieve data) - The data you want to work with (specified in the
FROMclause) - Any conditions or filters (using
WHERE) - How you want the results organized (with clauses like
GROUP BYorORDER BY)
Examples in DuckDB
Here's a simple query that retrieves all columns from a table:
Copy code
SELECT * FROM employees;
A more complex query that filters and aggregates data:
Copy code
SELECT
department,
COUNT(*) as employee_count,
AVG(salary) as avg_salary
FROM employees
WHERE hire_date >= '2020-01-01'
GROUP BY department
ORDER BY avg_salary DESC;
DuckDB supports both standard SQL queries and some extended functionality, like the ability to query directly from Parquet files:
Copy code
SELECT *
FROM 'data/employees.parquet'
WHERE salary > 50000;
Performance
DuckDB is specifically optimized for analytical queries, which typically involve scanning large amounts of data, performing aggregations, and joining tables. It uses vectorized query execution and parallel processing to make these operations particularly fast, especially compared to traditional row-oriented databases like SQLite.
Interactive Usage
Queries can be run through DuckDB's command-line interface, through programming languages like Python or R, or through various data tools that support DuckDB as a backend. The results can then be used for analysis, visualization, or further processing in your data pipeline.
Related terms
A SQL query is a structured request written in Structured Query Language (SQL) that allows you to retrieve, analyze, or manipulate data stored in a database.
analyze →Analyze is an SQL statement used in various database systems, including DuckDB, to gather statistics about tables and columns.
query engine →A query engine parses, optimizes, and executes SQL queries against data sources. Learn its core architecture, how it differs from a database, and see examples like DuckDB and Presto.
SQL analytics →SQL analytics refers to using SQL queries to analyze data and derive insights, typically working with large datasets stored in databases or data warehouses.
SQL →SQL (Structured Query Language) is the standard language for working with relational databases.
EXPLAIN clause →The EXPLAIN clause is a powerful diagnostic tool that shows how DuckDB plans to execute your SQL query.
FAQS
A query is a request to a database to retrieve, modify, or analyze data. In analytics, queries are usually written in SQL to ask specific questions of your data and get exactly the information you need.
A typical query has an operation (e.g. SELECT), the source data (FROM), optional filters (WHERE), and organization clauses like GROUP BY and ORDER BY.
Write standard SQL such as SELECT * FROM employees; in the DuckDB CLI, a client library, or MotherDuck. DuckDB also supports shortcuts like FROM employees; and SELECT * EXCLUDE (col).

