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
Related
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
I wondering is what I am doing right.
select distinct
Departments.Department_No, Departments.Department_Name
from
Departments
join
Employees on Departments.Department_No = Employees.Department_No
join
Jobs on Jobs.Job_ID = Employees.Job_ID
where
Departments.Department_No not in (select distinct Department_No
from Employees
where Employees.Job_ID like '%SA_REP%');
You want to display distinct values of Department Number and Department Name
You join Employees table with Department on Department Number
You join Jobs table with Employees on Job ID
You filter the result by excluding those Department Numbers of the entire Employee table that have a Job ID matching the pattern %SA_REP%
In my opinion you don't need
the join with the Jobs table
the join with the Employees table
you could maybe see if one of the other users' suggestions can bring performance improvement
SELECT DISTINCT departments.department_no,
departments.department_name
FROM departments
WHERE departments.department_no NOT IN (SELECT DISTINCT department_no
FROM employees
WHERE employees.job_id LIKE '%SA_REP%'
);
You could simply do this using NOT EXISTS instead of using a NOT IN subquery:
SELECT DISTINCT
d.Department_No
,d.Department_Name
FROM Departments d
JOIN Employees e ON d.Department_No = e.Department_No
WHERE NOT EXISTS
(select 1
from Employees e1
where e1.Job_ID like '%SA_REP%'
AND e1.Department_No = e.Department_No);
You can translate where condition without "in".
And you don't need to fetch date from "Jobs" - you don't use it
Select distinct Departments.Department_No, Departments.Department_Name
from Departments
Join Employees on Departments.Department_No = Employees.Department_No
where Employees.Job_ID not like '%SA_REP%';
I have an EmployeeDepartmetn juction table like this. I have all the departments in Depeartment table and employees in Employee table..
I want to get departments for an particular employee along with the all the departments available in depeartment table.
It should be like Select DepartmentId, DepartmentName, EmployeeID from Query.
Main criteria here is, Need to display NULL if the employee dont have that department. I am confused here...please help.
Please give Linq Query
Thanks in Advance
Put criteria in your left join:
Select distinct a.DeptID, b.DepartmentName, b.EmployeeID
From Department a
left join EmployeeDepartment b
on a.DeptID = b.DeptID and b.EmployeeID = 1 --insert employee ID here
It will show all departments (even those with no employees), then show the employee ID you chose in the third column only if that employee is assigned there.
You can do this with conditional aggregation:
select DeptId,
max(case when EmployeeId = 1 then EmployeeId end) as EmployeeId
from EmployeeDepartment ed
group by DeptId;
EDIT:
If you have a departments table as well:
select d.deptid, d.name, ed.employeeid
from Departments d left join
EmployeeDepartment ed
on d.deptid = ed.deptid and
ed.employeeid = 1;
I am trying to use a sub query on an inner join to get back all department numbers and names from a table that do not have programmer in the department, but I am having a little trouble as it returns no values. Here is my code, thanks for any help.
select Departments.Department_Name, Departments.Department_No
from employees inner join departments
on departments.department_No = employees.Department_No
where Employees.Department_No !=
(select Department_Name
from Employees, Departments
where Job_ID = '%pro%')
From what I can gather, you want something like this:
select d.Department_Name, d.Department_No
from departments d
WHERE NOT EXISTS (SELECT 1 FROM Employees
WHERE d.Department_No = Department_No
AND Job_ID LIKE '%pro%')
This selects all departments, for which there doesn't exist an employee whose job_ib contains 'pro'.
Try this one:
select d1.Department_Name,d1.Department_No
from departments d1
where d1.Department_No in ( select e1.Department_No
from Employees e1
where e1.Department_No=d1.Department_No and
e1.job_id not in ('programmer'));
The goal here is to return the ID, Name, and Manager Name and ID for each Employee. The table does not contain a manager name, only a manager ID for a given employee. The current query works great using an inner join, except that one employee does not have a manager ID (he's the boss), and so none of his information appears. The current query looks like this:
SELECT DISTINCT e.employee_id AS EMPLOYEE_ID,
e.FULL_NAME AS EMPLOYEE_NAME,
m.manager_ID AS REPORTS_TO,
m.FULL_NAME AS MANAGER_NAME
FROM EMPS e
INNER JOIN EMPS m ON e.manager_id = m.employee_id;
How can I include the name and information for this employee despite his lack of a seemingly necessary field? Thanks
If you want to include the employee name when there is no manager do a left join:
SELECT DISTINCT e.employee_id AS EMPLOYEE_ID,
e.FULL_NAME AS EMPLOYEE_NAME,
m.manager_ID AS REPORTS_TO,
m.FULL_NAME AS MANAGER_NAME
FROM EMPS e
LEFT JOIN EMPS m ON e.manager_id = m.employee_id;