Challenge - 5 Problems
JSON Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this JSON insertion query?
Consider the table
users with a data column of type jsonb. What will be the result after running this insertion?INSERT INTO users (data) VALUES ('{"name": "Alice", "age": 30}');
SELECT data->>'name' AS name FROM users WHERE data->>'age' = '30';PostgreSQL
INSERT INTO users (data) VALUES ('{"name": "Alice", "age": 30}'); SELECT data->>'name' AS name FROM users WHERE data->>'age' = '30';
Attempts:
2 left
💡 Hint
Remember that JSON numbers are stored as numbers, but the query compares the age as a string.
✗ Incorrect
The insertion stores age as a number. The WHERE clause compares the age as a string '30', which works because the operator ->> returns text. So the row matches and the name 'Alice' is returned.
📝 Syntax
intermediate2:00remaining
Which INSERT statement correctly inserts JSON data into a jsonb column?
Given a table
products with a details column of type jsonb, which of the following INSERT statements is syntactically correct?Attempts:
2 left
💡 Hint
JSON data must be a valid JSON string literal inside single quotes.
✗ Incorrect
Option A uses a valid JSON string inside single quotes. Option A is invalid syntax because JSON keys and strings must be quoted. Option A is invalid because jsonb is a type cast and requires ::jsonb or a function. Option A uses double quotes incorrectly and single quotes inside JSON keys are invalid.
❓ optimization
advanced2:00remaining
Which approach is best to insert multiple JSON objects efficiently?
You want to insert multiple JSON objects into a
jsonb column in one query. Which option is the most efficient and syntactically correct?Attempts:
2 left
💡 Hint
Look for a single INSERT statement that inserts multiple rows.
✗ Incorrect
Option D inserts multiple rows in one statement, which is efficient. Option D is invalid syntax. Option D tries to select from jsonb_array_elements but does not specify the column properly. Option D uses two separate inserts, less efficient.
🔧 Debug
advanced2:00remaining
Why does this JSON insertion fail with a syntax error?
Given the query:
Why does this cause a syntax error?
INSERT INTO orders (info) VALUES ('{customer: "John", total: 100}');Why does this cause a syntax error?
PostgreSQL
INSERT INTO orders (info) VALUES ('{customer: "John", total: 100}');
Attempts:
2 left
💡 Hint
Check JSON syntax rules for keys and strings.
✗ Incorrect
JSON requires keys to be double-quoted strings. The given JSON uses unquoted keys, causing a syntax error.
🧠 Conceptual
expert2:00remaining
What happens if you insert invalid JSON into a jsonb column?
You try to run:
What will PostgreSQL do?
INSERT INTO events (data) VALUES ('{event: start, time: now()}');What will PostgreSQL do?
PostgreSQL
INSERT INTO events (data) VALUES ('{event: start, time: now()}');
Attempts:
2 left
💡 Hint
PostgreSQL enforces JSON validity on jsonb columns.
✗ Incorrect
PostgreSQL checks JSON validity on insert. Invalid JSON causes an error and the insert fails.