- In this article will solve Highest Numbers of Products SQL question which is asked by eBay as per DataLemur website.
If you don’t know DatLemur then please do visit , it’s one of the best website to practice SQL questions and improve your SQL skills.
- We have been given user_transactions table that consist of orders made by eBay customers.
- We need to query to get top 3 users who have spent at-least $1000 in total.
- We first need to group all the transaction for each user and perform count of products that user purchased.
- Once we have number of product purchased by each user , we can filter based amount of money user spend . we can do that using Having clause doing sum(spend).
- Now all we have to filter top 3 records and we have to sort the records based on count of product and total spend.
- Here is the the final output.
select user_id, count(product_id) as product_num from user_transactions group by user_id having sum(spend) >=1000 order by product_num desc, sum(spend) desc limit 3
- Our solution get accepted by the platform.
- In this article, we solved question asked by eBay. In this particular question we learnt how to use having clause and apply sorted order many columns.
- If you want to upskill your SQL interview game, you can definitely check out this bestseller course