Write a query to display the owner_name and sum of payable_amount who paid the maximum bill amount in the year 2017. If there are multiple records display the record in ascending order based on owner_name Give an alias name as "TotalBillAmount".Note:1. Display only the owner_name and sum of payable_amount who paid the maximum bill amount in the year 2017.2. Use Joins3. Tables involved building, meter and bill4. Specify condition on year column of bill table. sum of payable_amount must be maximum in this year.5. Order by is mandatory. 6. Compare your executed result with the sample output given below.
Question
Write a query to display the owner_name and sum of payable_amount who paid the maximum bill amount in the year 2017. If there are multiple records display the record in ascending order based on owner_name Give an alias name as "TotalBillAmount".Note:1. Display only the owner_name and sum of payable_amount who paid the maximum bill amount in the year 2017.2. Use Joins3. Tables involved building, meter and bill4. Specify condition on year column of bill table. sum of payable_amount must be maximum in this year.5. Order by is mandatory. 6. Compare your executed result with the sample output given below.
Solution
It seems like you're asking for a SQL query. Here's a step-by-step guide on how to write it:
- First, you need to join the three tables. You can do this using the JOIN keyword. The tables are 'building', 'meter', and 'bill'. You need to find a common column between these tables to join them. Let's assume 'building_id' is a common column between 'building' and 'meter', and 'meter_id' is a common column between 'meter' and 'bill'.
SELECT building.owner_name, SUM(bill.payable_amount) AS TotalBillAmount
FROM building
JOIN meter ON building.building_id = meter.building_id
JOIN bill ON meter.meter_id = bill.meter_id
- Next, you need to add a WHERE clause to filter the records for the year 2017. Let's assume 'bill_date' is a column in 'bill' table which stores the date of the bill.
WHERE YEAR(bill.bill_date) = 2017
- Then, you need to GROUP BY 'owner_name' to get the sum of 'payable_amount' for each owner.
GROUP BY building.owner_name
- To get the owner who paid the maximum bill amount, you can use a subquery in the HAVING clause to compare the sum of 'payable_amount' with the maximum sum of 'payable_amount'.
HAVING SUM(bill.payable_amount) = (SELECT MAX(SUM(payable_amount)) FROM bill WHERE YEAR(bill_date) = 2017)
- Finally, you need to ORDER BY 'owner_name' in ascending order.
ORDER BY building.owner_name ASC;
So, the complete SQL query would be:
SELECT building.owner_name, SUM(bill.payable_amount) AS TotalBillAmount
FROM building
JOIN meter ON building.building_id = meter.building_id
JOIN bill ON meter.meter_id = bill.meter_id
WHERE YEAR(bill.bill_date) = 2017
GROUP BY building.owner_name
HAVING SUM(bill.payable_amount) = (SELECT MAX(SUM(payable_amount)) FROM bill WHERE YEAR(bill_date) = 2017)
ORDER BY building.owner_name ASC;
Please replace the column names and table names with the actual ones in your database.
Similar Questions
Write a query to display owner name,address ,meter number and payable amount of all the bill which are all not having fine_amount and that are generated for 2017 December. Display the records in ascending order based on owner name.Note:1. Display only the below 4 columns, i. owner name ii. address iii. meter number iv. payable_amountwith the condition not having fine_amount that are generated for 2017 December.2. Use Joins3. Tables involved building, meter and bill4. Specify condition on year, month and fine_amount columns of bill table. year must be 2017, month must be 12 and fine_amount should be not null5. Order by is mandatory. 6. Compare your executed result with the sample output given below.
Write a query to display owner name,address ,meter number and total units consumed by all the customers in the month december 2017.Display the records in descending order by total_units.Note:1. Display only the below 4 columns, i. owner name ii. address iii. meter number iv. Total unitsby all the customers in the month december 2017.2. Use Joins3. Tables involved building, meter and bill4. Specify condition on year, month column of bill table5. Order by is mandatory.
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.Sample output: [Only few records are shown here]ID METER_ID MONTH YEAR DUE_DATE TOTAL_UNITS PAYABLE_AMOUNT IS_PAYED PAYMENT_D FINE_AMOUNT ID METER_NUMBER BUILDING_ID39 26 8 2017 01-SEP-17 20700 724500 1 14-SEP-17 72450 26 SG190123 2614 10 10 2017 01-NOV-17 750 16875 1 09-NOV-17 1687.5 10 SG198329 10
Write a query to display the meter_number,owner_name and address of the owner who paid fine most number of times. If there are multiple records display in ascending order based on owner_name.Note:1. Display only the below 3 columns, i. meter_number ii. owner_name iii. address with the condition who paid fine most number of times.2. Use Joins3. Tables involved are building, meter and bill4. Order by is mandatory.5. 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.