0

This is my query in SQL.

SELECT MAX(course_id), sec_id, COUNT(ID) 
FROM (SELECT MAX(course_id), sec_id, COUNT(ID) 
      FROM section 
      NATURAL JOIN takes 
      GROUP BY course_id);

When executed, I'm getting this error:

<"Every derived table must have its own alias">

xQbert
  • 34,733
  • 2
  • 41
  • 62
  • your subquery needs to alias the aggregated columns in order for you to use those names in the outer query. The `select max(course_ID), sec_ID, count(ID)` will error because course_ID and ID don't exist in your subquery. To prove it run the subquery on it's own and look at the column names it gives for cols 1 and 3. – xQbert Oct 24 '17 at 18:41

1 Answers1

1

Add an alias to your derived table.

SELECT 
    MAX(course_id), 
    sec_id, 
    COUNT(ID) 
FROM 
    (
        SELECT 
            MAX(course_id), 
            sec_id, 
            COUNT(ID) 
        FROM 
            section NATURAL 
            JOIN takes 
        GROUP BY 
            course_id
    ) `A`;
bassxzero
  • 4,838
  • 22
  • 34