Table Schema
Inspect
Table
| Column | Type |
|---|---|
| usage_id | integer |
| user_id | integer |
| resource_type | varchar |
| compute_hours | decimal |
Find the top 3 users consuming the most compute hours.
Table Schema
| Column | Type |
|---|---|
| usage_id | integer |
| user_id | integer |
| resource_type | varchar |
| compute_hours | decimal |
Sample Data
| usage_id | user_id | resource_type | compute_hours |
|---|---|---|---|
| 1 | 101 | VM | 20 |
| 2 | 102 | VM | 30 |
| 3 | 101 | Storage | 10 |
| 4 | 103 | VM | 50 |
| 5 | 104 | VM | 40 |
| 6 | 101 | VM | 25 |
| user_id | total_compute_hours |
|---|---|
| 101 | 55 |
| 103 | 50 |
| 104 | 40 |
SQL Editor
| user_id | total_compute_hours |
|---|---|
| 101 | 55 |
| 103 | 50 |
| 104 | 40 |
Hints
Solution
Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.
SELECT user_id, SUM(compute_hours) AS total_compute_hours FROM cloud_usage GROUP BY user_id ORDER BY total_compute_hours DESC LIMIT 3;
Explanation
Read the expected output columns to determine the final grain.
Aggregate or rank the input rows to calculate the requested metric.
Filter, sort, and alias the final columns to match the output.
Related Questions