Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Tasks
9. Task 5

Instruction

Well done!

Next, let's see the number of states through which the biggest rivers flow.

Exercise

For each river that flows through more than four states, select its name and the number of states (name this column count_states) it goes through. Sort the result by the number of states, in descending order.

Stuck? Here's a hint!

In your SELECT, use the COUNT() aggregate function to count the number of states for each river. Join the tables river and river_state.

To group records and aggregate values, use GROUP BY with the name of the river.

In HAVING, use COUNT(rs.state_id) > 4.

In ORDER BY, use COUNT(rs.state_id).

There's more about COUNT() in the Aggregation section and more about HAVING in the Filter groups section of the SQL Basics course.