Question
drop table if exists orders ; CREATE TABLE orders ( ID serial NOT NULL PRIMARY KEY, info jsonb NOT NULL ); INSERT INTO orders (info)
drop table if exists orders ; CREATE TABLE orders ( ID serial NOT NULL PRIMARY KEY, info jsonb NOT NULL );
INSERT INTO orders (info) VALUES ( '{ "customer": "John Doe", "items": {"product": "Beer","qty": 6}}' ), ( '{ "customer": "Lily Bush", "items": {"product": "Diaper","qty": 24}}' ), ( '{ "customer": "Josh William", "items": {"product": "Toy Car","qty": 1}}' ), ( '{ "customer": "Mary Clark", "items": {"product": "Toy Train","qty": 2}}' );
Update the table so that quantity of toy car ordered by Josh William is 25.
Change the table so that items is an array of json objects. Each json object in the array has two fields, "qty" and "product". This change should happen for every row of the table. You want info to change from being like {customer: NAME, items:{product:thing, qty:x}} to looking like {customer: NAME, items:[{product:thing, qty:x}]}. Hint: you will use the function jsonb_build_array which takes in thing(s) and returns a json array containing thing(s).
Update the table so that there are two orders made by Josh William. Josh should also order 'Cheerio'. The quantity of his order for Cheerio should be 2. Do not add any new rows to the table. Instead, Josh's second order should appear in the same row as his first order.
Write a query that gives a table that shows only orders where qty is 2. Each row in the table should have id, customer, qty, and product as attributes. (You will need to write a subquery.)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started