Similar Problems
Similar Problems not available
Top Travellers - Leetcode Solution
Companies:
LeetCode: Top Travellers Leetcode Solution
Difficulty: Easy
Topics: database
The Top Travellers problem on leetcode is given as follows:
Suppose you have a database table UserTransaction with the following columns: +----------------+---------+ | Column Name | Type | +----------------+---------+ | Id | int | | User_Id | int | | Transaction | varchar | +----------------+---------+
You want to find all the users who have made at least one transaction. Each row of this table represents a transaction made by a user. Write a SQL query to output a list of distinct user Ids, who made at least one transaction, ordered in ascending order.
To solve this problem, we can use a simple SELECT statement to select all the distinct user Ids from the UserTransaction table who have made at least one transaction. We can also use the GROUP BY clause to group the transactions by user Ids and the COUNT function to count the total number of transactions made by each user. Finally, we can use the ORDER BY clause to sort the result in ascending order by user Id.
The SQL query to solve the Top Travellers problem looks like this:
SELECT DISTINCT User_Id FROM UserTransaction GROUP BY User_Id HAVING COUNT(Transaction) >= 1 ORDER BY User_Id ASC;
This query selects all the distinct user Ids from the UserTransaction table who have made at least one transaction, groups the transactions by user Ids, counts the total number of transactions made by each user, and finally, orders the result in ascending order by user Id.
In summary, the Top Travellers problem on leetcode can be solved by using a SQL query that selects all the distinct user Ids from the UserTransaction table who have made at least one transaction, groups the transactions by user Ids, counts the total number of transactions made by each user, and orders the result in ascending order by user Id.
Top Travellers Solution Code
1