0
0
PostgreSQLquery~20 mins

VALUES clause for inline data in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
VALUES Clause Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
Output of a simple VALUES clause
What is the output of this SQL query in PostgreSQL?

VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry');
PostgreSQL
VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry');
AThree rows with two columns: (1, 'apple'), (2, 'banana'), (3, 'cherry')
BOne row with six columns: 1, 'apple', 2, 'banana', 3, 'cherry'
CSyntax error due to missing SELECT keyword
DEmpty result set
Attempts:
2 left
💡 Hint
VALUES creates rows of data directly without needing a table.
query_result
intermediate
1:30remaining
Using VALUES with column aliases
What is the output of this query?

SELECT * FROM (VALUES (10, 'red'), (20, 'blue')) AS colors(id, name);
PostgreSQL
SELECT * FROM (VALUES (10, 'red'), (20, 'blue')) AS colors(id, name);
AError: aliasing columns not allowed in VALUES
BTwo rows with columns 'id' and 'name': (10, 'red'), (20, 'blue')
COne row with columns 'id' and 'name' containing (10, 'red', 20, 'blue')
DEmpty result set
Attempts:
2 left
💡 Hint
You can give names to columns when using VALUES inside FROM.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in VALUES usage
Which option contains a syntax error when using the VALUES clause in PostgreSQL?
AVALUES (1, 'a'), (2, 'b');
BSELECT * FROM (VALUES (1, 'a'), (2, 'b')) AS t(col1, col2);
CSELECT * FROM (VALUES (1, 'a'), (2, 'b')) t(col1, col2);
DVALUES (1, 'a'), (2, 'b') AS t(col1, col2);
Attempts:
2 left
💡 Hint
VALUES cannot be aliased directly without FROM or parentheses.
optimization
advanced
2:00remaining
Best way to insert multiple rows using VALUES
Which query is the most efficient and correct way to insert multiple rows into a table named fruits with columns id and name?
AINSERT INTO fruits (id, name) VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry');
BINSERT INTO fruits VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry');
CINSERT INTO fruits (id, name) SELECT * FROM (VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry'));
DINSERT INTO fruits (id, name) SELECT * FROM (VALUES (1, 'apple'), (2, 'banana'), (3, 'cherry')) AS t;
Attempts:
2 left
💡 Hint
Specifying columns is safer and clearer.
🧠 Conceptual
expert
2:30remaining
Understanding the scope of VALUES in complex queries
Consider this query:

WITH data AS (VALUES (1, 'x'), (2, 'y')) SELECT * FROM data WHERE column1 = 2;

What will happen when this query runs in PostgreSQL?
PostgreSQL
WITH data AS (VALUES (1, 'x'), (2, 'y')) SELECT * FROM data WHERE column1 = 2;
AReturns two rows: (1, 'x') and (2, 'y')
BReturns one row: (2, 'y')
CError: column1 is not recognized because VALUES columns are unnamed by default
DEmpty result set
Attempts:
2 left
💡 Hint
VALUES columns need explicit names in CTEs to be referenced.