sqlbeginner10 minutes

Calculate Total Sales Per Employee Using SQL GROUP BY

Learn how to aggregate sales data by employee using the SQL GROUP BY clause and aggregate functions in this beginner-friendly challenge.

Challenge prompt

Given a table named Sales with columns employee_id (integer), sale_amount (decimal), and sale_date (date), write a SQL query to calculate the total sales amount for each employee. Your query should return two columns: employee_id and total_sales. Order the results by total_sales in descending order.

Guidance

  • Use the SUM() aggregate function to calculate total sales for each employee.
  • Group the results by employee_id using GROUP BY clause.
  • Order the results by total_sales in descending order to see the top performers first.

Hints

  • The syntax for GROUP BY is: GROUP BY column_name
  • Use ORDER BY total_sales DESC to sort highest to lowest.
  • Remember to name the aggregated column using AS, e.g., SUM(sale_amount) AS total_sales.

Starter code

SELECT employee_id, 
       -- Calculate total sales here
FROM Sales
GROUP BY employee_id
ORDER BY total_sales DESC;

Expected output

employee_id | total_sales ----------- | ----------- 1 | 4500.00 2 | 3200.50 3 | 2100.75

Core concepts

GROUP BYSUM()ORDER BY

Challenge a Friend

Send this duel to someone else and see if they can solve it.