Skip to content
Advertisement

Write an SQL query that reports the best seller by total sales price, If there is a tie, report them all

I’m doing this leetcode question on SQL, and I have some questions that I wanted to ask you.

This is my solution:

My questions are:

  1. First, is my solution true?
  2. Is it necessary select distinct * from sales ? I do it whenever I see This table has no primary key, it can have repeated rows. The others didn’t do it this way, so I’m wondering whether I did it correctly, and it bothered me a lot.
  3. I’m new with SQL, and I write everything using only select, from, where, group by, having, order by. I’m not confident to use for example rank()/row_number(),... over(partition by)..., so my solutions are usually obviously longer than those given by others. I would like to know if the queries written as my way take indeed (a lot) more time to execute? (or how to know/compute the running time of a query?).

Thanks.

Advertisement

Answer

In SQL Server you can use TOP(1) WITH TIES to return not just 1 row, but all top rows with the same total value:

No, you do not need DISTINCT. It does not just make the query slower, it may produce incorrect results. In your case it doesn’t matter if the sales table has a primary key or not.

Your query would produce correct results if you remove DISTINCT.

If you use window function like rank() over() the query would likely be faster than your version, but a lot depends on the actual DBMS that you use, available indexes and other things. Most DBMS have methods to see how they run/execute the query. In SQL Server you can see the so-called execution plan, which gives very good idea of what the engine does to produce the result, how much data from which tables and how many times it reads, what transformations and calculations it performs.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement