Query to fetch the manager name by managerID - sql

I have a table named Employee and it has the fields as ID, Name, EmpID, rankID, DeptID and managerID. I have set the managerID as a foreign key to the Employee table with reference to ID in employee table. Now I want a query to fetch all the employee and their manager information. The "manager information should be manager name not the managerID."

select e1.ID, e1.Name, e1.EmpID, e1.rankID, e1.DeptID, e2.name as managername
from employee e1
left outer join employee e2 on e1.managerID = e2.id

Related

Manager name starting with letter

In a table with columns EMP_ID, EMP_NAME & MANAGER_ID fetch manager names for all employees whose names start with 'A'. Assume a manager is also an Employee.
Table: EMP
EMP_ID
EMP_NAME
MGR_ID
Let's assume you don't have a manager's table, all you have is this employee table. Then,
SELECT EMP_NAME
FROM EMP
WHERE EMP_ID IN (
SELECT MGR_ID
FROM EMP
WHERE EMP_NAME LIKE "A%"
)
should do the task.
You simply need a self join -
SELECT E1.EMP_NAME EMPLOYEE_NAME, E2.EMP_NAME MANAGER_NAME
FROM EMP E1
LEFT JOIN EMP E2 ON E1.MGR_ID = E2.EMP_ID
WHERE E1.EMP_NAME LIKE 'A%';

SQL Server : LEFT JOIN EMPLOYEE MANAGER relationship

I have a SQL Server table employee:
MANAGERID is a self-referencing foreign key to EMPLOYEEID. I want to build a query which generates output of a 4th column called DESIGNATION.
The desired result looks like this:
The logic is
Designation must be “Associate” when no other employee is reporting to this employee
Designation must be “Manager” when one or more employees are reporting to this employee
Designation must be “Head” when one or more managers are reporting to this employee
How to achieve this in SQL Server?
You can just use case with not exists:
select e.*,
(case when managerid is null then 'head'
when not exists (select 1 from employee e2 where e2.managerid = e.id)
then 'associate'
else 'manager'
end) as designation
from employee e;
EDIT:
So, "head" seems to mean "manager's manager". That can be accomplished as:
select e.*,
(case when exists (select
from employee e2 join
employee e3
on e2.id = e3.managerid
where e2.managerid = e.id
)
then 'head'
when not exists (select 1 from employee e2 where e2.managerid = e.id)
then 'associate'
else 'manager'
end) as designation
from employee e;

SQL query to get the employee name and their manager name from the same table

Employee table
Employee_id Employee_name Manager_id
-------------------------------------
Emp00001 Ram Emp00005
Emp00002 Sharath Emp00003
Emp00003 Nivas Emp00005
Emp00004 Praveen Emp00002
Emp00005 Maharaj Emp00002
Output
Employee Name Manager Name
------------------------------
Ram Maharaj
Sharath Nivas
Nivas Maharaj
Praveen Sharath
Maharaj Sharath
In the employee table, there are three columns Employee_id, employee_name and manager_id. From the table, how to fetch the employee name and their manager name?
You can self-join the table to get the manager's name from his ID:
SELECT e.employee_name, m.employee_name AS manager_name
FROM employee e
JOIN employee m on e.manager_id = m.employee_id
Please try this
SELECT employee_name AS Employee_Name,(SELECT employee_name FROM employee where
employeeid=ManagerID ) AS Manager_Name FROM employee
Using below query you can get Employeename and ManagerName here i have only one table EmpMgr:
select e.employeename as ename ,e.managerid as mgrid , e1.employeename as managername from EmpMgr e join EmpMgr e1 on e.managerid=e1.employeeid
Note : you can get all the employees name irrespective of manager name using the left join
select e.employeename as ename ,e.managerid as mgrid , e1.employeename as managername from EmpMgr e left join EmpMgr e1 on e.managerid=e1.employeeid
Select * from employee
select e1.empname,e2.empname as managername,e1.salary,
e1.mrg,e1.empno,e1.job,e2.mrg as BossMRG from employee e1
join
employee e2 on e1.mrg=e2.empno
left join
employee e3 on e1.mrg=e3.empno and e3.job='manager' or e3.empno=e2.mrg
Required table :- Employee.
Query :-
SELECT e.Employee Name,
e.Employee Name as Manager Name
FROM Employee e JOIN Employee m
ON e.Employee id = m.Manager id
Explanation :-
By giving this condition "Employee e JOIN Employee m" it would automatically consider single table as two different table as "e" and "m" and then compare Employee id from table e to the manager id of table m.
whenever it find match that both the ID's are same that will get added to the result.
Hello Friends, Please check below solution.
This is result screenshot:
This is the query:
SELECT e.employee_name,m.Employee_name FROM EmpTable e
INNER JOIN EmpTable m
ON M.Employee_id =e.manager_id
Hopefully, manager table will be available in your DB
SELECT employee_name, M.manager_name
FROM employee e
INNER JOIN tableManager M ON e.ManagerID = M.MangerID
SELECT e.ename,m.ename FROM Emp e
INNER JOIN Emp m
ON M.EMPNO =e.mgr

Get employee and his manager details without joins

I have a table Employee which contains Employee id and Manager id. I want to get Employee id, Employee's Manager id, and Employee's manager's Manager id.
I can get it using self join and other joins like
Select employe id, Manager id
from emplyee as a, employee as b
where a.manager_id = b.employee id
But is there a better way? Can we do this without joins, by only querying the table once?
If you don't consider a co-related subquery as a join you can do this:
Select e.employee_id,
e.manager_id,
(select manager_id
from employee e2
where e2.employee_id = e.manager_id) as employee_manager_id
from employee e;
But at some point you have to do some kind of "join".
you can use recursive CTE but still you have to use a join
Look at this example:
WITH MyCTE
AS (
SELECT EmpID, FirstName, LastName, ManagerID
FROM Employee
WHERE ManagerID IS NULL
UNION ALL
SELECT EmpID, FirstName, LastName, ManagerID
FROM Employee
INNER JOIN MyCTE ON Employee.ManagerID = MyCTE.EmpID
WHERE Employee.ManagerID IS NOT NULL
)
SELECT *
FROM MyCTE
please refer to this link for detail of CTE by Pinal Dave
http://blog.sqlauthority.com/2012/04/24/sql-server-introduction-to-hierarchical-query-using-a-recursive-cte-a-primer/

display all departments and all employees

if there :
(department) table: (id,name)
(employee) table : (id,dept_id,name)
how to show every department (id,name), then all employees (id,name) in this department under its department.
I'd like it as SQL statment
You need to use JOIN
I believe it's something like this:
SELECT department.id, department.name, employee.id, employee.name
FROM department
LEFT JOIN employee
ON department.id=employee.dept_id
ORDER BY department.id
Since all employees must be present under a particular department at any time, you can do a inner join on both the table with dept_id like
SELECT dept.id, dept.name, emp.id, emp.name
FROM department dept
JOIN employee emp
ON dept.id=emp.dept_id
Simply try this
SELECT D.ID,D.Name,E.ID,E.Name
FROM Department D Left JOIN Employee E ON E.dept_id = D.Id