Demonstrate Your Knowledge of SQL

Prompt 1 Tables

A community college uses the following tables to track each student’s progress:

                     Class

class_id (p)class_name
101Geometry
102English
103Physics

                          Student

student_id (p)first_namelast_name
500RobertSmith
762FrankCarter
881JosephEvans
933AnneBaker

                                   Enrollment

class_id (p)(f)student_id (p)(f)semester (p)grade
101500Fall 2019A
102500Fall 2019B
103762Fall 2019F
101881Spring 2020B
102881Fall 2020B
103762Spring 2021

Prompt 1 Questions

Answer the following questions by constructing a single query without using subqueries, unless otherwise instructed.

1. Write a query to retrieve all columns from the Enrollment table where the grade of A or B were assigned.

2. Write a query to return the first and last names of each student who has taken Geometry.

3. Write a query to return all rows from the Enrollment table where the student has not been given a failing grade (F).  Include any rows where the grade has not yet been assigned.

4. Write a query to return the first and last name of every student, along with the grade received in English if the student has ever enrolled in that class.  You need only include the Enrollment and Student tables, and may specify the class_id value of 102 for the English class.

5. Write a query to return the total number of students who have ever been enrolled in each of the classes.

6. Write a statement to modify Robert Smith’s grade for the English class from a B to a B+.  Specify the student by his student ID, which is 500, and the English class by class ID 102.

7. Create an alternate statement to modify Robert Smith’s grade in English, but for this version specify the student by first/last name, not by student ID.  This will require the use of a subquery.

8. A new student name Michael Cronin enrolls in the Geometry class.  Construct a statement to add the new student to the Student table (you can pick any value for the student_id, as long as it doesn’t already exist in the table).

9. Add Michael Cronin’s enrollment in the Geometry class to the Enrollment table.  You may only specify names (e.g. “Michael”, “Cronin”, “Geometry”) and not numbers (e.g. student_id, class_num) in your statement.  You may use subqueries if desired, but the statement can also be written without the use of subqueries. Use ‘Spring 2020’ for the semester value.

10. Write a query to return the first and last name of all students who have not enrolled in any class.  Use a correlated subquery against the Enrollment table.

11. Return the same results as the previous question (first and last name of all students who have not enrolled in any class), but formulate your query using a non-correlated subquery against the Enrollment table.

12. Write a statement to remove any rows from the Student table where the person has not enrolled in any classes.  You may use either a correlated or non-correlated subquery against the Enrollment table.

Prompt 2 Tables

The Customer_Order table, which stores data about customer orders, contains the following data:

                  Customer_Order

order_numcust_idorder_date
112101-15-2019
223407-24-2019
333605-02-2020
412101-15-2019
533603-19-2020
623407-24-2019
712101-15-2019
833606-12-2020

Prompt 2 Questions

1. Write a query to retrieve each unique customer ID (cust_id) from the Customer_Order table.  There are multiple ways to construct the query, but do not use a subquery.

2. Write a query to retrieve each unique customer ID (cust_id) along with the latest order date for each customer.  Do not use a subquery.

3. Write a query to retrieve all rows and columns from the Customer_Order table, with the results sorted by order date descending (latest date first) and then by customer ID.

4. Write a query to retrieve each unique customer (cust_id) whose lowest order number (order_num) is at least 3.  Do not use a subquery.

5. Write a query to retrieve only those customers who had 2 or more orders on the same day.  Retrieve the cust_id and order_date values, along with the total number of orders on that date.  Do not use a subquery.

6. Along with the Customer_Order table, there is another Customer table below. Write a query which returns the name of each customer who has placed exactly 3 orders.  Do not return the same customer name more than once, and use a correlated subquery against Customer_Order to determine the total number of orders for each customer:

                            Customer

cust_idcust_name
121Acme Wholesalers
234Griffin Electric
336East Coast Marine Supplies
544Sanford Automotive

   7. Construct a different query to return the same data as the previous question (name of each customer who has placed exactly 3 orders), but use a non-correlated subquery against the Customer_Order table.

7. Write a query to return the name of each customer, along with the total number of orders for each customer.  Include all customers, regardless of whether or not they have orders. Use a scalar, correlated subquery to generate the number of orders.

Demonstrate Your Knowledge of Advanced SQL

Prompt: A manufacturing company’s data warehouse contains the following tables.

                                          Region

region_id (p)region_namesuper_region_id (f)
101North America
102USA101
103Canada101
104USA-Northeast102
105USA-Southeast102
106USA-West102
107Mexico101

                    Product

product_id (p)product_name
1256Gear – Large
4437Gear – Small
5567Crankshaft
7684Sprocket

                                        Sales_Totals

product_id (p)(f)region_id (p)(f)year (p)month (p)sales
1256104202011000
4437105202021200
768410620203800
1256103202042200
4437107202051700
768410420206750
1256104202071100
4437105202081050
768410620209600
12561032020101900
44371072020111500
7684104202012900

Answer the following questions using the above tables/data:

1. The database designer included columns for Year and Month in the Sales_Totals table, but forgot to include a column for Quarter.  Write a CASE expression which can be used to return the quarter number (1, 2, 3, or 4) using other column values from the table.

2. Write a query which will pivot the Sales_Totals data so that there is a column for each of the 4 products containing the total sales across all months of 2020.  It is OK to include the product_id values in your query, and the results should look as follows:

tot_sales_large_gearstot_sales_small_gearstot_sales_crankshaftstot_sales_sprockets
6200545003050

3. Write a query which retrieves all columns from the Sales_Totals table, along with a column called sales_rank which assigns a ranking to each row based on the value of the Sales column in descending order.

4. Write a query which retrieves all columns from the Sales_Totals table, along with a column called product_sales_rank which assigns a ranking to each row based on the value of the Sales column in descending order, with a separate set of rankings for each product.

5. Expand on the query from question #4 by adding logic to return only those rows with a product_sales_rank of 1 or 2.

6. Write a set of SQL statements which will add a row to the Region table for Europe, and then add a row to the Sales_Total table for the Europe region and the Sprocket product (product_id = 7684) for October 2020, with a sales total of $1,500.  The statements should be executed as a single unit of work.

7. Write a statement to create a view called Product_Sales_Totals which will group sales data by product and year.  Columns should include product_id, year, product_sales, and gear_sales, which will contain the total sales for the “Gear – Large” and “Gear Small” products (should be generated by an expression, and it is OK to use the product_id values in the expression).

8. Write a query to return all sales data for 2020, along with a column showing the percentage of sales for each product.  Columns should include product_id, region_id, month, sales, and pct_product_sales.

9. Write a query to return the year, month, and sales columns, along with a 4th column named prior_month_sales showing the sales from the prior month.  There are only 12 rows in the sales_totals table, one for each month of 2020, so you will not need to group data or filter/partition on region_id or product_id.

10. If the tables used in this prompt are in the ‘sales’ database, write a query to retrieve the name and type of each of the columns in the Product table.

"Get 15% discount on your first 3 orders with us"
Use the following coupon
FIRST15

Order Now