Write a query to display the number of bills from the bill table which are payed for the building with connection_type 'Commercial'. Give an alias name as 'payment_count'.Note:1. Display only the number of bills from the bill table which are payed(is_payed=1) for the building with connection_type 'Commercial'.2. Tables involves are bill, meter, building, building_type and electricity_connection_type.3. Specify the alias name without any mistake.4. Compare your executed result with the sample output given below.
Question
Write a query to display the number of bills from the bill table which are payed for the building with connection_type 'Commercial'. Give an alias name as 'payment_count'.Note:1. Display only the number of bills from the bill table which are payed(is_payed=1) for the building with connection_type 'Commercial'.2. Tables involves are bill, meter, building, building_type and electricity_connection_type.3. Specify the alias name without any mistake.4. Compare your executed result with the sample output given below.
Solution
Here is the SQL query you need to write to get the desired result:
SELECT COUNT(*) AS payment_count
FROM bill
JOIN meter ON bill.meter_id = meter.id
JOIN building ON meter.building_id = building.id
JOIN building_type ON building.building_type_id = building_type.id
JOIN electricity_connection_type ON building.electricity_connection_type_id = electricity_connection_type.id
WHERE bill.is_payed = 1 AND electricity_connection_type.connection_type = 'Commercial';
This query works as follows:
- It starts by selecting all records from the
billtable. - It then joins the
metertable on the condition that themeter_idin thebilltable matches theidin themetertable. - It does the same for the
building,building_type, andelectricity_connection_typetables. - Finally, it filters the results to only include records where
is_payedis 1 (meaning the bill has been paid) and theconnection_typeis 'Commercial'. - The
COUNT(*)function is used to count the number of records that meet these conditions, and this count is given the aliaspayment_count.
Similar Questions
Write a query to display the number of the buildings who has given the fine amount.Give an alias name as 'building_count'.Note:1. Display only the number of buildings from the building table who has given the fine_amount. Use subqueries to achieve this.2. Tables involved are building, meter and bill.3. Specify the alias name without any mistake.4. Compare your executed result with the sample output given below.Note:1. Display only the number of the buildings who has given the fine amount from bill table.2. Specify the alias name without any mistake.3. Compare your executed result with the sample output given below.
Write a query to display the number of bills in which the bill payment is completed. Give an alias name as 'count_of_bills_with_payment'.Note:1. Display only the number of bills where payment is completed.(If payment is completed then is_payed will be 1, otherwise 0.)2. Specify the alias name without any mistake.3. Specify condition on is_payed column from bill table.4. Compare your executed result with the sample output given below.Sample output:
Write a query to display the number of bills having fine_amount. Give an alias name as 'count_of_bills_with_fine'.Note:1. Display only the number of bills having the fine_amount from bill table. (If fine_amount is greater than 0 then it indicates that bill got fine_amount.)2. Specify the alias name without any mistake.3. Specify condition on fine_amount column of bill table.3. Compare your executed result with the sample output given below.Sample output:
Write a query to display the sum of payable_amount and fine_amount of all the bills from bill table. Give an alias name to the sum of payable amount and fine amount as 'sum_payable_and_fine_amount'.Note:1. Display only the sum of payable_amount and fine_amount from bill table.2. Specify the alias name without any mistake.3. Compare your executed result with the sample output given below.Sample output:
Write a query to display the average of payable_amount from the bill table for the buildings with electricity connection slab rate less than 24. Give an alias name for payable amount as 'payable_amount'.Note:1. Display only the average of payable_amount from the bill table for the buildings with electricity connection slab rate less than 24.2. Tables involves are bill, meter, building, building_type, electricity_connection_type and slab.3. Specify the alias name without any mistake.4. 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.