Question

In: Computer Science

Question 1: Group by and Aggregates: Write SQL statements to answer the following questions using Assignment...

Question 1: Group by and Aggregates:

Write SQL statements to answer the following questions using Assignment 4’s schema (Customer-Invoice-Line-Product-Vendor). Make sure that your SQL script runs without any errors. Submit your answers in a .SQL file.

1 (2 Points) - Find the count of distinctvendors thatsupplied products that are priced lowerthan 185?

2 (2 Points) - For each vendor, find their product that has the lowest product quantity. Your output should include vendor code, vendor name, product description and product quantity for each vendor.

Hint: Use subquery to get minimum quantity

3 (2 Points) - Find how many products are there in each invoice. The output should include invoice number and number of products in the invoice.

4 (2 Points) - Find how many invoices are made by each customer. The output should be a list of cus_code and for each cus_code, the number of invoices made by this customer.

5 (2 Points) - Find the total value for all products in the inventory. The total value in the inventory is the sum of product quantity * product price for all products listed in the product table.

6 (2 Points) - Find vendor code, vendor contact, and the number of products supplied by each vendor.

7 (2 Points) - Find product description, price, and vendor code for thecheapest (lowest price) product.

8 (3 Points) - For each invoice, find the total price. The total invoice price is the sum of product price* line units for each product purchased in the invoice.

9 (3 Points) - Find how many products are bought by each customer. The output should be a list of cus_code and for each cus_code, the number of products purchased by this customer. A more complex query (if you want to try it), would be to list the name of the customer, along with the cus_code.

This is a schema.

CREATE DATABASE IF NOT EXISTS donaldtrump;
USE donaldtrump;
CREATE TABLE IF NOT EXISTS Customer (
cus_code INT PRIMARY KEY,
cus_lname VARCHAR (20),
cus_fname VARCHAR (20),
cus_initial CHAR (1),
cus_areacode INT,
cus_phone INT);
CREATE TABLE IF NOT EXISTS Invoice (
   inv_number INT PRIMARY KEY,
cus_code INT,
inv_date DATE,
FOREIGN KEY (cus_code) REFERENCES Customer (cus_code) );
CREATE TABLE IF NOT EXISTS Vendor (
   vend_code INT PRIMARY KEY,
vend_name VARCHAR (30),
vend_contact VARCHAR (30),
vend_areacode INT,
vend_phone INT);
CREATE TABLE IF NOT EXISTS Product (
   prod_code INT PRIMARY KEY,
prod_desc VARCHAR (50),
prod_price INT,
prod_quant INT,
vend_code INT,
FOREIGN KEY (vend_code) REFERENCES Vendor (vend_code) );
CREATE TABLE IF NOT EXISTS Line (
   inv_number INT,
prod_code INT,
line_units INT,
PRIMARY KEY (inv_number, prod_code),
FOREIGN KEY (inv_number) REFERENCES Invoice (inv_number),
FOREIGN KEY (prod_code) REFERENCES Product (prod_code) );

INSERT INTO CUSTOMER VALUES (10010,"Johnson","Alfred","A",615,8442573);
INSERT INTO CUSTOMER VALUES (10011,"Dunne","Leona","K",713,8941238);
INSERT INTO CUSTOMER VALUES (10012,"Smith","Walter","W",615,8942285);
INSERT INTO CUSTOMER VALUES (10013,"Roberts","Paul","F",615,2221672);
INSERT INTO CUSTOMER VALUES (10014,"Orlando","Myla",NULL,615,2971228);

INSERT INTO Invoice VALUES (1001,10011,"2008-08-03");
INSERT INTO Invoice VALUES (1002,10014,"2008-08-04");
INSERT INTO Invoice VALUES (1003,10012,"2008-03-20");
INSERT INTO Invoice VALUES (1004,10014,"2008-09-23");

INSERT INTO Vendor VALUES (232, "Bryson", "Smith", 615, 2233234);
INSERT INTO Vendor VALUES (235, "Walls", "Anderson", 615, 2158995);
INSERT INTO Vendor VALUES (236, "Jason", "Schmidt", 651, 2468850);

INSERT INTO Product VALUES (12321, "hammer", 189 ,20, 232);
INSERT INTO Product VALUES (65781, "chain", 12, 45, 235);
INSERT INTO Product VALUES (34256, "tape", 35, 60, 236);
INSERT INTO Product VALUES (12333, "hanger", 200 ,10, 232);

INSERT INTO Line VALUES (1001,12321,1);
INSERT INTO Line VALUES (1001,65781,3);
INSERT INTO Line VALUES (1002,34256,6);
INSERT INTO Line VALUES (1003,12321,5);
INSERT INTO Line VALUES (1002, 12321, 6);

Solutions

Expert Solution

SQL QUERIES:  

GROUP BY clause is used to group the results of aggregate functions according to the specified columnName.

1.

SELECT COUNT(vend_code)

FROM Product

WHERE prod_price< 182;

2.

SELECT v.vend_code, v.vend_name, p.prod_desc, p.prod_quant

FROM Vendor v, Product p

WHERE v.vend_code= p.vend_code

AND p.prod_quant= (SELECT MIN(p.prod_quant) FROM Product p WHERE p.vend_code= v.vend_code);

3.

SELECT inv_number, COUNT(prod_code)

FROM Line

GROUP BY inv_no;

4.

SELECT cus_code, COUNT(inv_number)

FROM Invoice

GROUP BY cus_code;

5.

SELECT SUM(prod_price * prod_quant)

FROM Product;

6.

SELECT v.vend_code, v.vend_contact, COUNT(p.prod_code)

FROM Vendor v, Product p

WHERE v.vend_code= p.vend_code

GROUP BY p.vend_code;

7.

SELECT prod_desc, prod_price, vend_code

FROM Product

ORDER BY prod_price

LIMIT 1;

8.

SELECT l.inv_number, SUM(l.line_units * p.prod_price)

FROM Line l, Product p

WHERE l.prod_code= p.prod_code

GROUP BY l.inv_number;

9.

SELECT i.cus_code, COUNT(li.prod_code)  

FROM Line li, Invoice i

WHERE li.inv_number= i.inv_number

GROUP BY i.cus_code;


Related Solutions

In this assignment, you are required to write the SQL statements to answer the following queries...
In this assignment, you are required to write the SQL statements to answer the following queries using PostgreSQL system. The SQL statements comprising the DDL for Henry Books Database are given to you in two files. For that database, answer the following queries. Create the files Q1 to Q10 in PostgreSQL. Do follow the restrictions stated for individual queries. 1. List the title of each book published by Penguin USA. You are allowed to use only 1 table in any...
Question 1: Part 1 Write SQL statements for the following queries from the ‘EMPLOYEE’ table in...
Question 1: Part 1 Write SQL statements for the following queries from the ‘EMPLOYEE’ table in the WPC Database in MySQL: Display all records from the Employee table for employees working in the “Marketing” department. Display all records from the Employee table for employees working in the “Marketing” department OR “Finance” Department. Display the Last Names of all employees such that each last name appears only once. Display all the attributes for employees whose employee number is less than 10....
How to write a query in SQL using the Group by function to decide if the...
How to write a query in SQL using the Group by function to decide if the results of the diet worked. There are 6 columns ID, Gender, treatment, start_weight_kg, end_weight_kg, and correct change (end weight minus start weight). With the information given write a SQL to determine if the the results(correct change) worked. Question is not incomplete.
QUESTION 1) Which of the following statements is correct? Group of answer choices: a) Logistic regression...
QUESTION 1) Which of the following statements is correct? Group of answer choices: a) Logistic regression extends the idea of linear regression to the situation where the OUTCOME variable is categorical b) Logistic regression extends the idea of linear regression to the situation where a PREDICTOR variable is categorical c) Linear regression extends the idea of logistic regression to the situation where a PREDICTOR variable is categorical d) Linear regression extends the idea of logistic regression to the situation where...
answer the following question for oracle developer: 1- ____ is the category of SQL commands to...
answer the following question for oracle developer: 1- ____ is the category of SQL commands to create, alter or drop SQL objects (create a table, drop a view) 2-____ handles access to objects. The DBA might grant select on Customers to Bob. This will allow user Bob to read the Customers table. 3- Run this query in SQL Developer: Select sysdate from dual What is sysdate? What is dual? 4-What is a surrogate key? How are surrogate keys implemented in...
Answer the following questions using the Toyota Corporation as the focus of your assignment 1) Analyze...
Answer the following questions using the Toyota Corporation as the focus of your assignment 1) Analyze how your company is leveraging social media in their communications strategy. List which social media platforms are currently being used by your company (which may include profiles for specific brands or products), and provide an example demonstrating how the company utilizes social media to offer sensory experiences, engages customers socially, and for industrious purposes. 2) Imagine your company wants to run a campaign to...
1. write SQL statements to create the following two tables: a) Department table that contains the...
1. write SQL statements to create the following two tables: a) Department table that contains the following columns(dept_no,deptname,location) set the deptno to be the primary key. b) Employee table contains the following columns(emp_no,empname,deptno,salary)set the emp_no to be the primary key and dept_no to be the foreign key. 2. Write SQL statements to Insert the following 3 rows in Employee table:        (101,’Sami’,’D-101’,5000)        (102,’Yousef’,’D-101’,4000)        (103,’Sami’,’D-102’,7000) 3. Write SQL statements to Insert the following 3 rows in Department table:       ...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and...
Assignment 3C: Answer the following questions Question 1. a. Declare a 32-bit signed integer variable and initialize it with the smallest possible negative decimal value. b. Declare an uninitialized array of 100 16-bit unsigned integers. c. Declare a string variable containing the word “DVC” repeated 20 times, and terminated with the null char. Question 2 For the following declarations, assuming that the address of I is 404000h What are the addresses of J, K, and L? What is the total...
Question 1: The following table to answer the following questions.
Question 1: The following table to answer the following questions.  a. Sketch the aggregate supply(s) and aggregate demand diagram(s). b. What is the equilibrium output and price level? c. If aggregate demand shifts right, what is long-run equilibrium output? d. If aggregate demand shifts left, what is equilibrium long-run output? e. For an economy at long-run equilibrium, would you suggest using aggregate demand to alter the level of output or to control any inflationary increases in the price level? Why?
Please Answer the questions listed in the comment section, 1 and 2! -- ==================================================================== -- SQL...
Please Answer the questions listed in the comment section, 1 and 2! -- ==================================================================== -- SQL Script for Bookstore Database Records -- This script demonstrates the use of transactions for Unit 13 Exercise -- -- Created by: Jennifer Rosato -- Created on: 12/2013 -- Modified by: David Vosen -- Modified on: 11/2016 -- Modified on: 11/2018 Add DBCC LOG(CIS_3107_##, 1) to view -- transactions. -- ==================================================================== --SELECT * FROM customer; --SELECT * FROM invoice; --SELECT * FROM line; --SELECT *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT