Leetcode Problem 2238. Number of Times a Driver Was a Passenger

2238. Number of Times a Driver Was a Passenger

Leetcode Solutions

Using DISTINCT, LEFT JOIN, and GROUP BY

  • Select distinct driver_id from the Rides table to get a list of all drivers.
  • Perform a LEFT JOIN on the Rides table with itself, joining on driver_id and passenger_id.
  • Use GROUP BY to group the results by driver_id.
  • Use COUNT to count the number of times each driver_id appears as a passenger_id in the joined table.
  • Use IFNULL to replace NULL counts with 0, ensuring that drivers who have never been passengers are included in the results with a count of 0.

erDiagram
    Rides {
        int ride_id PK
        int driver_id
        int passenger_id
    }

Using a Subquery for Counting Passenger Instances

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...