books with an id column and a details column of type JSONB.genre, author, and year.@> operator to find books with a specific genre.@> operator to find books by a specific author.books with an id column and a details column of type JSONB.genre, author, and year.@> operator to find books with a specific genre.@> operator to find books by a specific author.books table with JSONB columnbooks with two columns: id as an integer primary key and details as a JSONB type.Use CREATE TABLE with id SERIAL PRIMARY KEY and details JSONB.
books table with details JSONB values exactly as follows: {"genre": "fiction", "author": "Alice", "year": 2020}, {"genre": "non-fiction", "author": "Bob", "year": 2018}, and {"genre": "fiction", "author": "Charlie", "year": 2021}.Use INSERT INTO books (details) VALUES with JSON strings in single quotes.
@> operatorbooks where the details JSONB column contains the key-value pair {"genre": "fiction"} using the @> operator.Use WHERE details @> '{"genre": "fiction"}' to filter JSONB rows.
@> operatorbooks where the details JSONB column contains the key-value pair {"author": "Bob"} using the @> operator.Use WHERE details @> '{"author": "Bob"}' to filter JSONB rows by author.
What does the PostgreSQL @> operator do when used with JSONB data?
@> operator in PostgreSQL is used to check containment between JSONB values.Which of the following is the correct syntax to check if a JSONB column data contains the JSON {"key": "value"}?
SELECT * FROM table WHERE data ___ '{"key": "value"}'::jsonb;@> operator checks if the left JSONB contains the right JSONB.data contains the JSON, use data @> '{"key": "value"}'::jsonb.Given the table items with a JSONB column attributes, what will this query return?
SELECT id FROM items WHERE attributes @> '{"color": "red"}'::jsonb;Assuming items contains:
id | attributes
---+-------------------------
1 | {"color": "red", "size": "M"}
2 | {"color": "blue", "size": "L"}
3 | {"color": "red"}
4 | {"size": "S"}attributes contain the JSON {"color": "red"}.Identify the error in this query that tries to find rows where info JSONB column contains {"active": true}:
SELECT * FROM users WHERE info @> '{active: true}';active should be "active".::jsonb cast, but the main syntax error is the missing quotes around the key.You want to find all rows in products where the details JSONB column contains {"features": {"waterproof": true}} and also the price is less than 100. Which query is correct?
@> operator checks if details contains the given JSON. The JSON must have keys in double quotes and be cast to jsonb.AND price < 100 to filter by price.Pass the test and this pattern moves unknown