Write a query to display the meter_number from the meter table which got minimum amount of total_units in a month. Display the records in ascending order based on their meter_number.Note:1. Display only the meter_number from the meter table which got minimum amount of total_units in a month.2. Tables involved are meter and bill3. Order by is mandatory4. Compare your executed result with the sample output given below
Question
Write a query to display the meter_number from the meter table which got minimum amount of total_units in a month. Display the records in ascending order based on their meter_number.Note:1. Display only the meter_number from the meter table which got minimum amount of total_units in a month.2. Tables involved are meter and bill3. Order by is mandatory4. Compare your executed result with the sample output given below
Solution
Here is the SQL query that you can use to get the desired result:
SELECT m.meter_number
FROM meter m
JOIN bill b ON m.meter_id = b.meter_id
GROUP BY m.meter_number
HAVING SUM(b.total_units) = (
SELECT MIN(total_units_sum)
FROM (
SELECT SUM(b.total_units) as total_units_sum
FROM meter m
JOIN bill b ON m.meter_id = b.meter_id
GROUP BY m.meter_number
) as subquery
)
ORDER BY m.meter_number ASC;
Here is the step by step explanation of the query:
- We join the
meterandbilltables onmeter_id. - We group the results by
meter_number. - We use the
HAVINGclause to filter the groups by the condition that the sum oftotal_unitsfor the group is equal to the minimum sum oftotal_unitsacross all groups. This minimum sum is calculated in a subquery. - Finally, we order the results by
meter_numberin ascending order.
Similar Questions
Write a query to display the meter_number from the meter table for which the bill is paid with maximum fine_amount of a month.Note:1. Display only the meter_number from the meter table for which the bill is paid with maximum fine_amount of a month.2. Tables involved are meter and bill3. Compare your executed result with the sample output given below.Sample output:
Write a query to display the meter_number from the meter table which got minimum total_units during 9PM for a day in the electricity_reading. Display the records in ascending order based on their meter_number.Note:1. Display only the meter_number from the meter table which got minimum total_units during 9PM. ( 9PM reading will be in h21 column).2. Tables involved are meter and electricity_reading3. Compare your executed result with the sample output given below.
Write a query to display all the 'bills' with 'meter' details. Display the records in ascending order based on meter number.Note:1. Display all the columns of bill and meter table2. Use Joins3. Tables involved bill and meter4. Order by is mandatory5. Compare your executed result with the sample output given below.
Write a query to display the month and the minimum total_units of electricity consumed on that month from the bill table. Give an alias name to the minimum total_units as 'minimum_units'.Note:1. Display only the month and minimum total_units of electricity consumed from bill table.2. Specify the alias name without any mistake.3. Group by month to get minimum total_units of every month.4. Compare your executed result with the sample output given below.
Write a query to get the owner_name from the building table who have consumed maximum total_units of current in a month. Display the records in ascending order based on their owner_name.Note:1. Display only the owner_name from the building table who have consumed maximum total_units of current in a month.2. Tables involved are building, meter and bill3. Order by is mandatory4. Compare your executed result with the sample output given below.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.