Bird
Raised Fist0
PostgreSQLquery~10 mins

JSONB existence (?) operator in PostgreSQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the JSONB column 'data' contains the key 'name'.

PostgreSQL
SELECT * FROM users WHERE data [1] 'name';
Drag options to blanks, or click blank then click option'
A?
B@>
C->
D#>
Attempts:
3 left
💡 Hint
Common Mistakes
Using @> which checks for containment of a JSONB object, not a key.
Using -> which extracts JSONB object fields, not checks existence.
2fill in blank
medium

Complete the code to find rows where the JSONB column 'info' contains the key 'age'.

PostgreSQL
SELECT id FROM profiles WHERE info [1] 'age';
Drag options to blanks, or click blank then click option'
A@>
B?
C?&
D?|
Attempts:
3 left
💡 Hint
Common Mistakes
Using ?| or ?& which check for multiple keys, not a single key.
Using @> which checks for JSONB containment, not key existence.
3fill in blank
hard

Fix the error in the code to check if the JSONB column 'attributes' contains the key 'color'.

PostgreSQL
SELECT * FROM items WHERE attributes [1] 'color';
Drag options to blanks, or click blank then click option'
A#>
B->
C@>
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using -> which extracts JSONB values but does not check key existence.
Using @> which checks if JSONB contains another JSONB object.
4fill in blank
hard

Complete the code to check if the JSONB column 'settings' contains both keys 'dark_mode' and 'notifications'.

PostgreSQL
SELECT * FROM user_prefs WHERE settings [1] ARRAY['dark_mode', 'notifications'] ;
Drag options to blanks, or click blank then click option'
A?&
B?|
C;
Attempts:
3 left
💡 Hint
Common Mistakes
Using ?| which checks if any key exists, not all keys.
Adding a semicolon inside the WHERE clause causing syntax error.
5fill in blank
hard

Fill both blanks to find rows where the JSONB column 'config' contains either 'auto_save' or 'backup' keys.

PostgreSQL
SELECT * FROM documents WHERE config [1] ARRAY['auto_save', [2]] ;
Drag options to blanks, or click blank then click option'
A?|
B'backup'
D'autosave'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ?& which requires all keys to exist, not any.
Forgetting quotes around keys causing syntax errors.
Adding a semicolon inside the WHERE clause.

Practice

(1/5)
1.

What does the PostgreSQL JSONB existence operator ? check for?

easy
A. If a JSONB column contains any null values
B. If a JSONB column is empty
C. If a specified key or string exists at the top level in a JSONB column
D. If a JSONB column contains nested arrays

Solution

  1. Step 1: Understand the operator's purpose

    The ? operator checks for the presence of a key or string in JSONB data.
  2. Step 2: Identify the scope of the check

    It only checks the top-level keys or strings, not nested elements.
  3. Final Answer:

    If a specified key or string exists at the top level in a JSONB column -> Option C
  4. Quick Check:

    JSONB existence operator checks top-level keys [OK]
Hint: Remember: '?' checks top-level keys only in JSONB [OK]
Common Mistakes:
  • Thinking it checks nested keys
  • Confusing it with null or empty checks
  • Assuming it works on JSON type (not JSONB)
  • Believing it checks values instead of keys
2.

Which of the following is the correct syntax to check if the key 'name' exists in a JSONB column data?

SELECT * FROM users WHERE data ? 'name';
easy
A. SELECT * FROM users WHERE data ? name;
B. SELECT * FROM users WHERE data ? 'name';
C. SELECT * FROM users WHERE data ? "name";
D. SELECT * FROM users WHERE data ? :name;

Solution

  1. Step 1: Check operator usage

    The ? operator requires the key as a string literal in single quotes.
  2. Step 2: Validate syntax correctness

    SELECT * FROM users WHERE data ? 'name'; uses single quotes correctly; others either miss quotes or use invalid syntax.
  3. Final Answer:

    SELECT * FROM users WHERE data ? 'name'; -> Option B
  4. Quick Check:

    Key must be a single-quoted string [OK]
Hint: Use single quotes around key with '?' operator [OK]
Common Mistakes:
  • Omitting quotes around the key
  • Using double quotes instead of single quotes
  • Using parameter syntax (:name) incorrectly
  • Confusing key with column name without quotes
3.

Given the table products with a JSONB column attributes containing {"color": "red", "size": "M"}, what will this query return?

SELECT id FROM products WHERE attributes ? 'color';
medium
A. Syntax error due to incorrect operator usage
B. All product IDs where the value of 'color' is 'red'
C. All product IDs where the 'color' key does not exist
D. All product IDs where the 'color' key exists in attributes

Solution

  1. Step 1: Understand the query condition

    The condition attributes ? 'color' checks if the key 'color' exists in the JSONB column.
  2. Step 2: Determine the query result

    It returns all rows where 'color' is a key, regardless of its value.
  3. Final Answer:

    All product IDs where the 'color' key exists in attributes -> Option D
  4. Quick Check:

    Operator '?' checks key existence, not value [OK]
Hint: Remember '?' checks keys, not values [OK]
Common Mistakes:
  • Thinking it filters by key value
  • Assuming it returns rows without the key
  • Confusing '?' with containment operator
  • Expecting syntax error incorrectly
4.

Identify the error in this query that checks if the key "status" exists in the JSONB column info:

SELECT * FROM orders WHERE info ? status;
medium
A. Missing single quotes around the key 'status'
B. Using double quotes instead of single quotes
C. The operator '?' cannot be used with JSONB columns
D. The column name 'info' is invalid

Solution

  1. Step 1: Check key syntax in operator

    The key must be a string literal in single quotes when using the '?' operator.
  2. Step 2: Identify the error in the query

    The query uses status without quotes, causing a syntax error or wrong interpretation.
  3. Final Answer:

    Missing single quotes around the key 'status' -> Option A
  4. Quick Check:

    Keys must be single-quoted strings [OK]
Hint: Always quote keys with single quotes in '?' operator [OK]
Common Mistakes:
  • Using unquoted keys
  • Using double quotes instead of single quotes
  • Thinking '?' works on non-JSONB columns
  • Assuming column name is the problem
5.

You want to find all rows in the events table where the JSONB column details contains either the key 'location' or the key 'venue'. Which query correctly uses the JSONB existence operator to achieve this?

hard
A. SELECT * FROM events WHERE details ?| array['location', 'venue'];
B. SELECT * FROM events WHERE details ?& array['location', 'venue'];
C. SELECT * FROM events WHERE details ? array['location', 'venue'];
D. SELECT * FROM events WHERE details ? 'location' AND details ? 'venue';

Solution

  1. Step 1: Understand the requirement

    We want rows where either 'location' OR 'venue' keys exist in the JSONB column.
  2. Step 2: Choose the correct operator

    The ?| operator checks if any key in the array exists in the JSONB data.
  3. Step 3: Compare options

    SELECT * FROM events WHERE details ?| array['location', 'venue']; uses ?| array['location', 'venue'], which matches the requirement.
  4. Final Answer:

    SELECT * FROM events WHERE details ?| array['location', 'venue']; -> Option A
  5. Quick Check:

    Use '?|' for any key in array [OK]
Hint: Use '?|' with array for checking multiple keys [OK]
Common Mistakes:
  • Using OR with multiple '?' instead of '?|'
  • Using '?&' which requires all keys
  • Using AND instead of OR logic
  • Confusing single '?' with multiple keys
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard