Knowee
Questions
Features
Study Tools

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

Question

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

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

Solution 1

Sure, here is the SQL query that you can use to get the desired output:

SELECT s.name, AVG(g.grade) as average_grade
FROM students s
JOIN grades g ON s.id = g.student_id
GROUP BY s.id, s.name
ORDER BY average_grade DESC, s.name ASC
LIMIT 100;

Here is the step by step explanation:

  1. FROM students s - This is the main table we are selecting from, and we are aliasing it as s for simplicity.

  2. JOIN grades g ON s.id = g.student_id - We are joining the grades table (aliased as g) on the students table where the id field of the students table matches the student_id field of the grades table. This allows us to access the grade field for each student.

  3. GROUP BY s.id, s.name - We are grouping the results by the id and name fields of the students table. This allows us to calculate the average grade for each student.

  4. SELECT s.name, AVG(g.grade) as average_grade - We are selecting the name field from the students table and the average of the grade field from the grades table. The AVG function calculates the average of a group of values.

  5. ORDER BY average_grade DESC, s.name ASC - We are ordering the results first by the average_grade field in descending order (so the students with the highest average grade are first), and then by the name field in ascending order (so if two students have the same average grade, the one with the name that comes first alphabetically will be first).

  6. LIMIT 100 - We are limiting the results to the top 100 rows.

This problem has been solved

Solution 2

Sure, here is the SQL query that you can use to get the desired output:

SELECT s.name, AVG(g.grade) as average_grade
FROM students s
JOIN grades g ON s.id = g.student_id
GROUP BY s.id, s.name
ORDER BY average_grade DESC, s.name ASC
LIMIT 100;

Here is the step by step explanation:

  1. SELECT s.name, AVG(g.grade) as average_grade: This line is selecting the name of the student from the students table and the average of the grades from the grades table.

  2. FROM students s JOIN grades g ON s.id = g.student_id: This line is joining the students table and the grades table on the condition that the id in the students table matches the student_id in the grades table.

  3. GROUP BY s.id, s.name: This line is grouping the results by the id and name in the students table. This is necessary because we want to calculate the average grade for each student.

  4. ORDER BY average_grade DESC, s.name ASC: This line is ordering the results first by the average grade in descending order (so the students with the highest average grade come first), and then by the name in ascending order (so if two students have the same average grade, the one with the name that comes first in alphabetical order will come first).

  5. LIMIT 100: This line is limiting the results to the top 100 students.

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

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)

You have a database that tracks students in the students table by student_id. The names of the semesters are in a semesters table where the primary key is semester_id. Each semester, students are assigned a grade_point_average which is stored in the grades table. The grades table lists the student_id and the semester_id in addition to the grade_point_average.You need to create a query that finds all the semesters where the student had a grade point average that was less than their average grade point average. You begin typing your query:What should you type in the blank space? (Please type your answer all in lower case. Example: select)

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:

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.