Starting from:

$30

Homework Number Three SQL Solution

Overview




This Project is worth 150 points (out of 1000) toward your final grade. It is due on Wednesday, March 6, at 11:59 p.m. Late submissions will be penalized 20% during a 4-day grace period up until Sunday, March 10, 11:59 p.m. After that time, no late work will be accepted. Your submission should be a document saved and submitted as a PDF file via the link found in the assignment section of the “Week 6 Moodle February 17 - 23” -- which is the same place where you found this file.




This assignment will give you hands-on practice in working with MySQL and the SQL language. In this Project you will create a database and populate it with data using the scripts provided. You will use the database you create for running some analytical queries and answering a few questions.




Objectives







Become familiar with the SQL language & syntax for SELECT queries, DDL and DML



Become familiar with a tool of your choice for building and submitting queries (whether command mode or GUI.)



Successfully run the scripts necessary to create a sample database, verify that your database is correctly built.



Run SQL queries against your database to answer the assigned problems.









Step One: Downloading and Installing MySQL




For this project assignment you will need to download and install MySQL on your computer. How you do this will depend on what type of computer you have and what Operating System it is running.




You will want to download MySQL Community Server 8.0. * The current release number changes from time to time. As of this writing, the most recent release is 8.0.12. If for some reason you don’t want to use version 8.0 – for example if you already have version 5.7 running on your computer, you can use version 5.7.x. Either version will work fine.




Do NOT download “MySQL Cluster” software -- similar name, but a VERY different DBMS product.




This page (below) contains online documentation links where you can find help with the download and installation if you need it.




The download you need can be found here: https://dev.mysql.com/downloads/mysql/










CSCI3287 Database Systems













Homework Number Three – SQL







Choose the download file that matches your computer’s OS and version.




Once you have downloaded and installed MySQL, you should launch the MySQL instance so that it is running in the background on your computer.




Step Two: Choose Your Preferred Query Editor Tool




In order to create SQL queries and run them against your MySQL database, you will need a tool or a user interface through which you can create and execute queries, and then view/copy/export the answer set.




The default is the mysqld command line interface. This command line interface is installed with MySQL and is similar to using the Linux shell. If you choose to do your queries via the MySQL command line interface, you do not need to download/install any query tool.




However, managing and running queries against MySQL databases is simpler, faster and easier if you use a GUI (graphic user interface) tool. There are many available.







You can use MySQL Workbench. You have seen your instructor use MySQL Workbench in class to create an ERD (Entity Relationship Diagram) data model and then generate SQL to create tables. MySQL Workbench is free. You can use MySQL Workbench to build and submit queries against your database. It is available here: https://dev.mysql.com/downloads/workbench/




MySQL offers versions for Windows, Linux, MAC.




A great open source alternative is DBeaver. The community edition is free and it comes with versions for many different OS builds and works fine with MySQL. https://dbeaver.jkiss.org/download/




Another alternative for MAC users is DataGrip. https://www.jetbrains.com/datagrip/ They offer a free 30-day trial, and a special free edition for students that you can sign up for.




Step Three: Creating the Database




Once you have selected your query editor, you need to download the “ClassicModelsCreate” script file from Moodle, unzip it and execute it. It will create your database and tables, and then load the tables with data. The script runs fine as-is without any modification.




Before you can create your database, you need to make sure that your instance of MySQL is running in the background.




Then using your query editor, you must connect to the running MySQL instance prior to running the script.




HINT: You should download and print this ERD (below) and keep it handy when you are writing your queries. It is very helpful to have table and column names in front of you when writing SQL queries.







MySQL Sample Database Schema




The MySQL sample “Classic Models” database schema consists of the following tables:







Customers
stores customer data.
Products
stores a list of scale model cars.
ProductLines
stores a list of product line categories.
Orders
stores sales orders placed by customers.
OrderDetails
stores sales order line items for each sales order.
Payments
stores payments made by customers based on their accounts
Employees
stores all employee information as well as the organization structure such as
Offices
who reports to whom.
stores sales office data.

























































CSCI 3287 Database Systems Page 3

CSCI3287 Database Systems













Homework Number Three – SQL


























































































































































Note: GUI Query Editor Users: After you run some queries to create your tables, you might expect the new tables to immediately appear under the “object explorer” on the left side of your GUI query editor. They will eventually show up, but to see them appear right away, you will need to click on the “refresh” icon (if your tool has one.) If this icon does not appear, then click somewhere within your “object explorer” and the newly created tables should appear. (Depends on your query tool…)










CSCI 3287 Database Systems Page 4

CSCI3287 Database Systems













Homework Number Three – SQL










If you are using the command line editor, you can enter SHOW TABLES and MySQL will show you all the tables in your database.




After running the unzipped script file to create and load your database, you must run the following “Verify” script to ensure that your database is built correctly.




Verify Script:










SELECT table_schema, table_name, table_rows




FROM information_schema.tables




WHERE TABLE_SCHEMA LIKE 'classic%';







You should see the following tables and row counts for your Classic Models database.







table_schema


table_name table_rows
classicmodels


customers
122
classicmodels


employees
23
classicmodels


offices
7
classicmodels


orderdetails
2996
classicmodels


orders
326
classicmodels


payments
273
classicmodels


productlines
7
classicmodels


products
110






Preparing Your Assignment Submission




Your results for this homework assignment should be captured in a document (such as a .txt file, MS Word or similar tool.) Please then save your final deliverable document as a PDF. Your submission should be a document saved and submitted as a PDF file via the link found in the assignment section of the “Week 6 Moodle February 17 - 23” -- which is the same place where you found this file.




You must turn in BOTH your SQL Code and your ANSWER SET (unless otherwise specified.)






















CSCI 3287 Database Systems Page 5

CSCI3287 Database Systems













Homework Number Three – SQL







For each problem where a multi-row answer set is created, the number of rows you should expect in your answer set is listed in parentheses after the problem/question. Some queries will product NO answer set.




Query Problems




For this project you must create and execute queries against the ClassicModels database to fulfill the requirements listed below. For each query requirement, as a “hint”, the number of rows to expect in your answer set is listed in parentheses.




List the Product Lines in the Product Lines table. (7)



List the EmployeeNumber, LastName, FirstName, Extension for all employees working out of the San Francisco office. (6)



List the ProductCode, ProductName, ProductVendor, QuantityInStock for all products in the “Vintage Cars” product line with a QuantityInStock between 1000 and 5000. (8)



(Use a SUBQUERY) List the ProductCode, ProductName, ProductVendor, BuyPrice and MSRP for the least expensive (lowest MSRP) product sold by ClassicModels. (“MSRP” is the Manufacturer’s Suggested Retail Price.) (1)



What is the ProductName and Profit of the product that has the highest profit (profit = MSRP minus BuyPrice). (1)



List the country and the number of customers from that country for all countries having between 4 and 12 customers. List the countries sorted in descending order from highest to lowest number of customers. Title the column heading for the count of customers as “Customers”. (6)



List the ProductCode, ProductName, and number of orders for the product with the most orders. Title the column heading for the count of orders as “OrderCount”. (1)



List the EmployeeNumber, Firstname + Lastname (concatenated into one column in the answer set, separated by a blank and referred to as ‘name’) for all the employees reporting to Diane Murphy or Gerard Bondur. (8)



List the EmployeeNumber, LastName, FirstName of the president of the company (the one employee with no boss.) (1)



List the ProductName for all products in the “Vintage Cars” product line from the 1930’s. (12)















CSCI 3287 Database Systems Page 6

CSCI3287 Database Systems













Homework Number Three – SQL







List the month name and the total number of orders for the month in 2003 in which ClassicModels customers placed the most orders. (1)



List the firstname, lastname of employees who are Sales Reps who have no assigned customers. (2)



List the customername of customers with no orders who are not from Germany. (14)



List the customername and total quantity of products ordered for customers who have ordered fewer than 400 products across all their orders. (8)



Create a NEW table named “TopCustomers” with three columns: CustomerNumber (integer), ContactDate (DATE) and OrderTotal (a decimal number with 9 digits in total having two decimal places). None of these columns can be NULL. Include a PRIMARY KEY constraint named “TopCustomer_PK” on CustomerNumber. (no answer set)



Populate the new table “TopCustomers” with the CustomerNumber, today’s date, and the total value of all their orders (PriceEach * quantityOrdered) for those customers whose order total value is greater than $140,000. (inserted 10 rows, no answer set)



List the contents of the TopCustomers table in descending OrderTotal sequence. (10)



Add a new column to the TopCustomers table called OrderCount (integer). (No answer set)



Update the Top Customers table, setting the OrderCount column to a random number (from 0 to



18). (Should update 10 rows) HINT: use the RAND() and FLOOR() functions.




List the contents of the TopCustomers table in descending OrderCount sequence. (10)



Drop the TopCustomers table. (no answer set)


































































CSCI 3287 Database Systems Page 7

More products