[Solution] Return the second fastest runner

Click here to view the question


Solution:

Apply a limit and offset with an order by to return the second fastest runner

select
  name
  , seconds
from
  runners
order by
  seconds
limit 1 offset 1

Concepts Covered: Order by, Limit, Offset

Explanation:

We first need to arrange runners from fastest to slowest. This is done by applying "order by seconds." By default, SQL arranges numerical values from lowest to highest. If we had wanted to arrange numerical values from highest to lowest, we would run "order by second desc"

Then we apply a limit 1 to ensure our output only returns 1 row. If we wanted to return the fastest runner, we would be done! However, we need to apply an offset 1 to have SQL skip the first row and return the second. If we wanted to return the third fastest runner, we would have applied an offset 2. For the fourth fastest runner, we would have applied an offset 3, and so on.

Reply Oldest first
  • Oldest first
  • Newest first
  • Active threads
  • Popular
Like Follow
  • 3 mths agoLast active
  • 139Views
  • 1 Following