Bird
Raised Fist0
PostgreSQLquery~20 mins

ENUM types in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
ENUM Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this ENUM insertion query?
Given the ENUM type mood with values ('happy', 'sad', 'neutral'), what will be the result of this insertion?
PostgreSQL
CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');
CREATE TABLE person (id SERIAL PRIMARY KEY, name TEXT, current_mood mood);
INSERT INTO person (name, current_mood) VALUES ('Alice', 'happy');
SELECT * FROM person;
A[{"id":1,"name":"Alice","current_mood":"happy"}]
BSyntaxError: ENUM type not recognized
CError: invalid input value for enum mood
D[{"id":1,"name":"Alice","current_mood":null}]
Attempts:
2 left
💡 Hint
Remember ENUM values must be one of the predefined set.
🧠 Conceptual
intermediate
1:30remaining
Which statement about ENUM types in PostgreSQL is true?
Choose the correct statement about ENUM types in PostgreSQL.
AENUM types can be altered to add new values anywhere in the list without restrictions.
BENUM types allow any string value without restrictions once created.
CENUM types store values as integers internally and enforce allowed values.
DENUM types are not supported in PostgreSQL.
Attempts:
2 left
💡 Hint
Think about how ENUM restricts values and stores them.
📝 Syntax
advanced
2:00remaining
Which option correctly adds a new value 'excited' to an existing ENUM type 'mood'?
You have an ENUM type mood with values ('happy', 'sad', 'neutral'). Which SQL command correctly adds 'excited' as a new value?
AALTER TYPE mood ADD VALUE 'excited';
BALTER ENUM mood ADD 'excited';
CALTER TYPE mood ADD 'excited' AFTER 'happy';
DALTER TYPE mood ADD VALUE 'excited' AFTER 'happy';
Attempts:
2 left
💡 Hint
Check the exact syntax for adding a value at a specific position.
optimization
advanced
1:30remaining
What is a key advantage of using ENUM types over VARCHAR for fixed sets of values?
Why might you choose an ENUM type instead of VARCHAR for a column with limited possible values?
AENUM types use less storage and improve query performance by storing values as integers internally.
BENUM types allow any string value, so they are more flexible than VARCHAR.
CENUM types automatically index the column without extra commands.
DENUM types do not enforce value restrictions, making inserts faster.
Attempts:
2 left
💡 Hint
Think about storage and validation differences.
🔧 Debug
expert
2:30remaining
Why does this INSERT fail with an error?
Given the ENUM type status with values ('new', 'in_progress', 'done'), why does this query fail? INSERT INTO tasks (id, status) VALUES (1, 'completed');
PostgreSQL
CREATE TYPE status AS ENUM ('new', 'in_progress', 'done');
CREATE TABLE tasks (id INT PRIMARY KEY, status status);
INSERT INTO tasks (id, status) VALUES (1, 'completed');
ASyntax error due to missing quotes around 'completed'.
BError because 'completed' is not a valid value in the ENUM 'status'.
CError because the table 'tasks' does not exist.
DNo error; the row is inserted with 'completed' as status.
Attempts:
2 left
💡 Hint
Check if the inserted value matches the ENUM allowed values.

Practice

(1/5)
1. What is the main purpose of using ENUM types in PostgreSQL?
easy
A. To restrict a column to a fixed set of allowed values
B. To store large text data efficiently
C. To create temporary tables
D. To index numeric columns faster

Solution

  1. Step 1: Understand ENUM type purpose

    ENUM types define a list of allowed values for a column, ensuring data consistency.
  2. Step 2: Compare with other options

    Other options describe unrelated features like text storage, temporary tables, or indexing.
  3. Final Answer:

    To restrict a column to a fixed set of allowed values -> Option A
  4. Quick Check:

    ENUM = fixed allowed values [OK]
Hint: ENUM limits values to a fixed list, ensuring consistency [OK]
Common Mistakes:
  • Thinking ENUM stores large text data
  • Confusing ENUM with temporary tables
  • Assuming ENUM improves indexing speed
2. Which of the following is the correct syntax to create an ENUM type named mood with values 'happy', 'sad', and 'neutral'?
easy
A. CREATE ENUM TYPE mood AS ('happy', 'sad', 'neutral');
B. CREATE ENUM mood ('happy', 'sad', 'neutral');
C. CREATE TYPE mood ENUM ['happy', 'sad', 'neutral'];
D. CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral');

Solution

  1. Step 1: Recall ENUM creation syntax

    The correct syntax is CREATE TYPE name AS ENUM (values); with values in parentheses and single quotes.
  2. Step 2: Check each option

    CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); matches the correct syntax exactly. Others have wrong keywords or brackets.
  3. Final Answer:

    CREATE TYPE mood AS ENUM ('happy', 'sad', 'neutral'); -> Option D
  4. Quick Check:

    CREATE TYPE ... AS ENUM (values) [OK]
Hint: Use CREATE TYPE name AS ENUM (values) syntax [OK]
Common Mistakes:
  • Using CREATE ENUM instead of CREATE TYPE
  • Using square brackets instead of parentheses
  • Omitting AS keyword
3. Given the ENUM type and table below, what will be the result of the query?
CREATE TYPE colors AS ENUM ('red', 'green', 'blue');
CREATE TABLE items (id SERIAL PRIMARY KEY, color colors);
INSERT INTO items (color) VALUES ('green'), ('blue'), ('red');

SELECT color FROM items ORDER BY color;
medium
A. red, blue, green
B. blue, green, red
C. green, blue, red
D. red, green, blue

Solution

  1. Step 1: Understand ENUM ordering

    ENUM values are ordered by their declaration order: 'red' < 'green' < 'blue'.
  2. Step 2: Apply ORDER BY on color column

    Ordering by color sorts rows as per ENUM order, so 'red', 'green', 'blue'.
  3. Final Answer:

    red, green, blue -> Option A
  4. Quick Check:

    ENUM order = declaration order [OK]
Hint: ENUM sorts by declared order, not alphabetically [OK]
Common Mistakes:
  • Assuming alphabetical order instead of ENUM order
  • Confusing insertion order with sort order
  • Expecting default text sorting
4. What is wrong with the following SQL code?
CREATE TYPE status AS ENUM ('new', 'in_progress', 'done');
CREATE TABLE tasks (
  id SERIAL PRIMARY KEY,
  task_status status DEFAULT 'pending'
);
medium
A. ENUM types cannot be used as column types
B. Missing semicolon after CREATE TYPE statement
C. The default value 'pending' is not in the ENUM list
D. The SERIAL keyword is deprecated

Solution

  1. Step 1: Check ENUM values and default

    ENUM 'status' has values 'new', 'in_progress', 'done'. Default 'pending' is not listed.
  2. Step 2: Understand default value constraints

    Default must be one of ENUM values; otherwise, it causes an error.
  3. Final Answer:

    The default value 'pending' is not in the ENUM list -> Option C
  4. Quick Check:

    Default must be ENUM member [OK]
Hint: Default must be one of ENUM values [OK]
Common Mistakes:
  • Assuming any string can be default
  • Thinking ENUM can't be column type
  • Ignoring missing semicolon errors
5. You want to add a new value 'archived' to an existing ENUM type status with values ('new', 'in_progress', 'done'). Which statement correctly adds 'archived' after 'done'?
hard
A. ALTER TYPE status ADD VALUE 'archived' BEFORE 'in_progress';
B. ALTER TYPE status ADD VALUE 'archived' AFTER 'done';
C. ALTER TYPE status ADD VALUE 'archived' BEFORE 'done';
D. ALTER TYPE status MODIFY VALUE 'archived' AFTER 'done';

Solution

  1. Step 1: Recall how to add ENUM values

    PostgreSQL uses ALTER TYPE ... ADD VALUE 'new_value' [BEFORE|AFTER existing_value] syntax.
  2. Step 2: Identify correct position

    To add 'archived' after 'done', use AFTER 'done'.
  3. Step 3: Check options

    ALTER TYPE status ADD VALUE 'archived' AFTER 'done'; matches correct syntax and position. Others use BEFORE (wrong position) or invalid keywords.
  4. Final Answer:

    ALTER TYPE status ADD VALUE 'archived' AFTER 'done'; -> Option B
  5. Quick Check:

    ALTER TYPE ADD VALUE ... AFTER ... [OK]
Hint: Use ALTER TYPE ADD VALUE 'val' AFTER 'existing' to position [OK]
Common Mistakes:
  • Using MODIFY instead of ADD VALUE
  • Placing new value BEFORE wrong existing value
  • Using wrong position like BEFORE 'done'