Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Ordering
Eliminating duplicate results
Aggregation
Grouping
HAVING: filtering and ordering groups
22. 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
  customer_id,
  order_date,
  SUM(total_sum)
FROM orders
GROUP BY customer_id, order_date
ORDER BY SUM(total_sum) 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 summary salaries. Highest values should appear first. Show the last name, the first name, and the sum.

Stuck? Here's a hint!

Type:

SELECT
  last_name,
  first_name,
  SUM(salary)
FROM employees
GROUP BY last_name, first_name
ORDER BY SUM(salary) DESC;