SQL
Lab
Microsoft Microsoft Azure Interview Question 05

Cloud
Resource
Usage

Find the top 3 users consuming the most compute hours.

Table Schema

Inspect
Table

interactive
ColumnType
usage_idinteger
user_idinteger
resource_typevarchar
compute_hoursdecimal

Sample Data

Input
Output

Sample Input: cloud_usage
usage_iduser_idresource_typecompute_hours
1101VM20
2102VM30
3101Storage10
4103VM50
5104VM40
6101VM25
Expected Output
user_idtotal_compute_hours
10155
10350
10440

SQL Editor

Run
Query

postgresql
Waiting for query

user_idtotal_compute_hours
10155
10350
10440

Hints

Unlock
Clues

Hint 01: Identify the grouping level required by the output.
Hint 02: Aggregate with COUNT, SUM, AVG, or a window function as needed.
Hint 03: Filter after aggregation with HAVING or after ranking with an outer query.

Solution

Locked
Answer

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

Step By
Step

01

Read the expected output columns to determine the final grain.

02

Aggregate or rank the input rows to calculate the requested metric.

03

Filter, sort, and alias the final columns to match the output.

Related Questions

Keep
Solving