Knowee
Questions
Features
Study Tools

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).

...expand
🧐 Not the exact question you are looking for?Go ask a question

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:

  1. FROM students s: This is the main table we are selecting from, which contains the student information. We alias it as s for simplicity.

  2. JOIN grades g ON s.id = g.student_id: We join the grades table on the condition that the id in the students table matches the student_id in the grades table. This allows us to connect each student with their respective grades. We alias the grades table as g.

  3. JOIN courses c ON c.id = g.course_id: We join the courses table on the condition that the id in the courses table matches the course_id in the grades table. This allows us to connect each grade with the respective course. We alias the courses table as c.

  4. SELECT s.name AS name, c.name AS course, g.grade AS grade: We select the name column from the students table, the name column from the courses table, and the grade column from the grades table. We alias these as name, course, and grade respectively.

  5. 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.

This problem has been solved

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;

1/3

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.