User’s Third Transaction — SQL Questioned Asked By Uber

  • Post last modified:September 11, 2022
  • Reading time:1 mins read
Photo by Caspar Camille Rubin on Unsplash

Introduction

  • In this article we will solve Teams Power Users SQL question which is asked by Microsoft 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.

Question

  • We have been given user_transactions input table. our goal is to find out 3rd transaction made by each user.

Solution

  • First we will partition uber_transaction table based on user_id, we will add row_number as column to rank each record.
  • Here is output.
  • Now once we have our base table is ready , we can return cases where row_number is 3

Final Solution

select user_id,spend,transaction_date from(
  select user_id,spend,transaction_date, 
  row_number() over(partition by user_id order by transaction_date) as rn
  from transactions
) x where rn = 3

Submission

  • Our solution is accepted by the platform.

Conclusion

  • In this article we solved SQL question asked by Uber. we used window operation and used row_number function to rank each record in the partition.

Leave a Reply