Write a query to display the meter_number of the building_type named 'College'. Display the records in ascending order based on their meter_number.Note:1. Display only the meter_number column present in the meter table where building_type name is ‘College’. Use subqueries to achieve this.2. Tables involved are building_type, building and meter.3. Order by is mandatory4. Compare your executed result with the sample output given below.
Question
Write a query to display the meter_number of the building_type named 'College'. Display the records in ascending order based on their meter_number.Note:1. Display only the meter_number column present in the meter table where building_type name is ‘College’. Use subqueries to achieve this.2. Tables involved are building_type, building and meter.3. Order by is mandatory4. Compare your executed result with the sample output given below.
Solution 1
Here is the SQL query you need:
SELECT meter_number
FROM meter
WHERE building_id IN (
SELECT building_id
FROM building
WHERE building_type_id IN (
SELECT building_type_id
FROM building_type
WHERE building_type_name = 'College'
)
)
ORDER BY meter_number ASC;
This query works as follows:
- The innermost subquery selects the
building_type_idfrom thebuilding_typetable where thebuilding_type_nameis 'College'. - The next subquery selects the
building_idfrom thebuildingtable where thebuilding_type_idis in the list retrieved from the innermost subquery. - The outermost query selects the
meter_numberfrom themetertable where thebuilding_idis in the list retrieved from the second subquery. - Finally, the results are ordered in ascending order by
meter_number.
Solution 2
Here is the SQL query you asked for:
SELECT meter_number
FROM meter
WHERE building_id IN (
SELECT building_id
FROM building
WHERE building_type_id IN (
SELECT building_type_id
FROM building_type
WHERE building_type_name = 'College'
)
)
ORDER BY meter_number ASC;
This query works as follows:
- The innermost subquery selects the
building_type_idfrom thebuilding_typetable where thebuilding_type_nameis 'College'. - The next subquery selects the
building_idfrom thebuildingtable where thebuilding_type_idis in the list retrieved from the first subquery. - The outermost query selects the
meter_numberfrom themetertable where thebuilding_idis in the list retrieved from the second subquery. - Finally, the
ORDER BYclause sorts the results in ascending order bymeter_number.
Solution 3
Here is the SQL query you asked for:
SELECT meter_number
FROM meter
WHERE building_id IN (
SELECT building_id
FROM building
WHERE building_type_id IN (
SELECT building_type_id
FROM building_type
WHERE building_type_name = 'College'
)
)
ORDER BY meter_number ASC;
This query works as follows:
- The innermost subquery selects the
building_type_idfrom thebuilding_typetable where thebuilding_type_nameis 'College'. - The next subquery selects the
building_idfrom thebuildingtable where thebuilding_type_idis in the list retrieved from the innermost subquery. - The outermost query selects the
meter_numberfrom themetertable where thebuilding_idis in the list retrieved from the second subquery. - Finally, the results are ordered in ascending order by
meter_number.
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 all the contact number of the building_type named 'Police Station'. Display the records in ascending order based on the contact number.Note:1. Display only the contact_number column present in the building table where building_type name is ‘Police Station’. Use subqueries to achieve this.2. Tables involved are building_type and building.3. Order by is mandatory.4. Compare your executed result with the sample output given below.
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.
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.
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.Sample output:
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.