Skip to content

[Master Ticket] Incorrect Query Generation #27

Description

@Gautam-Rajeev

For the question : "get me the schools with the highest average marks in maths from each district", the resultant SQL query does not create the required logic.

Current answer :

 SELECT s.name as school_name, s.city as district, AVG(m.marks_obtained) as avg_math_marks FROM school s JOIN class c ON s.id = c.school_id JOIN student st ON c.id = st.class_id JOIN mark m ON st.id = m.student_id JOIN subject sub ON m.subject_id = sub.id WHERE sub.name = "Maths" GROUP BY s.city HAVING AVG(m.marks_obtained) = (SELECT MAX(avg_math_marks) FROM (SELECT s.city as district, AVG(m.marks_obtained) as avg_math_marks FROM school s JOIN class c ON s.id = c.school_id JOIN student st ON c.id = st.class_id JOIN mark m ON st.id = m.student_id JOIN subject sub ON m.subject_id = sub.id WHERE sub.name = "Maths" GROUP BY s.city) as temp_table);

This holds true for the questions that require to rank within partitions/groups

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions