Forum

Posted by Cyn, Feb. 2, 2022, 9:44 p.m.

#15 Why is CEILING((AVG(DATE(return_ts) - DATE(rental_ts)))) wrong?

Hi,
I probably didn't quite catch this question, but why this: CEILING((AVG(DATE(return_ts) - DATE(rental_ts)))) is wrong? My understanding is we need to round up the average days.

Answers

The problem is really asking you to properly handle scenarios where the rentals are longer than exactly one day. With taking the ceiling of the average, you're finding the average of all the rentals first, then rounding that up (in other words, likely far less than usual). You might be able to swap the ceiling and avg commands, but you also need to extract the days portion to make that work.
One note to consider when working through these problems, especially if you're preparing for interviews, is to consider how clear your query would appear to the interviewer. This isn't necessarily something you need to do immediately, but being able to refactor a solution can give you a greater understanding of it and be able to explain it to others. That portion is critical to interviewers trying to understand exactly what you're doing. Think of it as when math teachers ask to see your work - it's as much about your thought process and experience as it is being able to get the right answer.
SQLPad user avatar

Mike (228)

Feb. 3, 2022, 8:55 a.m.