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'.
Question
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'.
Solution 1
Sure, here is the SQL query you asked for:
SELECT SUBSTRING(owner_name, 1, 3) AS name_code, contact_number
FROM buildings
ORDER BY owner_name ASC;
Here's a step-by-step breakdown of the query:
-
SELECT: This keyword is used to select data from a database. -
SUBSTRING(owner_name, 1, 3) AS name_code: TheSUBSTRINGfunction extracts a part of a string. Here, it's used to get the first 3 characters of theowner_namecolumn. TheASkeyword is used to rename the column in the output asname_code. -
contact_number: This is the second column that we want to display in the output. -
FROM buildings: This specifies the table from which to retrieve the data. -
ORDER BY owner_name ASC: This orders the output in ascending order (ASC) based on theowner_namecolumn.
Solution 2
Sure, here is the SQL query you asked for:
SELECT SUBSTRING(owner_name, 1, 3) AS name_code, contact_number
FROM buildings
ORDER BY owner_name ASC;
Here's a step-by-step breakdown of the query:
-
SELECT: This keyword is used to select data from a database. -
SUBSTRING(owner_name, 1, 3) AS name_code: TheSUBSTRINGfunction extracts a part of a string. Here, it's used to get the first 3 characters of theowner_namecolumn. TheASkeyword is used to rename the column in the output asname_code. -
contact_number: This is the second column that we want to display in the output. -
FROM buildings: This specifies the table from which to retrieve the data, which isbuildingsin this case. -
ORDER BY owner_name ASC: This is used to sort the results in ascending order (ASC) based on theowner_namecolumn.
Similar Questions
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.
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().
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 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 the owner_name and mail id in which the string should end before the '@' symbol. Display the details of the buildings which are having the email address and give an alias name as 'email_name' and display it in ascending order based on owner name
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.