Forum

Posted by Esin Seçil, Jan. 12, 2024, 3:29 a.m.

Top 2 films by category

Hello I have a question about Q64.

I had difficulty with this question. I try to find top 2 films by category. Then I checked the answer. But I didn't understand, why you used MAX(C.name) AS category. What MAX() stands for? Another subject is; Why payment table is our main table? I start to query with film table. Because the question starts with top 2 films. So, I think that I use I main table must be film. Can you help me, I will be so happy.

 

Thank you

Answers

Hi Esin Seçil,

Thank you for your insightful questions. Here are my answers, and please feel free to reach out if you have any more queries.

  1. Using MAX(C.name) is a common technique to fetch the name without needing to group. Since all entries share the same value, other aggregation functions like MIN would also work and give you the same result. We often use this trick and have seen many others use it as well. Since the name and the ID have a one-to-one correspondence, this method works effectively.

  2. You're absolutely right; there are various ways to tackle this problem, and please feel free to start from any table you prefer. For our official solution, we start with the Payment table. This approach allows us to omit movies that aren't generating revenue so we can improve the query effieiency, as the top two films are defined by their revenue.

Hope this helps,

Leon

SQLPad user avatar

Leon (949)

Jan. 12, 2024, 10:23 a.m.