Ordering
Limiting the output
Eliminating duplicate results
Aggregation
Grouping
HAVING: filtering and ordering groups
24. Order groups
Let's practice

Instruction

Correct! There's one more thing before you go. Groups can be sorted just like rows. Take a look:

SELECT
  CustomerId,
  OrderDate,
  SUM(TotalSum) AS CustomerDailySum
FROM Order
GROUP BY CustomerId,
  OrderDate
ORDER BY SUM(TotalSum) DESC;

In this case, we'll order our rows according to the total daily sum of all orders by a specific customer. The rows with the highest value will appear first.

Exercise

Sort the employees according to their total salaries at the company. The greatest values should appear first. Show the last name, first name, and the sum. Name the column TotalSalary.

Stuck? Here's a hint!

Type:

SELECT
  LastName,
  FirstName,
  SUM(Salary) AS TotalSalary
FROM Employee
GROUP BY LastName,
  FirstName
ORDER BY SUM(Salary) DESC;