0
0
PostgreSQLquery~20 mins

Inserting JSON data in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSON Data Master
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 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';
A[{"name": "Alice"}]
BSyntaxError
C[{"name": null}]
D[]
Attempts:
2 left
💡 Hint
Remember that JSON numbers are stored as numbers, but the query compares the age as a string.
📝 Syntax
intermediate
2: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?
AINSERT INTO products (details) VALUES ('{"color": "red", "size": "M"}');
BINSERT INTO products (details) VALUES ({color: 'red', size: 'M'});
CINSERT INTO products (details) VALUES (jsonb '{"color": "red", "size": "M"}');
DINSERT INTO products (details) VALUES ("{'color': 'red', 'size': 'M'}");
Attempts:
2 left
💡 Hint
JSON data must be a valid JSON string literal inside single quotes.
optimization
advanced
2: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?
AINSERT INTO logs (entry) SELECT * FROM jsonb_array_elements('[{"event": "login"}, {"event": "logout"}]');
BINSERT INTO logs (entry) VALUES jsonb_array_elements('[{"event": "login"}, {"event": "logout"}]');
CINSERT INTO logs (entry) VALUES ('{"event": "login"}'); INSERT INTO logs (entry) VALUES ('{"event": "logout"}');
DINSERT INTO logs (entry) VALUES ('{"event": "login"}'), ('{"event": "logout"}');
Attempts:
2 left
💡 Hint
Look for a single INSERT statement that inserts multiple rows.
🔧 Debug
advanced
2:00remaining
Why does this JSON insertion fail with a syntax error?
Given the query:

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}');
AThe number 100 must be in quotes to be valid JSON.
BDouble quotes inside single quotes cause syntax errors in SQL.
CJSON keys must be in double quotes, so customer should be "customer".
DThe VALUES keyword is missing.
Attempts:
2 left
💡 Hint
Check JSON syntax rules for keys and strings.
🧠 Conceptual
expert
2:00remaining
What happens if you insert invalid JSON into a jsonb column?
You try to run:

INSERT INTO events (data) VALUES ('{event: start, time: now()}');

What will PostgreSQL do?
PostgreSQL
INSERT INTO events (data) VALUES ('{event: start, time: now()}');
AInsert NULL into the jsonb column.
BRaise a syntax error because the JSON is invalid.
CAutomatically convert the invalid JSON to valid JSON.
DInsert the data as text without error.
Attempts:
2 left
💡 Hint
PostgreSQL enforces JSON validity on jsonb columns.