Bird
Raised Fist0
PostgreSQLquery~20 mins

UUID type and generation in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
UUID 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 UUID generation query?
Consider the following PostgreSQL query that generates a UUID using the gen_random_uuid() function. What is the expected output type of the query?
PostgreSQL
SELECT gen_random_uuid();
AA single row with a random integer value
BMultiple rows each containing a UUID value
CA single row with a UUID value in standard 8-4-4-4-12 hexadecimal format
DAn error because <code>gen_random_uuid()</code> is not a valid function
Attempts:
2 left
💡 Hint
Think about what gen_random_uuid() returns and how many rows the query produces.
🧠 Conceptual
intermediate
2:00remaining
Which UUID version does gen_random_uuid() generate?
In PostgreSQL, the gen_random_uuid() function generates UUIDs. Which version of UUID does it produce?
AVersion 4 (randomly generated UUID)
BVersion 3 (name-based using MD5)
CVersion 1 (time-based UUID)
DVersion 5 (name-based using SHA-1)
Attempts:
2 left
💡 Hint
Consider the randomness aspect of the UUID generated by gen_random_uuid().
📝 Syntax
advanced
2:30remaining
Which query correctly creates a table with a UUID primary key that auto-generates UUIDs?
You want to create a PostgreSQL table named users with a user_id column as a UUID primary key that automatically generates a UUID for each new row. Which query is correct?
ACREATE TABLE users (user_id UUID PRIMARY KEY DEFAULT gen_random_uuid());
BCREATE TABLE users (user_id UUID PRIMARY KEY DEFAULT uuid_generate_v4());
CCREATE TABLE users (user_id UUID PRIMARY KEY DEFAULT random_uuid());
DCREATE TABLE users (user_id UUID PRIMARY KEY DEFAULT uuid());
Attempts:
2 left
💡 Hint
Check the exact function name for generating random UUIDs in PostgreSQL's pgcrypto extension.
❓ optimization
advanced
2:30remaining
Which approach improves UUID indexing performance in PostgreSQL?
You have a table with a UUID primary key generated by gen_random_uuid(). UUIDs are random and can cause index bloat. Which approach can improve index performance?
ADisable indexing on the UUID column
BUse UUID version 1 (time-based) instead of version 4 to generate UUIDs
CStore UUIDs as text instead of UUID type
DCreate a B-tree index on the UUID column without changes
Attempts:
2 left
💡 Hint
Think about how the order of UUID values affects index performance.
🔧 Debug
expert
3:00remaining
Why does this query fail with "function gen_random_uuid() does not exist" error?
You run this query: SELECT gen_random_uuid(); but get the error: function gen_random_uuid() does not exist. What is the most likely cause?
AThe query requires superuser privileges to run
BThe function name is misspelled; it should be <code>generate_random_uuid()</code>
CThe UUID type is not supported in this PostgreSQL version
DThe pgcrypto extension is not installed or enabled in the database
Attempts:
2 left
💡 Hint
Check if the required extension for UUID generation is available and enabled.

Practice

(1/5)
1. What is the main purpose of using a UUID type in PostgreSQL?
easy
A. To store unique identifiers that avoid duplicates across tables or systems
B. To store large text data efficiently
C. To perform mathematical calculations faster
D. To create indexes automatically on a table

Solution

  1. Step 1: Understand UUID purpose

    UUID stands for Universally Unique Identifier, designed to be unique across space and time.
  2. Step 2: Identify its use in PostgreSQL

    PostgreSQL uses UUID to store unique IDs that avoid duplicates, especially in distributed systems.
  3. Final Answer:

    To store unique identifiers that avoid duplicates across tables or systems -> Option A
  4. Quick Check:

    UUID = Unique ID storage [OK]
Hint: UUIDs are for unique IDs, not for text or math [OK]
Common Mistakes:
  • Confusing UUID with text or numeric types
  • Thinking UUID speeds up math operations
  • Assuming UUID auto-creates indexes
2. Which of the following is the correct way to generate a random UUID in PostgreSQL?
easy
A. SELECT uuid_generate_v4();
B. SELECT random_uuid();
C. SELECT gen_random_uuid();
D. SELECT create_uuid();

Solution

  1. Step 1: Recall PostgreSQL UUID functions

    PostgreSQL's pgcrypto extension provides gen_random_uuid() to generate random UUIDs.
  2. Step 2: Check function names

    uuid_generate_v4() is from another extension (uuid-ossp), not default. The others are invalid.
  3. Final Answer:

    SELECT gen_random_uuid(); -> Option C
  4. Quick Check:

    Random UUID function = gen_random_uuid() [OK]
Hint: Use gen_random_uuid() with pgcrypto extension [OK]
Common Mistakes:
  • Using uuid_generate_v4() without enabling uuid-ossp
  • Typing random_uuid() which does not exist
  • Confusing function names
3. Given the table creation and insert statements below, what will be the output of the SELECT query?
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE users (id UUID PRIMARY KEY DEFAULT gen_random_uuid(), name TEXT);
INSERT INTO users (name) VALUES ('Alice'), ('Bob');
SELECT id, name FROM users ORDER BY name;
medium
A. Two rows with NULL ids for Alice and Bob
B. Two rows with unique UUIDs for Alice and Bob, ordered by name
C. Syntax error because gen_random_uuid() is not allowed in DEFAULT
D. One row only because UUID duplicates cause conflict

Solution

  1. Step 1: Understand table and insert behavior

    The table uses gen_random_uuid() as default for id. Inserting names without specifying id generates unique UUIDs automatically.
  2. Step 2: Analyze SELECT output

    The SELECT query returns all rows ordered by name, showing unique UUIDs for Alice and Bob.
  3. Final Answer:

    Two rows with unique UUIDs for Alice and Bob, ordered by name -> Option B
  4. Quick Check:

    Default UUID generates unique IDs [OK]
Hint: DEFAULT gen_random_uuid() auto-fills unique IDs [OK]
Common Mistakes:
  • Expecting NULL ids when default is set
  • Thinking gen_random_uuid() causes syntax error
  • Assuming UUID duplicates block inserts
4. You run the following commands but get an error: ERROR: function gen_random_uuid() does not exist. What is the most likely fix?
SELECT gen_random_uuid();
medium
A. Change the query to SELECT random_uuid();
B. Use uuid_generate_v4() instead without any extension
C. Restart PostgreSQL server to refresh functions
D. Enable the pgcrypto extension using CREATE EXTENSION pgcrypto;

Solution

  1. Step 1: Identify cause of missing function

    The error means gen_random_uuid() is not available because the pgcrypto extension is not enabled.
  2. Step 2: Apply correct fix

    Enabling pgcrypto with CREATE EXTENSION pgcrypto; adds the function.
  3. Final Answer:

    Enable the pgcrypto extension using CREATE EXTENSION pgcrypto; -> Option D
  4. Quick Check:

    Missing function = enable pgcrypto [OK]
Hint: Enable pgcrypto extension to use gen_random_uuid() [OK]
Common Mistakes:
  • Trying uuid_generate_v4() without enabling uuid-ossp
  • Restarting server unnecessarily
  • Using random_uuid() which does not exist
5. You want to create a table that stores user sessions with a UUID primary key generated automatically, but also want to insert a specific UUID sometimes. Which table definition and insert statement is correct?
CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE sessions (
  session_id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id INT
);

-- Insert with automatic UUID
INSERT INTO sessions (user_id) VALUES (101);

-- Insert with specific UUID
INSERT INTO sessions (session_id, user_id) VALUES ('550e8400-e29b-41d4-a716-446655440000', 102);
hard
A. This setup works correctly, allowing both automatic and manual UUID insertion
B. You cannot insert a specific UUID if DEFAULT is set for the column
C. You must disable the default to insert specific UUIDs
D. The UUID must be generated outside PostgreSQL and inserted as text

Solution

  1. Step 1: Understand DEFAULT with manual insert

    Setting DEFAULT to gen_random_uuid() means if no value is given, PostgreSQL generates one automatically.
  2. Step 2: Check manual UUID insertion

    When you specify session_id explicitly, PostgreSQL uses that value instead of the default.
  3. Step 3: Confirm correctness

    This allows both automatic UUID generation and manual UUID insertion in the same table.
  4. Final Answer:

    This setup works correctly, allowing both automatic and manual UUID insertion -> Option A
  5. Quick Check:

    DEFAULT allows manual override [OK]
Hint: DEFAULT UUID generates only if no value given [OK]
Common Mistakes:
  • Thinking DEFAULT blocks manual inserts
  • Trying to disable default for manual UUIDs
  • Assuming UUID must be generated outside PostgreSQL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard