Knowee
Questions
Features
Study Tools

Write a query to display the building_type name and the number of buildings under the type which has maximum number of buildings. Give an alias name for number of buildings as number_of_buildings and for building name as building_name. If there are multiple records sort in ascending order by building_name.Note:1. Display only the building_type name and no. of buildings in building_type table with the condition having maximum no. of buildings.2. Use Joins3. Tables involved are building and building_type4. Order by is mandatory. 5. Specify the alias name without any mistake.6. Compare your executed result with the sample output given below.

Question

Write a query to display the building_type name and the number of buildings under the type which has maximum number of buildings. Give an alias name for number of buildings as number_of_buildings and for building name as building_name. If there are multiple records sort in ascending order by building_name.Note:1. Display only the building_type name and no. of buildings in building_type table with the condition having maximum no. of buildings.2. Use Joins3. Tables involved are building and building_type4. Order by is mandatory. 5. Specify the alias name without any mistake.6. Compare your executed result with the sample output given below.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

To answer this question, we will use SQL (Structured Query Language), which is a standard language for managing data held in a relational database management system.

Here is the SQL query:

SELECT bt.building_type AS building_name, COUNT(b.building_id) AS number_of_buildings
FROM building_type bt
JOIN building b ON bt.building_type_id = b.building_type_id
GROUP BY bt.building_type
HAVING COUNT(b.building_id) = (
  SELECT MAX(building_count)
  FROM (
    SELECT COUNT(building_id) AS building_count
    FROM building
    GROUP BY building_type_id
  ) AS subquery
)
ORDER BY building_name ASC;

Let's break down this query:

  1. We are joining the building_type table (aliased as bt) and the building table (aliased as b) on the building_type_id field which is common in both tables.

  2. We are grouping the results by the building_type field from the building_type table.

  3. We are counting the number of building_id from the building table for each building_type. This gives us the number of buildings for each type.

  4. We are only interested in the building types that have the maximum number of buildings. To find this, we use a subquery that counts the number of buildings for each building type, and then selects the maximum count.

  5. Finally, we order the results by the building_name in ascending order.

This problem has been solved

Similar Questions

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.

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

Write a query to display the name of all building types that ends with letter 'l'. Display the records in ascending order based on name.Note:1. Display only the name of building types that ends with L from building_type table.2. Order by is mandatory3. Compare your executed result with the sample output given below.

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.

Write a query to display the entire details of the building whose building_type_id is 2, sorted by owner_name in ascending order.Note:1. Display all the columns present in the building table.2. Specify the condition and retrieve the correct rows in the result(condition need to be specified on building_type_id column).3. You can compare your executed result with the sample output given below.4. Order by is mandatorySample output:

1/3

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.