Question: 2. This question concerns the relational schema for a bookstore as shown in figure 1. Given the schema in figure 1, write the following queries



2. This question concerns the relational schema for a bookstore as shown in figure 1. Given the schema in figure 1, write the following queries in SQL. You should ensure your queries run in SQLite (https://sql.js.org/examples/GUI/). (a) Find customers who bought at least one book by author Stephen King. (b) Find customers who bought no books written by Stephen King. (c) Find customers who bought every book written by Stephen King. (d) Find customers who bought exactly two books written by Stephen King. Do not use aggregation in your answer. create table CUSTOMER CID text primary key, NAME text, DOB date ); create table BOOK ( ISBN text primary key, TITLE text, AUTHOR text ); create table BUYS ( BID uuid primary key, CID text foreign key references CUSTOMER, ISBN text foreign key references BOOK, DATE timestamp, PRICE float ); (e) Find customers who bought exactly three books written by Stephen King. You may use aggregation in your answer if you wish. (f) Find customers who bought more than $1,000 worth of books by Stephen King. You may use aggregation in your answer if you wish. (g) Find pairs of books that were purchased by exactly the same set of customers. (h) Find pairs of customers that purchased the same number of books in total. (i) Find customers that purchased more than 90% of all books in the database. Hint: use a CTE for intermediate relations. (1) Write a query that returns book ISBNs and the following columns; i. The total number of distinct customers who purchased the book, ii. the earliest date the book was purchased, iii. the average price customers paid for the book, iv. and whether the book was ever purchased by a teen (values in this column should be 1 if it was or 0 if not). 2. This question concerns the relational schema for a bookstore as shown in figure 1. Given the schema in figure 1, write the following queries in SQL. You should ensure your queries run in SQLite (https://sql.js.org/examples/GUI/). (a) Find customers who bought at least one book by author Stephen King. (b) Find customers who bought no books written by Stephen King. (c) Find customers who bought every book written by Stephen King. (d) Find customers who bought exactly two books written by Stephen King. Do not use aggregation in your answer. create table CUSTOMER CID text primary key, NAME text, DOB date ); create table BOOK ( ISBN text primary key, TITLE text, AUTHOR text ); create table BUYS ( BID uuid primary key, CID text foreign key references CUSTOMER, ISBN text foreign key references BOOK, DATE timestamp, PRICE float ); (e) Find customers who bought exactly three books written by Stephen King. You may use aggregation in your answer if you wish. (f) Find customers who bought more than $1,000 worth of books by Stephen King. You may use aggregation in your answer if you wish. (g) Find pairs of books that were purchased by exactly the same set of customers. (h) Find pairs of customers that purchased the same number of books in total. (i) Find customers that purchased more than 90% of all books in the database. Hint: use a CTE for intermediate relations. (1) Write a query that returns book ISBNs and the following columns; i. The total number of distinct customers who purchased the book, ii. the earliest date the book was purchased, iii. the average price customers paid for the book, iv. and whether the book was ever purchased by a teen (values in this column should be 1 if it was or 0 if not)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
