Knowee
Questions
Features
Study Tools

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)

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

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:

  1. JOIN the courses, grades, and students tables on their respective id fields. This combines the tables into one table where each row contains information from all three tables for a specific student-course-grade combination.

  2. Filter out rows where the grade is less than 50 using the WHERE clause. This ensures that only passing students are considered.

  3. GROUP BY the id and name fields of the courses table. This groups the combined table by course, so that each group contains all the students enrolled in a specific course.

  4. ORDER BY the 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.

  5. Finally, LIMIT 1 is used to only select the first row, which is the most popular course.

This problem has been solved

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;

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.