skip to Main Content

Employee Table

New to MYSQL. I’m trying to create a query that essentially selects employees who have mentored more than 2 people. The mentored_by column uses the SSN of employee’s to tell who has mentored who, So you can see that that a couple of the SSNs in the SSN column match some of the SSNs in the mentored_by column. (Ignore the one value is that is null because I simply made it that he didn’t have a mentor).

How would I go about creating a query where it selects employees who have mentored more than 2 people (aka if theres the same SSN in multiple rows in mentored_by column)

2

Answers


  1. Something like this, I presume:

    select ssn
    from emp
    group by ssn
    having count(distinct mentored_by) > 2;
    
    Login or Signup to reply.
  2. Aggregate your data per mentor and count. With the found mentors select their rows from the employee table.

    select *
    from employees
    where ssn in
    (
      select mentored_by
      from employees
      group by mentored_by
      having count(*) > 2
    );
    

    For your sample data, you will get the first row (Henry), because he mentored three persons (Jennie, Harold, Jameson).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search