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().
Question
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().
Solution 1
Sure, here is the SQL query you asked for:
SELECT owner_name, LENGTH(owner_name) AS name_length
FROM buildings
ORDER BY owner_name ASC;
Here's a step-by-step breakdown of the query:
-
SELECT owner_name, LENGTH(owner_name) AS name_length: This part of the query is selecting two columns. The first is theowner_namecolumn. The second is the length of theowner_name, which is calculated using theLENGTH()function. TheAS name_lengthpart is giving this column an alias, or a different name, which isname_length. -
FROM buildings: This part of the query is specifying the table that we are selecting the data from, which is thebuildingstable. -
ORDER BY owner_name ASC: This part of the query is ordering the results by theowner_namecolumn in ascending order. If you wanted to order the results in descending order, you could useDESCinstead ofASC.
Solution 2
Assuming you are using SQL, the query would look something like this:
SELECT owner_name, LENGTH(owner_name) AS name_length
FROM buildings
ORDER BY owner_name ASC;
Here's the step by step breakdown:
-
SELECT owner_name, LENGTH(owner_name) AS name_length: This line is selecting two columns. The first is theowner_namecolumn. The second is the length of theowner_name, which is calculated using theLENGTH()function. TheAS name_lengthpart is giving this calculated column a name, or alias, ofname_length. -
FROM buildings: This line is specifying the table that we are selecting the data from, which in this case is thebuildingstable. -
ORDER BY owner_name ASC: This line is ordering the results by theowner_namecolumn in ascending order. If you wanted to order the results in descending order, you could useDESCinstead ofASC.
Similar Questions
Write a query to display the owner_name and contact_number of all building, Display the records in ascending order based on owner_name.
Write a query to display the first 3 characters of the owner_name and the contact_number of all buildings in ascending order based on the owner name of the building. Give an alias to the first 3 characters as 'name_code'.
Write a query to display the first 3 characters of the owner_name and the contact_number of all buildings in ascending order based on the owner name of the building. Give an alias to the first 3 characters as 'name_code'.Note:1. Display only the first 3 characters of owner names and contact_number from building table.2. Specify the alias name without any mistake.3. Order by is mandatory.4. Use the inbuilt function lpad().5. Compare your executed result with the sample output given below.Sample output:[only few records shown here]
Write a query to display the entire details of the 'building' whose owner_name starts with the letter 'M', Display the records in ascending order based on their owner_name.
Write a query to display all the details of the buildings whose owner name contains 'di' in ascending order based on the owner name of the building.Note:1. Display all the columns from the building table whose owner_name contains “di” from building table.2. Order by is mandatory3. 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.