Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), for each student, get all the courses that he/she is enrolled in along with the grade he/she scored for each course. Order the result by the student name in ascending order and if there is a tie, break it with the course name in ascending order and if there is a tie break it with the grade in ascending order. Your query should output a table with the following columns (name, course, grade).
Question
Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), for each student, get all the courses that he/she is enrolled in along with the grade he/she scored for each course. Order the result by the student name in ascending order and if there is a tie, break it with the course name in ascending order and if there is a tie break it with the grade in ascending order. Your query should output a table with the following columns (name, course, grade).
Solution
Sure, here is the SQL query that you can use to get the desired output:
SELECT s.name AS name, c.name AS course, g.grade AS grade
FROM students s
JOIN grades g ON s.id = g.student_id
JOIN courses c ON c.id = g.course_id
ORDER BY s.name ASC, c.name ASC, g.grade ASC;
Here is the step by step explanation:
-
FROM students s: This is the main table we are selecting from, which contains the student information. We alias it assfor simplicity. -
JOIN grades g ON s.id = g.student_id: We join thegradestable on the condition that theidin thestudentstable matches thestudent_idin thegradestable. This allows us to connect each student with their respective grades. We alias thegradestable asg. -
JOIN courses c ON c.id = g.course_id: We join thecoursestable on the condition that theidin thecoursestable matches thecourse_idin thegradestable. This allows us to connect each grade with the respective course. We alias thecoursestable asc. -
SELECT s.name AS name, c.name AS course, g.grade AS grade: We select thenamecolumn from thestudentstable, thenamecolumn from thecoursestable, and thegradecolumn from thegradestable. We alias these asname,course, andgraderespectively. -
ORDER BY s.name ASC, c.name ASC, g.grade ASC: We order the results first by the student's name in ascending order. If there is a tie, we then order by the course name in ascending order. If there is still a tie, we finally order by the grade in ascending order.
Similar Questions
Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), find the name of the most popular course (the one with the most enrolled students and students should be passing, i.e. achieving a grade of at least 50) and if there is a tie, get the course name that's lexicographically smaller. Your query should output a table with the following columns (name)
Given 3 tables, students(id, name) , courses(id, name) , grades(id, course_id, student_id, grade), find the top 100 students based on their average grades sorted descendingly by the average grade and in case multiple students have the same average grade, sort them lexicographically in ascending order by their names. Your query should output a table with the following columns (name, average_grade).
You have created a query to list information from the students table:SELECT student_id, grade_point_average FROM students ORDER BY grade_point_averageA partial result is shown below:Student_idgrade_point_average12344.015473.918763.929343.897253.883203.420433.339483.0You want the results to look like this:Student_idgrade_point_average12344.015473.918763.929343.897253.8You begin revising your query:SELECT student_id, grade_point_average FROM students ORDER BY grade_point_average ______ 5What should you type in the blank space? (Please type your answer all in upper case. Example: SELECT)
Consider these following tables and only solve the query. Tables: 1) student: s_no (primary key), student_name, course_no (foreign key), age 2) courser-course-no (primary key), course-name. Query: Display all the student name, course name and also age of the students, whose age is between 23 to 27 and course having 'BCA'. Find out the students whose age is maximum.
Using the table Students, choose the correct SQL statement that will return the resultant table given in Figure 4.SELECT Age, Country FROM Students ORDER BY Score ASC;SELECT DISTINCT Age, Country FROM Students ORDER BY Age ASC;SELECT DISTINCT Age, Country FROM Students ORDER BY Score DESC;SELECT DISTINCT Age, Country FROM Students ORDER BY Age DESC;
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.