Manager name starting with letter - sql

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%';

Related

How to join multiple tables to get names of employees who earn more than their manager?

I have created two tables:
create table DEPARTMENTS
(
dept_id number,
dept varchar2(50) not null,
mgr_id number,
location varchar2(50),
constraint pk_departments primary key (dept_id)
);
create table EMPLOYEES
(
emp_id number,
fname varchar2(50) not null,
lname varchar2(50) not null,
email varchar2(50),
phone number,
hired date,
job_id varchar(20),
salary number(7,2),
commission_pct number(4,2),
dept_id number,
grade number,
constraint pk_employees primary key (emp_id),
constraint fk_employees_dept_id foreign key (dept_id)
references DEPARTMENTS (dept_id)
constraint fk_employees_grade foreign key (grade)
references SALGRADE (grade)
);
How go I get names of employees who earn more than their managers given that the mgr_id is in table departments and the employee is in employees table and they are linked by dept_id ?
I found that 'self-join' is needed. But this case is a bit difficult since referencing dept_id is needed to refer to the mgr_id then compare it to the emp_id and create join ?
For better clarity and self understanding, using ANSI joins:
SELECT * FROM
(SELECT E.SALARY AS MANAGER_SALARY, D.DEPT_ID AS DEPT_ID
FROM EMPLOYEES E
INNER JOIN
DEPARTMENTS D
ON E.DEPT_ID = D.DEPT_ID)
MGR_DET
INNER JOIN
EMPLOYEES EMP
ON MGR_DET.DEPT_ID = EMP.DEPT_ID
where EMP.SALARY > MGR_DET.MANAGER_SALARY;
You can do this with three joins. Start with the employees table, match to departments to get the manager. And then match back to employees using the manager.
This looks like:
select e.*, em.salary -- or whatever columns you want
from employees e join
departments d
on d.dept_id = e.dept_id join
employees em
on em.emp_id = d.mgr_id
where e.salary > em.salary
Answer with implicit joins:
SELECT E1.* FROM EMPLOYEES E1, DEPARTMENT D, EMPLOYEES E2
WHERE E1.DEPT_ID = D.DEPT_ID AND D.DEPT_ID = E2.DEPT_ID AND D.MGR_ID = E2.MGR_ID
AND E1.SALARY >= E2.SALARY;
Here the employee E1 is the employee whose salary is greater than that of his department manager.
To find that, we join the employee table E1 and department table D to get on dept_id and then fetch the mgr_id. Now the next step is to find the employee with the same ID as the mgr_id of the department. Hence we again join it with another employee table E2 on emp_id. Finally we compare the salary of employee e1 and the manager (employee e2).
Answer with explicit joins:
SELECT E1.* FROM EMPLOYEES E1 JOIN DEPARTMENT D ON E1.DEPT_ID = D.DEPT_ID
JOIN EMPLOYEES E2 ON (D.DEPT_ID = E2.DEPT_ID AND D.MGR_ID = E2.MGR_ID)
WHERE E1.SALARY >= E2.SALARY;

Join two table with department wise maximum salary

Department wise maximum salary in a table it's working:
select * from employee where (emp_dept, emp_sal)
in (select emp_dept, max(emp_sal) from employee group by emp_dept);
but my concern is : i want emp_id, emp_name, emp_sal, emp_dept column with department wise maximnum salary
customer table : cust_id, cust_name, emp_id
employee table : emp_id, emp_name, emp_sal, emp_dep
I think you want :
select e.emp_id, e.emp_name, e.emp_sal, e.emp_dept
from employee e
where e.emp_sal = (select max(e1.emp_sal) from employee e1 where e1.emp_dept = e.emp_dept);
Assuming you want all the records from the employee table with the maximum salary added, then you could use something like:
Select e.emp_id, e.emp_name, e.emp_sal, e.emp_dept, d.max_salary
from employee e
left join
(select emp_dept,max(emp_sal) as max_salary
from employee
group by emp_dept) d
ON e.emp_dept=d.emp_dept
The sub query calculates the maximum salary for each department and then using the left join you can get it into the final table.
If you want the employee details of the employee with the maximum salary, then:
Select e.emp_id, e.emp_name, e.emp_sal, e.emp_dept, d.max_salary
from employee e
inner join
(select emp_dept,max(emp_sal) as max_salary
from employee
group by emp_dept) d
ON e.emp_dept=d.emp_dept and e.emp_sal=d.max_salary
In this case the join is based on the salary component as well, thereby returning only the employees with the maximum salary. Hope this helps.
Just use a window function:
select e.*,
max(emp_sal) over (partition by emp_dept) as dept_max_sal
from employee ;

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

small tricky query on self join

I have a table EMP with columns as below:
create table emp(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
mgr_id number(4,0),
sal number(7,2),
deptno number(2,0));
I want to list all employees' names along with their manager names, including those who do not have a manager. For those employees, their manager's name should be displayed as 'BOSS'.
The following query should work:
select e.ename, (case when m.ename is null then 'BOSS' else m.ename end) as mgrName
from emp e
left join emp m on m.empno = e.mgr_id
To my mind, the better solution is proposed by Charanjith.
In Oracle, we could even use NVL function instead of "case when" in order to replace null value by something. The result should be the same.
select e.ename empName, NVL(m.ename, 'BOSS') mgrName from emp e
left join emp m on m.empno = e.mgr_id
Moreover, we could see another solution : using inner join to filter on emp when a manager exists. Then union for all employees who don't have any manager.
select e.ename empName, m.ename mgrName from emp e inner join emp m on e.mgr_id = m.empno
union
select e.ename empName, 'BOSS' mgrName from emp e where not exists (select 1 from emp m where e.mgr_id = m.empno)
This work fine in oracle:
SELECT e.ename,
nvl(m.ename, 'BOSS')mgr
FROM emp a
LEFT JOIN emp b
ON m.empno = e.mgr_id;

Query to fetch the manager name by managerID

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