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)
Question
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)
Solution
Here is a SQL query that should give you the desired result:
SELECT c.name
FROM courses c
JOIN grades g ON c.id = g.course_id
JOIN students s ON s.id = g.student_id
WHERE g.grade >= 50
GROUP BY c.id, c.name
ORDER BY COUNT(s.id) DESC, c.name ASC
LIMIT 1;
Here's a step-by-step explanation of the query:
-
JOINthecourses,grades, andstudentstables on their respectiveidfields. This combines the tables into one table where each row contains information from all three tables for a specific student-course-grade combination. -
Filter out rows where the grade is less than 50 using the
WHEREclause. This ensures that only passing students are considered. -
GROUP BYtheidandnamefields of thecoursestable. This groups the combined table by course, so that each group contains all the students enrolled in a specific course. -
ORDER BYthe count of students in each group in descending order, and then by the course name in ascending order. This sorts the groups so that the group with the most students comes first, and in case of a tie, the course with the lexicographically smaller name comes first. -
Finally,
LIMIT 1is used to only select the first row, which is the most popular course.
Similar Questions
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).
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).
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.
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)
For the following tables and a query:Student (studentId, studentName, address) Result (studentId, subjectId, score) Subject (subjectId, subjectName) Which code snippet will display students with the highest 'SQL Basics' test scores?SELECT TOP 1 WITH TIES s.*FROM STUDENT s JOIN RESULT r ON s.studentId = r.studentIdWHERE r.subjectId =(SELECT ss.subjectId FROM dbo.Subject ssWHERE ss.subjectName = 'SQL Basics');SELECT TOP 1 WITH TIES s.*FROM STUDENT s JOIN RESULT r ON s.studentId = r.studentIdWHERE r.subjectName = 'SQL Basics'ORDER BY score DESC;SELECT TOP 1 WITH TIES s.*FROM STUDENT s JOIN RESULT r ON s.studentId = r.studentIdWHERE r.subjectId = 'SQL Basics'ORDER BY score DESC;SELECT TOP 1 WITH TIES s.*FROM STUDENT s JOIN RESULT r ON s.studentId = r.studentIdWHERE r.subjectId =(SELECT ss.subjectId FROM dbo.Subject ssWHERE ss.subjectName = 'SQL Basics')ORDER BY score 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.