cswork / libraryOpen practice workspace

Coding questions · IBM

Find Customers Whose Average Website Resource Usage Exceeds 50 Percent

Practice PostgreSQL aggregation with HAVING, customer-level average resource utilization, strict thresholds, two-decimal rounding, and ascending email output.

Write one read-only PostgreSQL query that reports customers whose average website resource usage exceeds 50 percent.

Schema

TableColumnTypeMeaning
customersidINTPrimary key
customersemailVARCHAR(255)Customer email
site_metricscustomer_idINTForeign key to customers.id
site_metricscpu_usageDECIMAL(5,2)CPU usage percentage
site_metricsmemory_usageDECIMAL(5,2)Memory usage percentage
site_metricsdisk_usageDECIMAL(5,2)Disk usage percentage

Task

  • Calculate each customer's average CPU, memory, and disk usage across all of that customer's site-metric rows. Return one row per qualifying customer, even if customers share an email address.
  • Include a customer if at least one of the three averages, before rounding, is strictly greater than 50. Apply this condition to the aggregated customer results with HAVING; do not filter individual metric rows by resource usage before averaging.
  • Return the columns email, average_cpu_usage, average_memory_usage, and average_disk_usage, in that order. Round each displayed average to two decimal places.
  • Sort rows by email in ascending order. The relative order of rows with the same email is unspecified.

Find Customers Whose Average Website Resource Usage Exceeds 50 Percent

Table customers

columntypeconstraints
idINTEGERPRIMARY KEY
emailVARCHAR(255)

Sample data:

01
1zoe@example.test
2amy@example.test
3li@example.test
4quiet@example.test

Table site_metrics

columntypeconstraints
customer_idINTEGERREFERENCES customers(id)
cpu_usageDECIMAL(5,2)
memory_usageDECIMAL(5,2)
disk_usageDECIMAL(5,2)

Sample data:

0123
1203560
1403570
2702020
2102020
3105520
3106520

Expected output

emailaverage_cpu_usageaverage_memory_usageaverage_disk_usage
li@example.test106020
zoe@example.test303565

Hints

  1. Keep each customer's metric rows together when calculating resource averages.
  2. Compare the underlying averages with the threshold before formatting the displayed values.