The questions can come in any order, so make sure you are selecting right option for all questions.
1. Write a query to display all the 'bills' with 'meter' details. Display the records in ascending order based on meter numberselect * from bill b, meter m where b.meter_id = m.id order by m.meter_number;
select m.meter_number, b.owner_name,b.address,b.contact_number from building b,meter m where b.id=m.building_id order by b.owner_name;
select m.meter_number,b.owner_name,b.address,b.contact_number,t.name,c.connection_name from meter m,building b, building_type t, electricity_connection_type c where m.building_id=b.id and b.building_type_id=t.id and t.connection_type_id = c.id order by t.name;
select c.connection_name,s.from_unit,s.to_unit,s.rate from electricity_connection_type c,slab s where s.connection_type_id = c.id order by c.connection_name;
select b.owner_name,b.address,count(m.id) as connection_count from building b inner join building_type t on b.building_type_id = t.id inner join meter m on b.id = m.building_id group by b.owner_name,b.address order by b.owner_name;
select b.owner_name,b.address,m.meter_number,bl.payable_amount from building b join meter m on b.id = m.building_id join bill bl on m.id = bl.meter_id where bl.year = 2017 and bl.month = 12 and bl.fine_amount is null order by b.owner_name;
select b.owner_name, b.address, m.meter_number, bl.payable_amount from building b join meter m on b.id = m.building_id join bill bl on m.id = bl.meter_id where bl.fine_amount = ( select max(fine_amount) from bill) order by bl.fine_amount desc;
select b.owner_name,b.address,m.meter_number, total_units from building b join meter m on m.building_id = b.id join bill bl on m.id = bl.meter_id where bl.month = 12 and bl.year = 2017 order by bl.total_units desc;
select b.owner_name, sum(bl.payable_amount) as totalbillamount from building b join meter m on b.id = m.building_id join bill bl on m.id = bl.meter_id where year = 2017 group by b.owner_name having sum(bl.payable_amount) = ( select max(sum(bi.payable_amount)) from bill bi join meter m on bi.meter_id = m.id join building b on m.building_id = b.id where year = 2017 group by b.owner_name) order by b.owner_name
Payment_Status will be payment_date is the payment is done, else if the payment is not done then display as 'Not Paid Yet'. Give an alias name as 'Payment_Status'. Display the record in ascending order based on owner_name.
select bu.owner_name,bt.name, m.meter_number, case when b.is_payed = 0 then 'Not Paid Yet' else to_char(b.payment_date, 'DD-MON-YY') end as payment_status from bill b join meter m on b.meter_id = m.id join building bu on m.building_id = bu.id join building_type bt on bu.building_type_id = bt.id where b.year= 2017 and b.month=12 order by bu.owner_name asc;
select t.connection_name, count(t.id) as connection_count from electricity_connection_type t join building_type bt on bt.connection_type_id = t.id join building b on bt.id = b.building_type_id group by t.connection_name order by connection_count desc;
select bt.name as building_name , count(b.building_type_id) as number_of_buildings from building_type bt join building b on bt.id = b.building_type_id group by bt.name having count(b.building_type_id) = ( select max(building_count) from ( select count(building_type_id) as building_count from building group by building_type_id ) max_buildings ) order by building_name;
select m.meter_number, b.owner_name,b.address from bill bi join meter m on bi.meter_id = m.id join building b on m.building_id = b.id group by m.meter_number, b.owner_name, b.address having count(bi.id) = ( select max(num_fines) from ( select count(id) as num_fines from bill group by meter_id ) max_fines ) order by b.owner_name asc;
select m.meter_number, bu.owner_name, bu.address from bill b join meter m on b.meter_id = m.id join building bu on m.building_id = bu.id where b.fine_amount = ( select min(fine_amount) from bill where fine_amount not in ( select min(fine_amount) from bill ) )
select m.meter_number, er.total_units, b.payable_amount, sum(h6+h7+h8+h9+h10+h11) as morning, sum(h12+h13+h14+h15) as afternoon, sum(h16+h17+h18+h19) as evening, sum(h20+h21+h22+h23+h24+h1+h2+h3+h4+h5) as night from electricity_reading er join meter m on er.meter_id = m.id join bill b on m.id = b.meter_id where b.month = 12 and b.year = 2017 group by m.meter_number,er.total_units,b.payable_amount order by er. total_units desc;