Bird
0
0

How can you combine the VALUES clause with a JOIN to match inline data with an existing table users(id, name)?

hard📝 Application Q9 of 15
PostgreSQL - Set Operations and Advanced Queries
How can you combine the VALUES clause with a JOIN to match inline data with an existing table users(id, name)?
ASELECT u.id, u.name, v.status FROM users u JOIN (VALUES 1, 'active', 2, 'inactive') AS v(id, status) ON u.id = v.id;
BSELECT u.id, u.name, v.status FROM users u, VALUES (1, 'active'), (2, 'inactive') AS v(id, status) WHERE u.id = v.id;
CSELECT u.id, u.name, v.status FROM users u JOIN VALUES (1, 'active'), (2, 'inactive') AS v(id, status) ON u.id = v.id;
DSELECT u.id, u.name, v.status FROM users u JOIN (VALUES (1, 'active'), (2, 'inactive')) AS v(id, status) ON u.id = v.id;
Step-by-Step Solution
Solution:
  1. Step 1: Understand correct JOIN syntax with VALUES

    SELECT u.id, u.name, v.status FROM users u JOIN (VALUES (1, 'active'), (2, 'inactive')) AS v(id, status) ON u.id = v.id; correctly wraps VALUES rows in parentheses and aliases columns, then joins on matching id.
  2. Step 2: Identify errors in other options

    SELECT u.id, u.name, v.status FROM users u, VALUES (1, 'active'), (2, 'inactive') AS v(id, status) WHERE u.id = v.id; uses comma join syntax incorrectly with VALUES. SELECT u.id, u.name, v.status FROM users u JOIN VALUES (1, 'active'), (2, 'inactive') AS v(id, status) ON u.id = v.id; misses parentheses around VALUES rows. SELECT u.id, u.name, v.status FROM users u JOIN (VALUES 1, 'active', 2, 'inactive') AS v(id, status) ON u.id = v.id; has incorrect VALUES syntax without proper row grouping.
  3. Final Answer:

    SELECT u.id, u.name, v.status FROM users u JOIN (VALUES (1, 'active'), (2, 'inactive')) AS v(id, status) ON u.id = v.id; -> Option D
  4. Quick Check:

    JOIN with VALUES needs parentheses and proper aliasing [OK]
Quick Trick: Use parentheses and alias columns when JOINing with VALUES [OK]
Common Mistakes:
  • Omitting parentheses around VALUES rows in JOIN
  • Using comma join syntax incorrectly with VALUES

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes