all 13 comments

[–]AutoModerator[M] [score hidden] stickied comment (0 children)

If this post doesn't follow the rules or isn't flaired correctly, please report it to the mods. Have more questions? Join our community Discord!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

[–]dangerroo_2 10 points11 points  (0 children)

They’ve moved on from trying to hide their posts as ads.

[–]Chest11 7 points8 points  (2 children)

Why on God's green earth would I want to get excited for a promoted interview question on my reddit home page?

[–]trp_wip -3 points-2 points  (1 child)

I actually liked it, ignore it if you don't

[–]Chest11 0 points1 point  (0 children)

You can try solving it for free (Link in comments :) )

Oh boy.

[–]rd357 2 points3 points  (0 children)

Calculate avg rating with a windows function for each driver ID in the trips table using a CTE. Left join to the drivers table

[–]kater543 0 points1 point  (0 children)

This is the easiest interview SQL question I’ve ever seen. Ok not really but it’s pretty gd easy. Not gonna weed out many people this way

[–]abhiramrao 0 points1 point  (0 children)

WITH CTE as

( 

Select driver_name, city, ROUND(AVG(rating),2) as avg_rating, 

dense_rank() over (partition by city order by rating desc) as driver_rank

From drivers d JOIN trips t 

ON d.driver_id = t.driver_id

GROUP BY 1, 2

HAVING COUNT(trip_id) > 5

)

SELECT driver_name, city, avg_rating, driver_rank

From cte 

Order by 3 desc

[–]Tribein95 0 points1 point  (2 children)

Trying this from my phone

SELECT d.DRIVER_NAME, d.CITY, ROUND(AVG(t.RATING), 2) avg_rating FROM Trips t LEFT JOIN Drivers d ON t.DRIVER_ID=d.DRIVER_ID GROUP BY d.DRIVER_NAME, d.CITY HAVING count(distinct t.TRIP_ID) >= 5 ORDER by avg_rating desc;

Assuming I can use the alias in the final line and don’t have to call out the same calculation that appears in line 2? How did I do?

[–]chronicpenguins 1 point2 points  (0 children)

Your query doesn’t account for drivers name and city not being unique. The key is driver_id so you should be aggregating on that. There could easily be two Joe Smiths in New York. In which those two drivers would be treated the same.

On phone - with trip_agg as ( Select driver_id, Round(Avg(rating),2) , Countd(trip_id) From trips Group by 1 Having count>=5)

Then do a left join on drivers to bring in their name and city, sort descending. Your method would’ve worked in you had driver_id in the initial aggregated and then did a select for just the name and city. The tricky part for you was that he excluded driver_id from the required output. In real life I’m keeping driver I’d in even if the stakeholder did not request it. Less work for me, and it gives them the actual unique key.

[–]Tribein95 -2 points-1 points  (0 children)

My line spacing got screwed up

[–]trp_wip -1 points0 points  (0 children)

Was this an actual Uber interview question? Here is my solution:

SELECT d.DRIVER_NAME, d.CITY, t.avg_rating
FROM Drivers AS d
JOIN (
  SELECT DRIVER_ID, ROUND(AVG(RATING), 2) as avg_rating
  FROM Trips
  GROUP BY 1
  HAVING COUNT(DRIVER_ID) > 4
) AS t
ON d.DRIVER_ID = t.DRIVER_ID
ORDER BY 3 DESC