liveBook · Manning

Enum

As explained in DuckDB in Action

An enum is a data type whose values are restricted to a defined set of named alternatives.

An enumeration (enum) is a data type whose values come from a fixed set of named alternatives. DuckDB stores enum values internally as integers, making them efficient for storage and processing while retaining string-like access. section 1.7.2 section 10.1.7

Create an enum for tags

A tag vocabulary can become an enum and then an enum list: CREATE TYPE tag AS enum (SELECT DISTINCT tagname FROM tags); ALTER TABLE posts ADD tagEnums tag[]; UPDATE posts SET tagEnums = tagNames WHERE posttypeid = 1;. Query membership with list_contains(tagEnums, 'java'). section 10.1.7

Listing 10.15 Creating an enum type for tags from the tags table
CREATE TYPE tag AS enum (SELECT DISTINCT tagname FROM tags);

Create an enum directly from the distinct values in a tags table. section 10.1.7

Useful enum operations include:

  • enum_code returns an enum's numeric code. section 10.1.7
  • enum_range returns all enum values; enum_first, enum_last, and enum_range_boundary inspect its boundaries. section 10.1.7
  • DuckDB automatically casts enums to strings for string functions and comparisons. section 10.1.7

How DuckDB in Action covers it

How deep it goes
Explains integer storage, creation, conversion, functions, and query comparisons, but does not detail execution internals.
Examples
Demonstrates weekday values, tag vocabularies, enum lists, membership filters, and top-tag aggregation queries.
What it assumes
Assumes basic SQL tables and columns while introducing enums alongside lists, maps, structs, and traditional data types.
What you can do after
Apply it via DuckDB SQL to model categorical values, convert string columns, and query large tag datasets efficiently.
How it connects
Connects enums with lists, strings, structured data types, SQL casting, and analytical query optimization.

Also explained in

Compare how 23 books explain enum →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.