Query for a self join for employee table - sql

Suppose I have an employee table. I have Name and Manager columns. Say there are 10 employees of which 2 are managers. So Name will have 10 names and Manager name would be in Manager column.
How to use self join? I am just learning self join

To perform a self join, you simply give the same table a different alias.
For example, in your employee table you would have a managerid - which stores the id of the manager.
Then to get the manager's name - you just self join to the employee table on managerid - using a different alias (I have used m in the example below):
For example, your table would look like this:
CREATE TABLE Employees (id INT, Name VARCHAR(20), ManagerId INT);
To get the Employee's Name and his/her Manager's Name, you would do something like this:
SELECT
e.Name AS EmployeeName,
ISNULL(m.Name, 'No Manager') AS ManagerName
FROM employee e
LEFT JOIN employee m on m.id = e.ManagerId
If you want to learn more about self joins - see here

Related

SELECT or JOIN using same column twice

So I have three tables: Employee, Secretary and Manager
Given Schema
The Employee table has the following columns:
Employee_Number
Name
Home Address
Telephone Number
The Secretary table contains:
Secretary_Number
Employee_Number (linked with foreign key to Employee table)
Manager_Number (linked with foreign key to Manager table)
The Manager table contains:
Manager_Number
Employee_Number (linked with foreign key to Employee table)
What's required and what I tried
I am trying to do a JOIN so that I can see following columns:
Secretary's Number
Secretary's Name
Manager's Number
Manager's Name
I have the following join statement, which shows all the columns, and shows the Secretary's name and number, as well as the Manager Number:
SELECT
SECRETARY.SECRETARY_NUMBER,
SECRETARY.EMPLOYEE_NUMBER AS SECRETARY_EMPLOYEE,
EMPLOYEE.NAME AS SECRETARY_NAME,
SECRETARY.MANAGER_NUMBER,
MANAGER.EMPLOYEE_NUMBER AS MANAGER_EMPLOYEE,
EMPLOYEE.NAME AS MANAGER_NAME
FROM SECRETARY, MANAGER, EMPLOYEE
WHERE SECRETARY.MANAGER_NUMBER = MANAGER.MANAGER_NUMBER
AND SECRETARY.SECRETARY_NUMBER = EMPLOYEE.EMPLOYEE_NUMBER
AND MANAGER.EMPLOYEE_NUMBER = EMPLOYEE.EMPLOYEE_NUMBER;
Problem
But I can't get the Manager's Name to show up, or not repeat the same info as Secretary Name.
Any help would be greatly appreciated!
SELECT
s.SECRETARY_NUMBER,
s.EMPLOYEE_NUMBER AS SECRETARY_EMPLOYEE,
e.NAME AS SECRETARY_NAME,
s.MANAGER_NUMBER,
m.EMPLOYEE_NUMBER AS MANAGER_EMPLOYEE,
e2.NAME AS MANAGER_NAME
FROM
SECRETARY s
INNER JOIN
EMPLOYEE e
ON
e.EMPLOYEE_NUMBER = s.EMPLOYEE_NUMBER
INNER JOIN
MANAGER m
ON
m.EMPLOYEE_NUMBER = s.MANAGER_NUMBER
INNER JOIN
EMPLOYEE e2
ON
e2.EMPLOYEE_NUMBER = m.EMPLOYEE_NUMBER;

Microsoft SMSS - Joining tables on t2.primary key = t1.foreign_key

I need to join two tables together to create a table with columns for employee id, employee name and their boss' name.
The 'hier' table
The 'employees' table
The query I wrote is almost working, putting an employee name in the right spot, but not the right employee:
SELECT em.emp_id, em.emp_name, em.emp_name AS boss_name
FROM employees em
LEFT JOIN hier h ON (h.boss_id = em.emp_name)
Which outputs:
I need to have each person's boss to have the right name, and in the case of Big Boss, 'N/A'. Like so:
You need a self join with Employee table
SELECT em.emp_id, em.emp_name, e1.emp_name AS boss_name
FROM employees em
LEFT JOIN employees em1 ON em.boss_id = em1.emp_id

issue with hierarchical queries in db2

I have the following tables
Lead
id varchar
employee_id varchar
Employee
id varchar
lead_id varchar
There will be a group of employees assigned to a lead. The Lead table holds the employee id of the lead.
The employee table will have lead_id which will be the id key of the leader.
The table will also contain employees which are not assigned to any lead
I need a query which will display the hierarchical result which will list the leaders and the employees under the leader
leader1 (employee )
employee1
employee 2
Leader 2(employee)
employee 3
employee 4
Any idea how this kind of hierarchical result can be obtained by a db2 query?
Click on the this link to view the table structure
The answer is a join of the two tables like
SELECT l.employee_id as leader_employee_id, e.id as employee_id
FROM LEAD l
INNER JOIN EMPLOYEE e
ON e.lead_id = l.employee_id

SQL subquery Total/Count

I am trying to write a query that lists the name of a manager and the number of people they manage.
In the Manager table we have the managers name and id.
In the Employee table we have the employees name, id and managerID.
I don't understand how to get the count of the employees that a manager manages.
SELECT COUNT(e.EmpID), m.ManagerID
FROM Employee e
INNER JOIN Manager m
ON e.ManagerID= m.ManagerID
GROUP BY m.ManagerID
SELECT m.Name, COUNT(e.id) AS NumberOfEmployeesManaged
FROM Manager m INNER JOIN Employee e ON m.id = e.managerID
GROUP BY m.Name
That should do it I think, just a simple count of the employee ids after joining the manager and employee tables, grouped on manager name.
SELECT count(emp.empid), mgr.managerid
FROM Employee emp
INNER JOIN Manager mgr ON emp.managerid=mgr.managerid
GROUP BY mgr.managerid;
I don't know if you can use the COUNT aggregator in a JOIN. But you can run 2 queries. One would select the manager's name & id. The 2nd would look like this:
$id = the manager's id
SELECT COUNT(*) FROM Employee WHERE managerID=$id
Alternately, you could not use COUNT and run a query like this:
SELECT id FROM Employee WHERE managerID=$id
Then the # of resulting rows would be the count of employees managed by the manager.

Deriving a column's data from a matching column in SQL

So I have a table that has, employee number, employee name, supervisor number.
I want to run a query that will retrieve employee name, employee number, supervisor name and supervisor number. Only one employee doesn't have a supervisor meaning it will have to display nulls. How would I do this? I'm using Oracle SQL Plus. My attempts haven't worked at all! Any help would be much appreciated.
SELECT ename Employee, empno Emp#, super Manager#
FROM emp;
That gets me three of the columns but to be honest I don't even know where to start to get the supervisors names.
It's for university, but I'm studying for a test it's not for an assignment so no cheating happening here :).
The following should work, and give you nulls if the employee has no supervisor:
SELECT empGrunt.ename Employee
, empGrunt.empno EmpNum
, empSuper.ename SupervisorName
, empSuper.empno SupervisorName
FROM emp empGrunt LEFT OUTER JOIN emp empSuper
ON empGrunt.super = empSuper.empno
Assuming that SupervisorNumber is a foreign key relationship back to the Employee table (where it's the EmployeeNumber of the supervisor's record), then you need to use an outer join.
What you need in this case is a left join:
select
e.EmployeeName,
e.EmployeeNumber,
s.EmployeeName as SupervisorName
from Employee e
left join Employee s on s.EmployeeNumber = e.SupervisorNumber