Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Second-Highest Usage per Customer
00:00
5 left

Second-Highest Usage per Customer

MediumSQL · PostgreSQL

Problem

Write a SQL query to find the second-highest energy consumption record for each utility customer from a transaction table at Smart Energy Water.

Treat second-highest as the second distinct consumption value for each customer. Include every transaction tied at that value.

Output

  1. One row per qualifying transaction, with customer_id, transaction_id, and energy_consumption.
  2. Include only customers with at least two distinct non-NULL consumption values.
  3. Order by customer_id, then transaction_id.

Schema

energy_transactions
ColumnTypeDescription
transaction_idPKINTUnique identifier for the energy transaction
customer_idVARCHAR(20)Utility customer identifier
transaction_dateDATEDate when the consumption was recorded
energy_consumptionNUMERIC(12,2)Energy consumed during the transaction period
Tablesenergy_transactions
Interviewer

Your question is Second-Highest Usage per Customer. Start with the requirements and the one table in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.