Bird
Raised Fist0
PostgreSQLquery~10 mins

ENUM types in PostgreSQL - Interactive Code Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an ENUM type named 'mood' with values 'happy', 'sad', and 'neutral'.

PostgreSQL
CREATE TYPE mood AS ENUM ([1]);
Drag options to blanks, or click blank then click option'
A'happy'; 'sad'; 'neutral'
B'happy', 'sad', 'neutral'
C"happy", "sad", "neutral"
Dhappy, sad, neutral
Attempts:
3 left
💡 Hint
Common Mistakes
Using semicolons instead of commas.
Not quoting the ENUM values.
Using double quotes instead of single quotes.
2fill in blank
medium

Complete the code to add a column 'current_mood' of type 'mood' to the 'users' table.

PostgreSQL
ALTER TABLE users ADD COLUMN current_mood [1];
Drag options to blanks, or click blank then click option'
AENUM
BVARCHAR(10)
CTEXT
Dmood
Attempts:
3 left
💡 Hint
Common Mistakes
Using VARCHAR or TEXT instead of the ENUM type.
Using the keyword ENUM without the type name.
3fill in blank
hard

Fix the error in the code to insert a value 'excited' into the 'current_mood' column of 'users'.

PostgreSQL
INSERT INTO users (current_mood) VALUES ([1]);
Drag options to blanks, or click blank then click option'
A'excited'
Bexcited
C'happy'
D'neutral'
Attempts:
3 left
💡 Hint
Common Mistakes
Inserting a value not defined in the ENUM type.
Not quoting the value.
Using a value that is not part of the ENUM list.
4fill in blank
hard

Fill both blanks to change the ENUM type 'mood' by adding a new value 'excited' after 'happy'.

PostgreSQL
ALTER TYPE mood [1] [2] 'excited' AFTER 'happy';
Drag options to blanks, or click blank then click option'
AADD
BVALUE
CRENAME
DMODIFY
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'DROP' or 'RENAME' instead of 'ADD'.
Omitting the 'VALUE' keyword.
5fill in blank
hard

Fill all three blanks to select all users whose 'current_mood' is either 'happy' or 'excited'.

PostgreSQL
SELECT * FROM users WHERE current_mood [1] ('[2]', '[3]');
Drag options to blanks, or click blank then click option'
A=
BIN
Cexcited
Dhappy
Attempts:
3 left
💡 Hint
Common Mistakes
Using '=' instead of 'IN' for multiple values.
Not quoting the ENUM values.
Swapping the order of the ENUM 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'