Bird
0
0

Given the ENUM type and table below, what will be the result of the query?

medium📝 query result Q13 of 15
PostgreSQL - Advanced Features
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;
Ared, blue, green
Bblue, green, red
Cgreen, blue, red
Dred, green, blue
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes