Question

Write a SELECT statement that answers this question: What is the total quantity purchased for each...

  1. Write a SELECT statement that answers this question: What is the total quantity purchased for each instrument within each category? Return these columns:

The category_name column from the category table

The instrument_name column from the instruments table

The total quantity purchased for each instrument with orders in the order_instruments table

Use the WITH ROLLUP operator to include rows that give a summary for each category name as well as a row that gives the grand total.
Use the IF and GROUPING functions to replace null values in the category_name and instrument_name columns with literal values if they’re for summary rows.

Database Schema:
------------------------------
Tables: musicians, orders, order_instruments, instruments, categories

Table columns
-----------------------
musicians: musician_id, email_address, password, first_name, last_name, shipping_address_id, billing_address_id

orders: order_id, musician_id, order_date, ship_amount, tax_amount, ship_date, ship_address_id, card_type, card_number, card_expires, billing_address_id

order_instruments: item_id, order_id, instrument_id, item_price, discount_amount, quantity

instruments: instrument_id, category_id, instrument_code, instrument_name, description, list_price, discount_percent, date_added

categories: category_id, category_name

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below is the query required where group by is used to get sum of order quantity and rollup function to get the aggregation of all the sum of quantity for each category and instrument.

CASE WHEN is used as if condition to place literals 'category order qty' and 'instrument order qty' in place of null in the last row of aggregation.

SELECT CASE WHEN cat.category_name IS NULL
THEN 'category order qty'
ELSE cat.category_name
END,
CASE WHEN instr.instrument_name IS NULL
THEN 'instrument order qty'
ELSE instr.instrument_name
END,
SUM(ord_indtr.quantity)
FROM categories cat
INNER JOIN instruments instr ON cat.category_id = instr.category_id
INNER JOIN order_instruments ord_indtr ON instr.instrument_id = ord_indtr.instrument_id
GROUP BY ROLLUP(cat.category_name, instr.instrument_name)

Add a comment
Know the answer?
Add Answer to:
Write a SELECT statement that answers this question: What is the total quantity purchased for each...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Table structure: administrators = admin_id, email_address, password, first_name, last_name categories = category_id, category_name customers = customer_id,...

    Table structure: administrators = admin_id, email_address, password, first_name, last_name categories = category_id, category_name customers = customer_id, email_address, password, first_name, last_name, shipping_address_id, billing_address_id order_items = item_id, order_id, product_id, item_price, discount_amount, quantity orders = order_id, customer_id, order_date, ship_amount, tax_amount, ship_date, ship_address_id, card_type, card_number, card_expires, billing_address_id products = product_id, category_id, product_code, product_name, description, list_price, discount_percent, date_added 5. Write a SELECT statement that uses regular expression functions to get the username and domain name parts of the email addresses in the Administrators table. Return...

  • #6 Write a SELECT statement that returns these columns: The count of the number of orders...

    #6 Write a SELECT statement that returns these columns: The count of the number of orders in the Orders table The sum of the tax_amount columns in the Orders table Write a SELECT statement that returns one row for each category that has products with these columns: The category_name column from the Categories table The count of the products in the Products table The list price of the most expensive product in the Products table Sort the result set so...

  • I NEED TO WRITE THE FOLLOWING QUERIES IN MYSQL (13)Next, grant to a user admin the read privileges on the complete descr...

    I NEED TO WRITE THE FOLLOWING QUERIES IN MYSQL (13)Next, grant to a user admin the read privileges on the complete descriptions of the customers who submitted no orders. For example, these are the customers who registered themselves and submitted no orders so far. The granted privilege cannot be propagated to the other users. 0.3 (14)Next, grant to a user admin the read privileges on information about the total number of orders submitted by each customer. Note, that some customers...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT