Skip to main content

Numeric Data Types

Learning Focus

Use this lesson to understand Numeric Data Types in DuckDB and choose the right type for your data.

Integer Types

TypeStorageRange
TINYINT1 byte-128 to 127
SMALLINT2 bytes-32,768 to 32,767
INTEGER4 bytes-2.1B to 2.1B
BIGINT8 bytes-9.2 quintillion to 9.2 quintillion
HUGEINT16 bytesUp to 1.8 × 10^38
UBIGINT8 bytes0 to 18.4 quintillion (unsigned)
CREATE TABLE counters (
tiny_val TINYINT,
small_val SMALLINT,
int_val INTEGER,
big_val BIGINT,
huge_val HUGEINT
);

INSERT INTO counters VALUES (100, 30000, 1500000, 9000000000, 1234567890123456789);

SELECT * FROM counters;

Floating-Point Types

TypeStoragePrecision
FLOAT4 bytes~7 decimal digits
DOUBLE8 bytes~15 decimal digits
CREATE TABLE measurements (
temperature FLOAT,
latitude DOUBLE,
longitude DOUBLE
);

INSERT INTO measurements VALUES (36.6, 37.7749295, -122.4194155);
SELECT * FROM measurements;
Float Precision

Floating-point types may lose precision in financial calculations. Use DECIMAL for money.

Decimal / Numeric Types

-- DECIMAL(precision, scale)
-- precision = total significant digits
-- scale = digits after decimal point

CREATE TABLE financials (
revenue DECIMAL(18,2),
tax_rate DECIMAL(5,4)
);

INSERT INTO financials VALUES (1500000.75, 0.2100);
SELECT revenue * (1 + tax_rate) AS total FROM financials;

Special Numeric Types

-- Sequence-like behavior
CREATE SEQUENCE emp_seq START 1;
SELECT nextval('emp_seq'); -- 1
SELECT nextval('emp_seq'); -- 2

-- Bit operations
SELECT 5::INTEGER & 3; -- 1 (bitwise AND)
SELECT 5::INTEGER | 3; -- 7 (bitwise OR)

Type Casting

SELECT 42::BIGINT;
SELECT '3.14'::DOUBLE;
SELECT CAST(100 AS DECIMAL(10,2));
SELECT TRY_CAST('abc' AS INTEGER); -- returns NULL instead of error

Common Pitfalls

PitfallConsequencePrevention
Using FLOAT for currencyRounding errors in sumsUse DECIMAL(18,2) for money
INTEGER overflow on large aggregationsIncorrect resultsUse BIGINT or HUGEINT for COUNT on large tables
Implicit type coercion surprisesUnexpected query resultsUse explicit CAST() or ::type syntax

Quick Reference

SELECT 42::INTEGER, 3.14::DOUBLE, 100.50::DECIMAL(10,2);
SELECT typeof(42); -- integer
SELECT typeof(3.14); -- double
SELECT typeof(100::DECIMAL); -- decimal(18,3)

What's Next