Write a query to display the building owner name ,address and number of connections present in each building. Display the records in ascending order based on owner name.Give an alias name as connection_count.Note:1. Display only the building owner_name ,address and number of connections.2. Use Joins3. Tables involved building, building_type, meter4. Specify the alias name without any mistake.5. Order by is mandatory. 6. Compare your executed result with the sample output given below.
Question
Write a query to display the building owner name ,address and number of connections present in each building. Display the records in ascending order based on owner name.Give an alias name as connection_count.Note:1. Display only the building owner_name ,address and number of connections.2. Use Joins3. Tables involved building, building_type, meter4. Specify the alias name without any mistake.5. Order by is mandatory. 6. Compare your executed result with the sample output given below.
Solution
Based on the instructions given, here is a SQL query that should give you the desired output:
SELECT
b.owner_name,
b.address,
COUNT(m.connection_id) AS connection_count
FROM
building b
JOIN
meter m ON b.building_id = m.building_id
GROUP BY
b.owner_name,
b.address
ORDER BY
b.owner_name ASC;
Here's a step-by-step breakdown of the query:
-
SELECT b.owner_name, b.address, COUNT(m.connection_id) AS connection_count: This line is selecting the columns we want to display in the final output. We're selecting the owner's name and address from thebuildingtable, and counting the number of connections from themetertable. -
FROM building b JOIN meter m ON b.building_id = m.building_id: This line is joining thebuildingandmetertables on thebuilding_idcolumn, which is presumably a common column in both tables. -
GROUP BY b.owner_name, b.address: This line is grouping the results by the owner's name and address. This is necessary because we're using an aggregate function (COUNT) in theSELECTstatement. -
ORDER BY b.owner_name ASC: This line is ordering the results in ascending order by the owner's name.
Please note that the actual column names and table names in your database might be different, so you'll need to replace them accordingly. Also, the building_type table was not used in this query as it was not needed based on the provided instructions.
Similar Questions
Write a query to display the meter number,owner name,address,contact number,building type name and connection name of all buildings.Display the records in ascending order based on building type name.Note:1. Display only the below 6 columns, i. meter number ii. owner name iii. address iv. contact number v. building type name vi. Connection name 2. Use Joins3. Tables involved building, meter, building_type, electricity_connection_type4. Order by is mandatory.5. Compare your executed result with the sample output given below.
Write a query to display the owner_name and contact_number of all building, Display the records in ascending order based on owner_name.Note:1. Display only the column named ‘owner_name’ and ‘contact_number’ present in the building table.2. Order by is mandatory
QueryQ2Write a query to display the meter number,owner name,address and contact number of all the buldings. Display the record in ascending order based on owner name.Note:1. Display only the below 4 columns, i. meter number ii. owner name iii. address iv. contact number 2. Use Joins3. Tables involved building and meter4. Order by is mandatory.5. Compare your executed result with the sample output given below.Sample output: [Only few records are shown here]
Write a query to display the owner name and the length of the owner's name of the buildings. Display the records in ascending order based on owner name. Give an alias as name_length to the length of the owner names.Use the inbuilt function length().Note:1. Display only the owner name and length of the owner_name from building table.2. Specify the alias name without any mistake.3. Order by is mandatory4. Compare your executed result with the sample output given below.Sample output:[only few records shown here]
Write a query to display all the building details in which building_type named ‘Library’. Display the records in ascending order based on their owner_name.Note:1. Display all the column present in the building table where building_type name is ‘Library’. Use subqueries to achieve this.2. Tables involved are building_type and building3. Order by is mandatory.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.