Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8899203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:48:11+00:00 2026-06-15T00:48:11+00:00

So there are three tables that would be applicable in this statement. The division

  • 0

So there are three tables that would be applicable in this statement. The division table, which houses the division name and division id, the workon table which houses the projects and employee ids that correlate to the project, and the employee table that houses the employee id, department id, and name. I’m trying to find the department that has the most employees who work on projects.

This is my code:

select distinct 
    (dname) as "Division Name"
from 
    employee e, division d
where 
    d.did = e.did and 
    d.did in (
        select did from employee where empid in (
            select empid from workon having count(pid) >= all(pid)
        )
    )

I’m supposed to get the answer “human resources” but I cannot seem to get that answer no matter what code I use.

Workon table

PID EMPID   HOURS
3   1   30
2   3   40
5   4   30
6   6   60
4   3   70
2   4   45
5   3   90
3   3   100
6   8   30
4   4   30
5   8   30
6   7   30
6   9   40
5   9   50
4   6   45
2   7   30
2   8   30
2   9   30
1   9   30
1   8   30
1   7   30
1   5   30
1   6   30
2   6   30

Employee Table

EMPID   NAME    SALARY  DID
1   kevin   32000   2
2   joan    42000   1
3   brian   37000   3
4   larry   82000   5
5   harry   92000   4
6   peter   45000   2
7   peter   68000   3
8   smith   39000   4
9   chen    71000   1
10  kim 46000   5
11  smith   46000   1

Division
DID DNAME   MANAGERID
1   engineering 2
2   marketing   1
3   human resource  3
4   Research and development    5
5   accounting  4
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T00:48:13+00:00Added an answer on June 15, 2026 at 12:48 am

    Check this reference out please.

    SQLFIDDLE

    select d.id, d.name, p.maxcounts
    from dept d, 
    (select we.dep, max(we.counts) as maxcounts 
     from (select w.eid, count(w.pid) as counts, 
     e.dep as dep from employee e, workon w
    where e.id = w.eid
    group by e.dep) as we) as p
    where d.id = p.dep
    ;
    

    RESULTS:

    ID      NAME                MAXCOUNTS
    111     human resoruces     5
    

    FOLLOWING is the edit based on your own data:

    Reference : SQLFIDDLE_Based_ON_OP_Data

    There are three ways you can achieve this. Either use the nested selects, save Max(count) into a variable or order data by desc and limit it to 1.

    Method 1:

    — using nested select

    —sub query 1 explaining to OP how final answer is derived

    select e.dep, count(w.eid) as num_emp
    from employee e, workon w
    where e.id = w.eid
    group by e.dep
    order by e.dep
    ;
    -- **results of sub query 1:**
    
    DEP     NUM_EMP
    1           4
    2           5
    3           7
    4           5
    5           3
    

    — Final nested select query

    select ee.dep, dd.name, count(ww.eid)
    from employee ee, dept dd, workon ww
    where ee.id = ww.eid
    and ee.dep = dd.id
    group by ee.dep, dd.name
    having count(ww.eid) = 
    (select distinct max(t.num_emp)
    from (select e.dep, count(w.eid) as num_emp
    from employee e, workon w
    where e.id = w.eid
    group by e.dep
    order by e.dep)as t)
    ;
    

    — results using nested selects

    DEP     NAME            COUNT(WW.EID)
    3       human resource  7
    

    — query using a variable

    select max(x.num_emp) into @myvar from 
    (select e.dep, count(w.eid) as num_emp
    from employee e, workon w
    where e.id = w.eid
    group by e.dep) as x
    ;
    
    
    select x.dep, x.name, x.num_emp as num_emp from 
    (select e.dep, d.name, count(w.pid) as num_emp
    from employee e, workon w, dept d
    where e.id = w.eid
    and e.dep = d.id
    group by e.dep) as x
    where x.num_emp = @myvar
    ;
    

    — results using variable

    DEP     NAME              NUM_EMP
    3       human resource    7
    

    — query using limit 1 with ordered desc table

    select e.dep, d.name, count(w.eid) as num_emp
    from employee e, workon w, dept d
    where e.id = w.eid
    and e.dep = d.id
    group by e.dep
    order by num_emp desc 
    limit 1
    

    — results using order by desc and limit 1:

    DEP     NAME              NUM_EMP
    3       human resource    7
    

    Now when using Method 3, it may or may not matter to you that sometimes there will be two departments with same highest number of employees working in projects. So in that case you may use either nested or variable methods.

    *PS I do not have the privilledge to be full time on StackOverFlow, hence sorry for getting back to you late 🙂 *

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a column that exists in 2 tables. In table 1, this column
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
I have three mysql tables that I would like to extract some information from,
I'm working with three MySQL tables that are related in a PHP application. There
I have three tables (people, clients_people, clients) that would be joined using SELECT *
I have three tables that I would like to query: Streams, Entries, and FieldInstances.
Say I have these three tables: Table: Baskets id | name 1 Sale 2
I'm from SQL world, so please, don't ask me that there are no tables,
alt text http://img502.imageshack.us/img502/7245/75088152.jpg There are two tables that I join them together, one of
Background In a PostgreSQL 9.0 database, there are various tables that have many-to-many relationships.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.