CARVIEW |
- Installation
- Documentation
- Getting Started
- Connect
- Data Import
- Overview
- Data Sources
- CSV Files
- JSON Files
- Overview
- Creating JSON
- Loading JSON
- Writing JSON
- JSON Type
- JSON Functions
- Format Settings
- Installing and Loading
- SQL to / from JSON
- Caveats
- Multiple Files
- Parquet Files
- Partitioning
- Appender
- INSERT Statements
- Client APIs
- Overview
- ADBC
- C
- Overview
- Startup
- Configuration
- Query
- Data Chunks
- Vectors
- Values
- Types
- Prepared Statements
- Appender
- Table Functions
- Replacement Scans
- API Reference
- C++
- CLI
- Dart
- Go
- Java (JDBC)
- Julia
- Node.js (Deprecated)
- Node.js (Neo)
- ODBC
- PHP
- Python
- Overview
- Data Ingestion
- Conversion between DuckDB and Python
- DB API
- Relational API
- Function API
- Types API
- Expression API
- Spark API
- API Reference
- Known Python Issues
- R
- Rust
- Swift
- Wasm
- SQL
- Introduction
- Statements
- Overview
- ANALYZE
- ALTER TABLE
- ALTER VIEW
- ATTACH and DETACH
- CALL
- CHECKPOINT
- COMMENT ON
- COPY
- CREATE INDEX
- CREATE MACRO
- CREATE SCHEMA
- CREATE SECRET
- CREATE SEQUENCE
- CREATE TABLE
- CREATE VIEW
- CREATE TYPE
- DELETE
- DESCRIBE
- DROP
- EXPORT and IMPORT DATABASE
- INSERT
- LOAD / INSTALL
- PIVOT
- Profiling
- SELECT
- SET / RESET
- SET VARIABLE
- SUMMARIZE
- Transaction Management
- UNPIVOT
- UPDATE
- USE
- VACUUM
- Query Syntax
- SELECT
- FROM and JOIN
- WHERE
- GROUP BY
- GROUPING SETS
- HAVING
- ORDER BY
- LIMIT and OFFSET
- SAMPLE
- Unnesting
- WITH
- WINDOW
- QUALIFY
- VALUES
- FILTER
- Set Operations
- Prepared Statements
- Data Types
- Overview
- Array
- Bitstring
- Blob
- Boolean
- Date
- Enum
- Interval
- List
- Literal Types
- Map
- NULL Values
- Numeric
- Struct
- Text
- Time
- Timestamp
- Time Zones
- Union
- Typecasting
- Expressions
- Overview
- CASE Expression
- Casting
- Collations
- Comparisons
- IN Operator
- Logical Operators
- Star Expression
- Subqueries
- TRY
- Functions
- Overview
- Aggregate Functions
- Array Functions
- Bitstring Functions
- Blob Functions
- Date Format Functions
- Date Functions
- Date Part Functions
- Enum Functions
- Interval Functions
- Lambda Functions
- List Functions
- Map Functions
- Nested Functions
- Numeric Functions
- Pattern Matching
- Regular Expressions
- Struct Functions
- Text Functions
- Time Functions
- Timestamp Functions
- Timestamp with Time Zone Functions
- Union Functions
- Utility Functions
- Window Functions
- Constraints
- Indexes
- Meta Queries
- DuckDB's SQL Dialect
- Overview
- Indexing
- Friendly SQL
- Keywords and Identifiers
- Order Preservation
- PostgreSQL Compatibility
- SQL Quirks
- Samples
- Configuration
- Extensions
- Overview
- Installing Extensions
- Advanced Installation Methods
- Distributing Extensions
- Versioning of Extensions
- Troubleshooting of Extensions
- Core Extensions
- Overview
- AutoComplete
- Avro
- AWS
- Azure
- Delta
- DuckLake
- Encodings
- Excel
- Full Text Search
- httpfs (HTTP and S3)
- Iceberg
- Overview
- Iceberg REST Catalogs
- Amazon S3 Tables
- Amazon SageMaker Lakehouse (AWS Glue)
- Troubleshooting
- ICU
- inet
- jemalloc
- MySQL
- PostgreSQL
- Spatial
- SQLite
- TPC-DS
- TPC-H
- UI
- VSS
- Guides
- Overview
- Data Viewers
- Database Integration
- File Formats
- Overview
- CSV Import
- CSV Export
- Directly Reading Files
- Excel Import
- Excel Export
- JSON Import
- JSON Export
- Parquet Import
- Parquet Export
- Querying Parquet Files
- File Access with the file: Protocol
- Network and Cloud Storage
- Overview
- HTTP Parquet Import
- S3 Parquet Import
- S3 Parquet Export
- S3 Iceberg Import
- S3 Express One
- GCS Import
- Cloudflare R2 Import
- DuckDB over HTTPS / S3
- Fastly Object Storage Import
- Meta Queries
- Describe Table
- EXPLAIN: Inspect Query Plans
- EXPLAIN ANALYZE: Profile Queries
- List Tables
- Summarize
- DuckDB Environment
- ODBC
- Performance
- Overview
- Environment
- Import
- Schema
- Indexing
- Join Operations
- File Formats
- How to Tune Workloads
- My Workload Is Slow
- Benchmarks
- Working with Huge Databases
- Python
- Installation
- Executing SQL
- Jupyter Notebooks
- marimo Notebooks
- SQL on Pandas
- Import from Pandas
- Export to Pandas
- Import from Numpy
- Export to Numpy
- SQL on Arrow
- Import from Arrow
- Export to Arrow
- Relational API on Pandas
- Multiple Python Threads
- Integration with Ibis
- Integration with Polars
- Using fsspec Filesystems
- SQL Editors
- SQL Features
- Snippets
- Creating Synthetic Data
- Dutch Railway Datasets
- Sharing Macros
- Analyzing a Git Repository
- Importing Duckbox Tables
- Copying an In-Memory Database to a File
- Troubleshooting
- Glossary of Terms
- Browsing Offline
- Operations Manual
- Overview
- DuckDB's Footprint
- Logging
- Securing DuckDB
- Non-Deterministic Behavior
- Limits
- Development
- DuckDB Repositories
- Profiling
- Building DuckDB
- Overview
- Build Configuration
- Building Extensions
- Android
- Linux
- macOS
- Raspberry Pi
- Windows
- Python
- R
- Troubleshooting
- Unofficial and Unsupported Platforms
- Benchmark Suite
- Testing
- Internals
- Why DuckDB
- Code of Conduct
- Release Calendar
- Roadmap
- Sitemap
- Live Demo
DuckDB's SQL dialect closely follows the conventions of the PostgreSQL dialect. The few exceptions to this are listed on this page.
Floating-Point Arithmetic
DuckDB and PostgreSQL handle floating-point arithmetic differently for division by zero. DuckDB conforms to the IEEE Standard for Floating-Point Arithmetic (IEEE 754) for both division by zero and operations involving infinity values. PostgreSQL returns an error for division by zero but aligns with IEEE 754 for handling infinity values. To show the differences, run the following SQL queries:
SELECT 1.0 / 0.0 AS x;
SELECT 0.0 / 0.0 AS x;
SELECT -1.0 / 0.0 AS x;
SELECT 'Infinity'::FLOAT / 'Infinity'::FLOAT AS x;
SELECT 1.0 / 'Infinity'::FLOAT AS x;
SELECT 'Infinity'::FLOAT - 'Infinity'::FLOAT AS x;
SELECT 'Infinity'::FLOAT - 1.0 AS x;
Expression | PostgreSQL | DuckDB | IEEE 754 |
---|---|---|---|
1.0 / 0.0 | error | Infinity | Infinity |
0.0 / 0.0 | error | NaN | NaN |
-1.0 / 0.0 | error | -Infinity | -Infinity |
'Infinity' / 'Infinity' | NaN | NaN | NaN |
1.0 / 'Infinity' | 0.0 | 0.0 | 0.0 |
'Infinity' - 'Infinity' | NaN | NaN | NaN |
'Infinity' - 1.0 | Infinity | Infinity | Infinity |
Division on Integers
When computing division on integers, PostgreSQL performs integer division, while DuckDB performs float division:
SELECT 1 / 2 AS x;
PostgreSQL returns 0
, while DuckDB returns 0.5
.
To perform integer division in DuckDB, use the //
operator:
SELECT 1 // 2 AS x;
This returns 0
.
UNION
of Boolean and Integer Values
The following query fails in PostgreSQL but successfully completes in DuckDB:
SELECT true AS x
UNION
SELECT 2;
PostgreSQL returns an error:
ERROR: UNION types boolean and integer cannot be matched
DuckDB performs an enforced cast, therefore, it completes the query and returns the following:
x |
---|
1 |
2 |
Implicit Casting on Equality Checks
DuckDB performs implicit casting on equality checks, e.g., converting strings to numeric and boolean values. Therefore, there are several instances, where PostgreSQL throws an error while DuckDB successfully computes the result:
Expression | PostgreSQL | DuckDB |
---|---|---|
'1.1' = 1 | error | true |
'1.1' = 1.1 | true | true |
1 = 1.1 | false | false |
true = 'true' | true | true |
true = 1 | error | true |
'true' = 1 | error | error |
Case Sensitivity for Quoted Identifiers
PostgreSQL is case-insensitive. The way PostgreSQL achieves case insensitivity is by lowercasing unquoted identifiers within SQL, whereas quoting preserves case, e.g., the following command creates a table named mytable
but tries to query for MyTaBLe
because quotes preserve the case.
CREATE TABLE MyTaBLe (x INTEGER);
SELECT * FROM "MyTaBLe";
ERROR: relation "MyTaBLe" does not exist
PostgreSQL does not only treat quoted identifiers as case-sensitive, PostgreSQL treats all identifiers as case-sensitive, e.g., this also does not work:
CREATE TABLE "PreservedCase" (x INTEGER);
SELECT * FROM PreservedCase;
ERROR: relation "preservedcase" does not exist
Therefore, case-insensitivity in PostgreSQL only works if you never use quoted identifiers with different cases.
For DuckDB, this behavior was problematic when interfacing with other tools (e.g., Parquet, Pandas) that are case-sensitive by default – since all identifiers would be lowercased all the time. Therefore, DuckDB achieves case insensitivity by making identifiers fully case insensitive throughout the system but preserving their case.
In DuckDB, the scripts above complete successfully:
CREATE TABLE MyTaBLe (x INTEGER);
SELECT * FROM "MyTaBLe";
CREATE TABLE "PreservedCase" (x INTEGER);
SELECT * FROM PreservedCase;
SELECT table_name FROM duckdb_tables();
table_name |
---|
MyTaBLe |
PreservedCase |
PostgreSQL's behavior of lowercasing identifiers is accessible using the preserve_identifier_case
option:
SET preserve_identifier_case = false;
CREATE TABLE MyTaBLe (x INTEGER);
SELECT table_name FROM duckdb_tables();
table_name |
---|
mytable |
However, the case insensitive matching in the system for identifiers cannot be turned off.
Using Double Equality Sign for Comparison
DuckDB supports both =
and ==
for quality comparison, while PostgreSQL only supports =
.
SELECT 1 == 1 AS t;
DuckDB returns true
, while PostgreSQL returns:
postgres=# SELECT 1 == 1 AS t;
ERROR: operator does not exist: integer == integer
LINE 1: SELECT 1 == 1 AS t;
Note that the use of ==
is not encouraged due to its limited portability.
Vacuuming Tables
In PostgreSQL, the VACUUM
statement garbage collects tables and analyzes tables.
In DuckDB, the VACUUM
statement is only used to rebuild statistics.
For instruction on reclaiming space, refer to the “Reclaiming space” page.
Strings
Since version 1.3.0, DuckDB escapes characters such as '
in strings serialized in nested data structures.
PostgreSQL does not do this.
For an example, run:
SELECT ARRAY[''''];
PostgreSQL returns:
{'}
DuckDB returns:
['\'']
Functions
regexp_extract
Function
Unlike PostgreSQL's regexp_substr
function, DuckDB's regexp_extract
returns empty strings instead of NULL
s when there is no match.
to_date
Function
DuckDB does not support the to_date
PostgreSQL date formatting function.
Instead, please use the strptime
function.
Resolution of Type Names in the Schema
For CREATE TABLE
statements, DuckDB attempts to resolve type names in the schema where a table is created. For example:
CREATE SCHEMA myschema;
CREATE TYPE myschema.mytype AS ENUM ('as', 'df');
CREATE TABLE myschema.mytable (v mytype);
PostgreSQL returns an error on the last statement:
ERROR: type "mytype" does not exist
LINE 1: CREATE TABLE myschema.mytable (v mytype);
DuckDB runs the statement and creates the table successfully, confirmed by the following query:
DESCRIBE myschema.mytable;
column_name | column_type | null | key | default | extra |
---|---|---|---|---|---|
v | ENUM('as', 'df') | YES | NULL | NULL | NULL |
Exploiting Functional Dependencies for GROUP BY
PostgreSQL can exploit functional dependencies, such as i -> j
in the following query:
CREATE TABLE tbl (i INTEGER, j INTEGER, PRIMARY KEY (i));
SELECT j
FROM tbl
GROUP BY i;
PostgreSQL runs the query.
DuckDB fails:
Binder Error:
column "j" must appear in the GROUP BY clause or must be part of an aggregate function.
Either add it to the GROUP BY list, or use "ANY_VALUE(j)" if the exact value of "j" is not important.
To work around this, add the other attributes or use the GROUP BY ALL
clause.
Behavior of Regular Expression Match Operators
PostgreSQL supports the POSIX regular expression matching operators ~
(case-sensitive partial regex matching) and ~*
(case-insensitive partial regex matching) as well as their negated variants, !~
and !~*
, respectively.
In DuckDB, ~
is equivalent to regexp_full_match
and !~
is equivalent to NOT regexp_full_match
.
The operators ~*
and !~*
are not supported.
The table below shows that the correspondence between these functions in PostgreSQL and DuckDB is almost non-existent. We recommend avoiding the POSIX regular expression matching operators in DuckDB.
Expression | PostgreSQL | DuckDB |
---|---|---|
'aaa' ~ '(a|b)' |
true | false |
'AAA' ~* '(a|b)' |
true | error |
'aaa' !~ '(a|b)' |
false | true |
'AAA' !~* '(a|b)' |
false | error |
About this page
In this article
- Floating-Point Arithmetic
- Division on Integers
- UNION of Boolean and Integer Values
- Implicit Casting on Equality Checks
- Case Sensitivity for Quoted Identifiers
- Using Double Equality Sign for Comparison
- Vacuuming Tables
- Strings
- Functions
- Resolution of Type Names in the Schema
- Exploiting Functional Dependencies for GROUP BY
- Behavior of Regular Expression Match Operators