1

I'm having a "syntax error" when I try to run this query. My idea is to get the name of the students with the biggest amount of Evaluations in those two categories.

This fails :

SELECT first_name,last_name
    FROM People
    WHERE ID IN  (
      SELECT ID_Student
            FROM Evaluations
            WHERE category IN ( 'Exam',
                                'Behaviour')
            GROUP BY ID_Student ORDER BY count(ID_Student) DESC ;
        );

While this works perfectly :

SELECT first_name,last_name 
    FROM People 
    WHERE ID IN  ( 42866836,53074038 );

I know the subquery works on its own, as I can remove it and run it separately and it correctly returns a list of ID_Student.

Can someone advice?

2
  • Can you try executing your query without ORDER BY in your WHERE?
    – Aura
    May 14, 2018 at 17:14
  • What's your DBMS and the exact error message?
    – dnoeth
    May 14, 2018 at 17:14

3 Answers 3

2

Try

SELECT first_name,last_name
    FROM People
    WHERE ID IN  (
      SELECT ID_Student
            FROM Evaluations
            WHERE category IN ( 'Exam',
                                'Behaviour')
            GROUP BY ID_Student ORDER BY count(ID_Student) DESC 
        );

without the semicolon after DESC for the syntax error. The ORDER BY is somewhat pointless there too. You can remove that. With the GROUP BY you might have wanted to reduce the cardinality of the subquery's result to improve performance. But for that you better rewrite it with an EXISTS and a correlated subquery.

SELECT People.first_name,
       People.last_name
       FROM People
       WHERE EXISTS (SELECT *
                            FROM Evaluations
                            WHERE Evaluations.category IN ('Exam',
                                                           'Behaviour')
                                  AND Evaluations.ID_Student = People.ID);

However something like

SELECT People.first_name,
       People.last_name,
       count(*)
       FROM People
            INNER JOIN Evaluations
                       ON Evaluations.Student_ID = People.ID
       WHERE Evaluations.category IN ('Exam',
                                      'Behaviour')
       GROUP BY People.first_name,
                People.last_name
       ORDER BY count(*) DESC;

seems to better fit the description of what you actually want to have.

2
  • Thanks so much for your answer @sticky bit, your last example worked perfectly. Just one question, why my initial code did not work? ( other than the semicolon typo ). I mean, I am sending three ID from the subquery to the main query. Why is that different than when I manually type three IDs in the "IN (...)" block? May 14, 2018 at 18:30
  • 1
    @MatiasBarrios: Your subquery just passes the Student_IDs, that are in the Evaluations table where the categories match. The count and all that "gets lost". So you effectively selected the correct students with your query but without the count of evaluations. But there is no difference in typing in the IDs manually or selecting them via a subquery (if the sets match of course).
    – sticky bit
    May 14, 2018 at 18:34
2

In standard SQL Order by clause is invalid in subquery unless any LIMIT clause or TOP clause is specified in subquery or you can directly use FETCH FIRST.

So, you should either remove order by clause or you should re-write your query as with correlated subquery

SELECT first_name,last_name
FROM People p
WHERE EXISTS (SELECT 1 
              FROM Evaluations 
              WHERE ID_Student = p.ID AND  
                    category IN ( 'Exam', 'Behaviour')
             );
1
  • In Standard SQL there's neither LIMIT nor TOP, but FETCH FIRST.
    – dnoeth
    May 14, 2018 at 17:43
2

You want to order people by number of evaluations?

Try this :

with eval as (select id_student, COUNT(ID_Student) cnt  
    FROM Evaluations  
    WHERE category IN ( 'Exam',  
                        'Behaviour')  
    GROUP BY ID_Student)  
SELECT first_name,last_name  
    FROM People  
        inner join eval on people.id = eval.id_student  
        order by eval.cnt desc;  

Otherwise, no need of group and order in your subquery.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.