Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
which project or projects have the least number of employees?

Employee table has
-Fname
-minit
-lname
-ssn
-bdate
-address
-sex
-salary
-Super_ssn
-Dno

Department table has
-Dname
-Dnumber
-Mgr_ssn
-Mgr_start_date

Works_on table has
-essn
-pno
-hours


What I have tried:

I have tried the following but getting an error:
SQL
select pno, count(*) 
from works_on
group by pno
having count(*) = (
select min(CNT)
from (
select pno, count(*) as CNT
from works_on
group by pno ) );
The error I get is:
LIMIT 0, 1000	Error Code: 1248. Every derived table must have its own alias	0.000 sec

What am I missing?
Posted
Updated 15-Oct-22 19:13pm
v2
Comments
Greg Utas 16-Oct-22 7:58am    
The project that was cancelled. :)

You need to provide the table alias in your inner queries SELECT. Add an alias after the closing bracket of the FROM clause subquery, i.e. each subquery in brackets must be given an alias (AS something)

Derived table is:
SQL
FROM ( subquery )
Above needs an alias, like:
SQL
FROM ( subquery ) AS anyName

Try:
SQL
select 
    pno, count(*) 
from 
    works_on
group by 
    pno
having 
    count(*) = (
        select 
            min(CNT)
        from (
            select 
                pno, count(*) as CNT
            from 
                works_on
            group by 
                pno 
            ) AS innerTable 
    );
 
Share this answer
 
Comments
SanjayMondal2001 16-Oct-22 1:35am    
Thank You so much, sir, I'm in my learning phase, thank you for the explanation now I understood where I was wrong, YOU ARE GREAT!
If you don't understand an error message, google it:
Error Code: 1248. Every derived table must have its own alias - Google Search[^]
The top link explains the problem: https://stackoverflow.com/questions/1888779/what-is-the-error-every-derived-table-must-have-its-own-alias-in-mysql[^] (As do all the others ... )

In future, please try to do at least basic research yourself, and not waste your time or ours.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900