Summarizing Data with GROUP BY
Welcome to this coding lab! In this lab, you'll be using SQLite and the Chinook database to tackle SQL challenges that test your skills in various SQL concepts.
Database Schema
Before you begin, familiarize yourself with the schema of the Chinook database. The database includes several tables, but for this lab, you'll be focusing on the "Invoice" table. The relevant fields in this table are:
- "InvoiceId" (Primary Key)
- "CustomerId" (Foreign Key to Customer)
- "Total"
Instructions
To successfully complete the challenge, please adhere to the following:
- Stick to the exact names for tables and columns as defined in the Chinook database schema. Incorrect naming will result in a failed lab.
- Use double quotes around table and column names for clarity.
- Make sure to include an "ORDER BY" clause in your SQL query to avoid output mismatch.
Challenge
Group invoices by customer IDs and calculate the summed-up total amounts.
Table Used: Invoice
Task Description:
In this challenge, you need to group invoices by their CustomerId
and calculate the total amount billed to each customer. Make sure to name the columns explicitly in your query.
Output Requirements:
-
The final output should have two columns:
-
CustomerId
-
TotalAmount
: The total amount should be the sum of theTotal
column for eachCustomerId
.
-
Ordering Requirements:
- Order your output by
CustomerId
in ascending order.