Complete the code to select rows where the JSONB column 'data' contains the key 'name'.
SELECT * FROM users WHERE data [1] '{"name": "John"}';
The @> operator checks if the left JSONB contains the right JSONB. Here, it finds rows where 'data' contains the key 'name' with value 'John'.
Complete the code to find rows where the JSONB column 'info' contains the key-value pair 'age': 30.
SELECT * FROM profiles WHERE info [1] '{"age": 30}';
The @> operator checks if 'info' contains the key 'age' with value 30.
Fix the error in the query to correctly check if 'attributes' JSONB contains the key 'color' with value 'red'.
SELECT * FROM items WHERE attributes [1] '{"color": "red"}';
The operator @> is correct for containment. Also, the JSON must be valid with double quotes around keys and strings.
Fill both blanks to select rows where 'profile' JSONB contains the key 'status' with value 'active' and the key 'score' with value greater than 50.
SELECT * FROM users WHERE profile [1] '{"status": "active"}' AND (profile->>'score')::int [2] 50;
The @> operator checks if 'profile' contains the key 'status' with value 'active'. The operator > compares the 'score' string value numerically (after casting) to 50.
Fill all three blanks to create a query that selects rows where 'details' JSONB contains 'type' equal to 'premium', 'active' equal to true, and 'visits' greater than 100.
SELECT * FROM accounts WHERE details [1] '{"type": "premium", "active": true}' AND (details->>[2])::int [3] 100;
The @> operator checks if 'details' contains the keys 'type' and 'active' with specified values. The key 'visits' is extracted as text, cast to integer, and compared with '>' to 100.