Write a query to display the staff_name, subject_name and subject_code details from staff and subject table. Display all the staff_name even if they have not handling any subjects. Display the records sorted in ascending order based on staff_name.Note:1. Display only the staff_name, subject_name and subject_code details from staff and subject table.2. Display all the staff_name even if they have not handling any subjects.3. Use joins. Tables involved are subject and staff4. Order by is mandatory.Sample Output:
Question
Write a query to display the staff_name, subject_name and subject_code details from staff and subject table. Display all the staff_name even if they have not handling any subjects. Display the records sorted in ascending order based on staff_name.Note:1. Display only the staff_name, subject_name and subject_code details from staff and subject table.2. Display all the staff_name even if they have not handling any subjects.3. Use joins. Tables involved are subject and staff4. Order by is mandatory.Sample Output:
Solution
The query you're looking for would look something like this:
SELECT staff.staff_name, subject.subject_name, subject.subject_code
FROM staff
LEFT JOIN subject ON staff.staff_id = subject.staff_id
ORDER BY staff.staff_name ASC;
Here's a step-by-step breakdown of this query:
-
SELECT staff.staff_name, subject.subject_name, subject.subject_code: This part of the query specifies the columns that you want to display in the output. In this case, you want thestaff_namefrom thestafftable, and thesubject_nameandsubject_codefrom thesubjecttable. -
FROM staff: This part of the query specifies the main table you're working with, which is thestafftable. -
LEFT JOIN subject ON staff.staff_id = subject.staff_id: This part of the query joins thesubjecttable to thestafftable. TheLEFT JOINkeyword returns all records from the left table (staff), and the matched records from the right table (subject). If there is no match, the result isNULLon the right side. -
ORDER BY staff.staff_name ASC: This part of the query sorts the output in ascending order by thestaff_namecolumn. TheASCkeyword specifies that the sorting should be in ascending order. If you wanted to sort in descending order, you would use theDESCkeyword instead.
Similar Questions
Write a query to display the subject_name and subject_code of the subjects handled by the staff named 'Lakshmi'. Display the records sorted in ascending order based on subject_name.
Write a query to retrieve the names of all employees and sort them in ascending order.Table: EmployeeInput records:Input format :The input table is already created, and records are already prepopulated, as mentioned in the problem statement.Output format :The output displays the names of all employees in ascending order as shown below.NameAngelMarkMathewSteveRefer to the sample output for the column headers.
Choose the query to display names of the department in the college ordered in department_name in ascending order.select department_name from Department order by department_name asc;select department_name from Department order by name asc;select name from Department order by name;None of the listed optionsReview Answer
Write a query to display the ename of employees working in 'HR' department and who draw an annual salary greater than 1000000. [Consider employees who work in multiple departments too]. Display the records sorted in ascending order based on employee ename.Note:1. Display all the ename of employees working in 'HR' department and having annual salary greater than 1000000.2. Use Joins. Tables involved are employee, works and department3. Order by is mandatory.
Write a query to display the coursename of the courses having the coursefees above average fees of all the courses. Display the records sorted in ascending order based on coursename.Note:1. Display only the coursename of the courses having the coursefees above average fees of all the courses.2. Order by is mandatory.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.