retrive sql query - sql

SQL query to retrive the following condition. There are two cloumns one with user_id;s as primary keys and another tale as manager key. i want to retrive the query so that it displays manager key along with the user id's as manager 1, manager 2

Try this: you can achieve this using a self join
SELECT e1.name, e1.managerid, e2.name, e2.user_id
FROM Employee e1
INNER JOIN Employee e2
ON e1.managerid= e2.user_id

Try this, you can use the same approach for finding the heirarchy:
SELECT e1.name AS EmployeeName
,e1.user_id AS USERID
,e2.name AS ManagerName1
,e2.user_id AS ManagerID1
,e3.name AS ManagerName2
,e3.user_id AS ManagerID2
FROM Employee e1
LEFT JOIN Employee e2
ON e1.managerid = e2.user_id
LEFT JOIN Employee e3
ON e2.managerid = e3.user_id

Related

How to find levels of managers in self-referenced key

So I have a table employees(ID, MaganerID)
My task is to find 2-nd level manager who is not getting any bonuses(another table)
I'm stuck here. Any advice what should I do next?
SELECT ID
FROM Employees
WHERE ID NOT IN (Select EmployeeID FROM Bonuses)
AND ID IN (Select LeaderID FROM Employees WHERE LeaderID IS NOT NULL)
AND ID NOT IN (Select ID FROM Employees WHERE ID <> LeaderID)
Well since you want a level 2 manager is much more simple:
SELECT DISTINCT E2.ID
FROM Employees E
INNER JOIN Employees E1 ON E1.ID = E.ManagerID
INNER JOIN Employees E2 ON E2.ID = E1.ManagerID
INNER JOIN Bonuses B ON B.EmployeeID = E2.ID AND B.Bonus = 0
You could also use the Recurse option using the WITH keyword if you are using TSQL. If you simply dont want to not exists on the other table just reverse the last bool check on the join.
Presumably by second-level manager you mean where their EmployeeID is somebody else's LeaderID, whose EmployeeID in turn is a third person's LeaderID?
I think currently you'll just get first level managers.
This should work... I've included your final WHERE statement, although you don't explain what that's for. Do you really want to exlude records where the ID is different to the LeaderID?
SELECT L2.ID
FROM Employees E
JOIN Employees L1 on E.LeaderID = L1.ID
JOIN Employees L2 on L1.LeaderID = L2.ID
WHERE L2.ID not in (SELECT EmployeeID from Bonuses)
AND L2.ID NOT IN (Select ID FROM Employees WHERE ID <> LeaderID)

Need help on a query

I have a table employees that looks like this:
Id Name Manager_Id
1 ABC 4
2 DEF 20
3 GHI 30
4 JKL 40
The below query does not return any results. I was expecting that it would return "JKL". What am I missing here?
select e1.Name from Employees e1 where e1.id =
(select e2.manager_id from employees e2 where e2.id = e1.id);
If you break this query down from the outside in, you're expecting to find a record where e1.id = e2.manager_id but also where e2.id = e1.id. In other words, you're querying for a record where id = manager.id, which simply does not exist.
What I assume you're trying to do is select all the records where the id exists as a manager_id in the table.
This can be done much more simply with an in operator:
SELECT name
FROM employees
WHERE id IN (SELECT manager_id FROM employees)
use in, not = :
select e1.Name from Employees e1 where e1.id IN
(select e2.manager_id from employees e2 where e2.id = e1.id);
The = operator is for testing if scalars are the same. IN tests set membership.

SQL query to find EmployeeName, Manager Name from a table

I have 2 tables as follows
EmployeeTable:
Employe_Manger:
Note that E1 is manager for E2, E3 and E4 are managers for E5.
Now find a query to find out the manager name and employee name side by side.
i.e. result should be
SELECT one.EmployeeName AS "Employee", two.EmployeeName AS "Manager"
FROM Employee AS one
INNER JOIN Employee_Manager AS temp ON one.EmployeeId = temp.EmployeeId
INNER JOIN Employee AS two ON temp.ManagerId = two.EmployeeId
Is it what you want to achieve?

SQL Server fetch alias name for query

Please check fiddle: myFiddle
Query:
create table Emp(empId int primary key, EmpName varchar(50),MngrID int)
insert into Emp(empId,EmpName,MngrID)values(1,'A',2)
insert into Emp(empId,EmpName,MngrID)values(2,'B',null)
A has mngr B but A has no mngr, so while fetching the record from query it shows me:
EmpId EmpName MngrName(alias MngrName)
1 A B
2 B null
How to fetch the above data using a query?
For some reason it doesn't work in SQLFiddle, but i ran it in my own instance of SQL Server to verify it does work:
SELECT e1.EmpID, e1.EmpName, e2.EmpName
FROM emp e1 LEFT OUTER JOIN emp e2
ON e1.MngrID = e2.EmpID
Basically, you're doing a 'self join' by declaring two instances of the table (e1 and e2), and then joining the first instance's MngrID to the second instance's EmpID.
You need to LEFT JOIN table to itself:
select A.empID, A.empName, b.empName as mgrName
from emp A left join emp B on A.mngrID = b.empID
http://sqlfiddle.com/#!3/184dc/8
select empId,EmpName,(SELECT EmpName FROM emp WHERE MngrID = amp1.MngrID) AS Manager from emp as amp1

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.