2. Which of the following is the correct way to declare a column with jsonb type in PostgreSQL?
easy
A. CREATE TABLE data (info JSONB);
B. CREATE TABLE data (info JSONB[]);
C. CREATE TABLE data (info JSON);
D. CREATE TABLE data (info BINARYJSON);
Solution
Step 1: Review PostgreSQL syntax for JSONB
The correct data type name is jsonb (case-insensitive), used directly as a column type.
Step 2: Check each option
Option A uses JSONB correctly. Option A tries to declare an array of jsonb, which is valid but not the simplest declaration. Option A uses JSON type, not jsonb. Option A uses a non-existent type BINARYJSON.
Final Answer:
CREATE TABLE data (info JSONB); -> Option A
Quick Check:
Correct jsonb column syntax = Option A [OK]
Hint: Use JSONB directly as column type, no extra keywords [OK]
Common Mistakes:
Confusing JSON and JSONB types
Using invalid type names like BINARYJSON
Adding unnecessary array brackets
3. Given the table items with a json column data storing '{"a":1, "b":2}', what will this query return?
SELECT data @> '{"a":1}'::jsonb FROM items;
medium
A. Error because data is json, but query uses jsonb operator
B. True for rows where data contains key "a" with value 1
C. False for all rows because data is json, not jsonb
D. Returns all rows regardless of content
Solution
Step 1: Understand operator compatibility
The @> operator is defined for jsonb types, not for json. Using it on a json column causes an error.
Step 2: Analyze the query
The query tries to use @> on data which is json, but the right side is cast to jsonb. This mismatch causes a type error.
Final Answer:
Error because data is json, but query uses jsonb operator -> Option A
Quick Check:
jsonb operator on json column = Error [OK]
Hint: jsonb operators need jsonb columns, not json [OK]
Common Mistakes:
Assuming json and jsonb are interchangeable
Ignoring type casting errors
Expecting operator to work on json type
4. You have a table with a jsonb column but your query is slow when filtering by keys. What is the most likely cause?
medium
A. No GIN or BTREE index on the jsonb column
B. Using json type instead of jsonb
C. The JSON data is stored as plain text
D. The query uses LIKE operator on the jsonb column
Solution
Step 1: Understand indexing for jsonb
jsonb supports GIN or BTREE indexes to speed up key/value lookups.
Step 2: Identify cause of slow queries
If no index exists on the jsonb column, queries filtering by keys must scan all rows, causing slowness.
Final Answer:
No GIN or BTREE index on the jsonb column -> Option A
Quick Check:
Missing index on jsonb = slow queries [OK]
Hint: Add GIN index on jsonb for fast key lookups [OK]
Common Mistakes:
Confusing json and jsonb types
Using LIKE on jsonb instead of jsonb operators
Ignoring indexing for jsonb columns
5. You want to store JSON data and frequently query nested keys with fast performance. Which approach is best?
hard
A. Store data as json and create expression indexes on keys
B. Store data as jsonb and create a GIN index on the column
C. Store data as text and parse JSON in application code
D. Store data as jsonb but avoid indexes for faster inserts
Solution
Step 1: Choose data type for fast queries
jsonb is optimized for querying and indexing nested JSON data.
Step 2: Use appropriate indexing
Creating a GIN index on the jsonb column enables fast key and path lookups.
Final Answer:
Store data as jsonb and create a GIN index on the column -> Option B
Quick Check:
jsonb + GIN index = best for fast nested queries [OK]
Hint: Use jsonb with GIN index for fast nested JSON queries [OK]
Common Mistakes:
Using json type which is slower for queries
Avoiding indexes to speed inserts but hurting queries
Parsing JSON outside database causing overhead
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.