0
0
PostgreSQLquery~20 mins

JSONB modification functions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JSONB Modifier 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 JSONB update?
Given the table data with a JSONB column info containing {"name": "Alice", "age": 30}, what is the result of this query?
SELECT info || '{"age": 31, "city": "NY"}'::jsonb FROM data;
A{"name": "Alice", "age": 31}
B{"name": "Alice", "age": 30, "city": "NY"}
C{"name": "Alice", "age": 31, "city": "NY"}
D{"name": "Alice", "age": 30}
Attempts:
2 left
💡 Hint
The || operator merges JSONB objects, replacing existing keys.
query_result
intermediate
2:00remaining
What does jsonb_set do in this query?
Consider a JSONB column info with value {"user": {"name": "Bob", "age": 25}}. What is the output of:
SELECT jsonb_set(info, '{user,age}', '26', false) FROM data;
A{"user": {"name": "Bob"}}
B{"user": {"name": "Bob", "age": "26"}}
C{"user": {"name": "Bob", "age": 25}}
D{"user": {"name": "Bob", "age": 26}}
Attempts:
2 left
💡 Hint
jsonb_set replaces the value at the specified path with the new value.
📝 Syntax
advanced
2:00remaining
Which option correctly removes a key from JSONB?
You want to remove the key city from a JSONB column info. Which query is valid and works as expected?
ASELECT info - 'city' FROM data;
BSELECT jsonb_remove(info, 'city') FROM data;
CSELECT info \ 'city' FROM data;
DSELECT jsonb_delete(info, 'city') FROM data;
Attempts:
2 left
💡 Hint
The - operator removes a key from JSONB.
optimization
advanced
2:00remaining
Best way to update nested JSONB key efficiently?
You have a large JSONB column data with nested keys. You want to update settings.notifications.email to false for many rows. Which approach is most efficient?
ADelete the entire settings key and insert a new JSONB object with updated values.
BExtract JSONB to text, replace with string functions, then cast back to JSONB.
CUse jsonb_insert to add the key email with value false.
DUse jsonb_set with path '{settings,notifications,email}' and update only changed rows.
Attempts:
2 left
💡 Hint
jsonb_set updates nested keys without rewriting entire JSONB.
🧠 Conceptual
expert
2:00remaining
What happens if jsonb_set tries to update a missing path with create_missing = false?
Given a JSONB column doc with value {"a": {"b": 1}}, what is the result of:
SELECT jsonb_set(doc, '{a,c}', '2', false) FROM data;
A{"a": {"b": 1}}
B{"a": {"b": 1, "c": 2}}
CNULL
DError: path does not exist
Attempts:
2 left
💡 Hint
create_missing = false means do not create keys if path is missing.