Question: SQL assignment Instructions using Oracle SQL Developer: Create a query that answers the following question: What customer has ordered a total of more than 3

SQL assignment

Instructions using Oracle SQL Developer:

Create a query that answers the following question:

What customer has ordered a total of more than 3 containers of "Uncle John's Lemon Ice". (Ensure that you use the PRODUCT_SKU in this query, but make sure the query outputs the customers business name along with the amount of the product order ordered). Sort the results in descending order by customers business name. Note the containers may be ordered across multiple orders (e.g. if they had one order with 2 containers ordered and another order with 2 containers it would satisfy the criteria.

My answer is at the bottom of the document but I keep receiving errors so any help would be greatly appreciated! Thank You!

-- Customer Table CREATE TABLE customer (customer_ID NUMERIC(4) NOT NULL, first_name VARCHAR2(25CHAR) NOT NULL, last_name VARCHAR2(25CHAR) NOT NULL, business VARCHAR2(30CHAR) NOT NULL, address VARCHAR2(25CHAR) NOT NULL, city VARCHAR2(15CHAR), state VARCHAR2(2CHAR), zip_code NUMERIC(5), country VARCHAR2(3CHAR) NOT NULL, phone_no VARCHAR2(25CHAR) NOT NULL, CONSTRAINT pk_customerID PRIMARY KEY (customer_ID));

-- Invoice Table CREATE TABLE invoice (invoice_ID NUMERIC(4), customer_ID NUMERIC(4), order_date DATE, delivery_date DATE, CONSTRAINT pk_invoiceID PRIMARY KEY (invoice_ID), CONSTRAINT fk_invoiceID_customerID FOREIGN KEY (customer_ID) REFERENCES customer(customer_ID));

-- Product Table CREATE TABLE product (product_SKU CHAR(7) NOT NULL, product_name VARCHAR2(255) NOT NULL, price NUMERIC(5,2), category VARCHAR2(30), CONSTRAINT pk_productSKU PRIMARY KEY (product_SKU) );

-- Invoice_Item Table CREATE TABLE invoice_item (invoice_ID NUMERIC(4), item_ID NUMERIC(3), product_SKU CHAR(7), quantity NUMERIC(4) NOT NULL, item_price NUMERIC(5,2) NOT NULL, CONSTRAINT pk_invoiceItems PRIMARY KEY (invoice_ID, item_ID), CONSTRAINT fk_invoiceItems_invoiceID FOREIGN KEY (invoice_ID) REFERENCES invoice (invoice_ID), CONSTRAINT fk_invoiceItems_productSKU FOREIGN KEY (product_SKU) REFERENCES product (product_SKU));

INSERT INTO customer VALUES(0001,'Emma','Watson','Target','4 Privet Drive','London','','91142','ENG','(537)839-9902'); INSERT INTO customer VALUES(0002,'Beyonce','Carter','Sephora','11 East Ave','New York','NY','30314','USA','(631)139-3108'); INSERT INTO customer VALUES(0003,'Ryan','Gosling','Walmart','11 Rodeo Dr','Beverly Hills','CA','90210','USA','(902)910-2820'); INSERT INTO customer VALUES(0004,'David','Beckham','Nike','30 Kelsall St','Liverpool','','91039','ENG','(716)811-2012'); INSERT INTO customer VALUES(0005,'Kendrick','Lamar','Top Dog Entertainment','11 East Ave','Compton','CA','89120','USA','(989)637-4726'); INSERT INTO customer VALUES(0006,'Ashton','Kutcher','Adidas','112 Beacon St','Boston','MA','12904','USA','(314)210-3902');

INSERT INTO invoice VALUES(1111,0001,'03-FEB-2017','10-FEB-2017'); INSERT INTO invoice VALUES(1112,0002,'08-FEB-2017','16-FEB-2017'); INSERT INTO invoice VALUES(1113,0003,'04-FEB-2017','11-FEB-2017'); INSERT INTO invoice VALUES(1114,0004,'05-FEB-2017','12-FEB-2017'); INSERT INTO invoice VALUES(1115,0005,'07-FEB-2017','14-FEB-2017'); INSERT INTO invoice VALUES(1116,0006,'07-FEB-2017','13-FEB-2017'); INSERT INTO invoice VALUES(1117,0001,'11-MAR-2017','17-MAR-2017'); INSERT INTO invoice VALUES(1118,0003,'03-MAR-2017','10-MAR-2017'); INSERT INTO invoice VALUES(1119,0002,'08-MAR-2017','16-MAR-2017'); INSERT INTO invoice VALUES(1120,0003,'04-MAR-2017','11-MAR-2017'); INSERT INTO invoice VALUES(1121,0004,'05-MAR-2017','12-MAR-2017'); INSERT INTO invoice VALUES(1122,0005,'07-MAR-2017','14-MAR-2017'); INSERT INTO invoice VALUES(1123,0006,'07-MAR-2017','13-MAR-2017'); INSERT INTO invoice VALUES(1124,0001,'11-MAR-2017','17-MAR-2017');

INSERT INTO product VALUES('01', 'Uncle Johns Lemon Ice',9.99,'Icee'); INSERT INTO product VALUES('02', 'Uncle Johns Raspberry Ice',9.99,'Icee'); INSERT INTO product VALUES('03', 'Pumpkin Pie',15.99,'Pie'); INSERT INTO product VALUES('04', 'Chocolate Cake',17.99,'Cake'); INSERT INTO product VALUES('05', 'Blueberry Muffins',12.99,'Muffins'); INSERT INTO product VALUES('06', 'Cream Cheese Brownies',16.99,'Brownies'); INSERT INTO product VALUES('07', 'Pumpkin Muffins',12.99,'Muffin'); INSERT INTO product VALUES('08', 'Red Velvet Cupcakes',16.99,'Cake'); INSERT INTO product VALUES('09', 'Fudgsicles',11.99,'Ice Cream'); INSERT INTO product VALUES('10', 'Chocolate Cupcakes',15.99,'Cake'); INSERT INTO product VALUES('11', 'Brownies',13.99,'Brownies'); INSERT INTO product VALUES('12', 'Mud Pie',15.99,'Pie'); INSERT INTO product VALUES('13', 'Ice Cream Sandwiches',11.99,'Ice Cream'); INSERT INTO product VALUES('14', 'Chocolate Ice Cream',10.99,'Ice Cream'); INSERT INTO product VALUES('15', 'Vanilla Ice Cream',10.99,'Ice Cream');

INSERT INTO invoice_item VALUES (1111,1,'01',2,9.99); INSERT INTO invoice_item VALUES (1111,2,'01',2,9.99); INSERT INTO invoice_item VALUES (1111,3,'02',1,9.99); INSERT INTO invoice_item VALUES (1112,1,'09',1,11.99); INSERT INTO invoice_item VALUES (1112,2,'12',2,15.99); INSERT INTO invoice_item VALUES (1113,1,'11',3,13.99); INSERT INTO invoice_item VALUES (1114,1,'10',1,15.99); INSERT INTO invoice_item VALUES (1115,1,'08',3,16.99); INSERT INTO invoice_item VALUES (1115,2,'07',2,12.99); INSERT INTO invoice_item VALUES (1116,1,'04',2,17.99); INSERT INTO invoice_item VALUES (1116,2,'03',1,15.99); INSERT INTO invoice_item VALUES (1116,3,'15',2,10.99); INSERT INTO invoice_item VALUES (1117,1,'01',1,9.99); INSERT INTO invoice_item VALUES (1118,1,'03',6,15.99); INSERT INTO invoice_item VALUES (1119,1,'11',3,13.99); INSERT INTO invoice_item VALUES (1119,2,'13',5,11.99); INSERT INTO invoice_item VALUES (1120,1,'01',3,9.99); INSERT INTO invoice_item VALUES (1121,1,'02',1,9.99); INSERT INTO invoice_item VALUES (1122,1,'06',7,16.99); INSERT INTO invoice_item VALUES (1123,1,'04',3,17.99); INSERT INTO invoice_item VALUES (1123,2,'10',3,15.99); INSERT INTO invoice_item VALUES (1123,3,'11',2,13.99); INSERT INTO invoice_item VALUES (1124,1,'13',2,11.99);

ALTER TABLE product ADD UNIQUE (product_name);

SELECT c.customer_ID, c.first_name, c.last_name, c.business, p.product_SKU, COUNT(invItem.quantity) FROM customer c, invoice i, product p, invoice_item invItem WHERE c.customer_ID = i.customer_ID AND i.invoice_ID = invItem.invoice_ID AND invItem = p.product_SKU AND p.product_name LIKE 'Uncle Johns Lemon Ice' GROUP BY c.customer_ID HAVING COUNT(p.product_SKU) > 3 ORDER BY c.business DESC;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!