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

Instruction

Time for a break? No? Then here comes the next exercise!

Correlated subqueries can be used to find the best object in a certain category. Here we select orchestras which have the best rating among orchestras coming from the same city:

SELECT
  name,
  city_origin,
  rating
FROM orchestras o1
WHERE rating = (SELECT MAX(rating) FROM orchestras o2 WHERE o1.city_origin = o2.city_origin)

In the subquery, we select the maximal rating for all orchestras which come from the same city as the orchestra in the outer query. Note that we use aliases o1, o2 to distinguish between subquery orchestra and outer query orchestra. In the outer query we select orchestras which have rating equal to the rating found in the subquery.

Exercise

Select the name, wage, and experience of all members who earned the most within each orchestra.

Stuck? Here's a hint!

The query is similar to the example. In the subquery select the maximal wage for all members coming from the orchestra as the member in the outer query. Use the = operator to select members with the same wage.