Question: Supercloud Customer Analysis
"Using Supercloud usage logs (compute hours, storage used), write a query to find customers with the highest resource consumption."

Step-by-Step Explanation:

You have a table called supercloud_usage with:

  • customer_id
  • compute_hours (how many CPU hours they used)
  • storage_gb_used (how much storage they used in gigabytes)

You need to:

  1. Find customers who have the highest total resource consumption.
  2. Resource consumption should be based on both compute hours and storage used.

Sample Data (supercloud_usage):

customer_idcompute_hoursstorage_gb_used
101500200
102700100
103300500
104900300

Step 1: Define Resource Consumption

You could define total resource consumption as:

  • compute_hours + storage_gb_used

(Unless the company specifies weights like compute is more important — but here, assume equal weight.)

Example:

  • Customer 101: 500 + 200 = 700
  • Customer 102: 700 + 100 = 800
  • Customer 103: 300 + 500 = 800
  • Customer 104: 900 + 300 = 1200

Step 2: Calculate Total Resource Consumption

Add compute_hours and storage_gb_used for each customer.


Step 3: Rank Customers by Total Consumption

Sort them in descending order to find who consumed the most.


SQL Query:

SELECT
  customer_id,
  compute_hours,
  storage_gb_used,
  (compute_hours + storage_gb_used) AS total_resource_consumption
FROM
  supercloud_usage
ORDER BY
  total_resource_consumption DESC;


Explanation of SQL:

  • (compute_hours + storage_gb_used) calculates total resource consumption.
  • ORDER BY total_resource_consumption DESC sorts customers from highest to lowest consumption.

Expected Output:

customer_idcompute_hoursstorage_gb_usedtotal_resource_consumption
1049003001200
102700100800
103300500800
101500200700

Important Points:

  • Always clarify whether compute and storage should be treated equally.
    (Sometimes companies weight them differently, for example: 1 compute hour = 2 units.)
  • If there's a tie (e.g., 800 vs 800), both customers are ranked equally by amount but ordered arbitrarily.
  • You can extend the query by adding a RANK() if needed to officially rank them.

Example with ranks:

SELECT
  customer_id,
  compute_hours,
  storage_gb_used,
  (compute_hours + storage_gb_used) AS total_resource_consumption,
  RANK() OVER (ORDER BY (compute_hours + storage_gb_used) DESC) AS usage_rank
FROM
  supercloud_usage;