Search code examples
mysqlmysql-error-1064

Every derived table must have its own alias? How is this wrong


I've given my derived table an alias but still when i run the query it gives me the error "Every derived table must have its own alias".

select a,b,c,sum(d) as 'sum'
from(select a,b,c,sum(d)as 'd' from e join f using(z)) as 'alias'
group by a;


EDIT: better sample
----This gives derived table error-----
select name,sum(pop) as 'total' from(select name as 'name',sum(population) 
as pop from table1 join table2 using(countrycode));

----This gives me the SQL syntax error--------
select name,sum(pop) as 'total' from(select name as 'name',sum(population) 
as pop from table1 join table2 using(countrycode)) as 'alias';

Thanks in advance.


Solution

  • Try this :

    select name,
           sum(pop) as `total` 
    from( 
            select name as `name`, sum(population) as pop 
            from table1 
            join table2 using(countrycode)
    ) `m` <---- alias needed here too