-1
SELECT name 
FROM instructor natural join teaches
WHERE course_id = (SELECT course_id 
                  FROM (SELECT course_id, MAX(mycount) 
                        FROM (SELECT course_id, count(ID) as mycount 
                              FROM takes GROUP BY course_id)));

This query doesn't work in oracle. It shows single group function error.

ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
007
  • 21
  • 3
  • Please better tell what would you like to obtain and post what are your tables. This query is very por quality. – Kacper Nov 03 '16 at 18:59
  • Possible duplicate of [SQL not a single-group group function](http://stackoverflow.com/questions/1795198/sql-not-a-single-group-group-function) – Alfabravo Nov 03 '16 at 19:00

3 Answers3

0

Could be you obtain more then a result try in and missing a group by

SELECT name 
FROM instructor natural join teaches
WHERE course_id IN  (SELECT course_id 
              FROM (SELECT course_id, MAX(mycount) 
                    FROM (SELECT course_id, count(ID) as mycount 
                          FROM takes GROUP BY course_id) 
                    GROUP BY course_id));
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
0

You're missing a group by. :)

SELECT name FROM instructor 
    natural join teaches 
    WHERE course_id = 
    (SELECT course_id FROM 
        (SELECT course_id, MAX(mycount) FROM 
            (SELECT course_id, count(ID) as mycount FROM takes GROUP BY course_id)
        group by course_ID) -- don't forget this one!
    );

Also, that outer subquery won't really do anything. You've already got one row per course_ID, so the max(mycount) will be exactly the same as mycount.

0

lThe error occurs in your second level of subqueries:

(SELECT course_id, MAX(mycount) 
 FROM (SELECT course_id, count(ID) as mycount 
       FROM takes
       GROUP BY course_id)
)

You have an aggregation function but no GROUP BY.

I would also advise you to avoid NATURAL JOIN. This is just a bug waiting to happen. Hiding the keys used for the join is a bad thing, that makes queries harder to understand and more prone to error.

And then, your query is rather hard to follow. I would suggest that you ask another question, providing sample data and desired results. There is probably a simpler way to express this.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786