SQL how to connect Managers to employee - mysql

I am still a rookie and learning SQL and I am stuck with two things That I can not do
-how to Display name of employees and names of their managers but they are on same table and column
-Find the names of the employees whose manager is “Salah Mohamed”
I know I can do it with inner join but it gets strange results when I try it.
Thanks in advance.
Department Table:
ID Name MGR_ID Branch no.
101 HR 80021 1
102 Technical support 80010 2
103 Marketing 80077 3
104 Logistics 78804 4
105 Engineering 56651 5
Emp Table:
ID Name dept_id
52213 Salah Mohamed 105
56651 Amr Saker 105
75515 Nahed Yousef 104
76605 Nour Waheed 102
78804 Amr Akl 104
80010 Ahmed Kamal 102
80021 Ahmed Attia 101
80045 Mona Ahmed 103
80065 Hesham Hamdy 101
80077 Lina Emad 103

You need to use inner join for entire solution and case .. when expression specially for the manager row as follows:
SELECT emp.NAME employee_name,
Case when emp.id <> mgr.id then mgr.NAME end as manager_name
FROM emp JOIN department d ON d.id = emp.dept_id
JOIN emp mgr ON d.mgr_id = mgr.id

This should solve it. Here you first join the employee table to department table to get the IDs of the manager. Then you join the department table to employee table again on manager id to get the names of the manager. Then you can put your manager name condition in where. Hope it helps.
Edit:
SELECT emp1.NAME employee_name,
mng.NAME manager_name
FROM emp emp1
JOIN department d
ON d.id = emp1.dept_id
JOIN emp mng
ON d.mgr_id = mng.id
where mng.NAME='Salah Mohamed'

Related

MySQL (version lower than 8.0) : SELECT WHERE Multiple column and group by max level

I have 2 tables in my database.
table of employees in my department. This table is stored centrally. Which I have no right to manage in this table
emp_id
name
job postion
dept_1
dept_2
dept_3
000010
emp1 name1
Director
CEO
Human resource
000012
emp2 name2
employee
CEO
Human resource
000013
emp2 name2
Director
CEO
Human resource
Recruitment
000014
emp2 name2
employee
CEO
Human resource
Recruitment
000015
emp2 name2
employee
CEO
Human resource
Recruitment
..
..
..
..
..
..
..
..
..
..
000200
emp2 name2
Head Director
CEO
Department table. It's a table that I have created to show my department.
dept_id
dept_level
dept_name
dept_order
1
1
CEO
1
2
2
Human Resource
2
2
3
Recruitment
3
2
3
Training
4
2
2
Sale
5
2
3
Sale planning
6
2
3
Sale marketing
7
I want to know Which department's employees are under?
I tried using the command :
SELECT *
FROM table1
INNER table2 ON (table2.dept_name = table1.dept_1 OR table2.dept_name = table1.dept_2 OR table2.dept_name = table1.dept_3)
But it's not the answer I want.
I want
emp_id
name
job postion
dept_level
dept_name
dept_order
000010
emp1 name1
Director
2
Human Resource
2
000012
emp2 name2
employee
2
Human Resource
2
000013
emp2 name2
Director
3
Recruitment
3
000200
emp2 name2
Head Director
1
CEO
1
What should I do?
P.S. Sorry my english
WITH cte AS ( SELECT *, MAX(d.dept_level) OVER (PARTITION BY e.emp_id) maxlevel
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3) )
SELECT *
FROM cte
WHERE dept_level = maxlevel
ORDER BY emp_id
For MySQL 5.7 / MariaDB 10.1 use
SELECT *
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3)
JOIN ( SELECT e.emp_id,
MAX(d.dept_level) maxlevel
FROM employees e
JOIN department d ON d.dept_name IN (e.dept_1, e.dept_2, e.dept_3)
GROUP BY e.emp_id ) ed USING (emp_id)
WHERE d.dept_level = ed.maxlevel
ORDER BY emp_id
fiddle

SQL identify the first name and last name, that is assigned to tasks to what company

I have 3 Tables, new to SQL, I'm Trying to Combine 2 tables (Users and Tasks) into Companies or into a new table. What I need is to probably replace under Companies Table: TaskID and UserID with TaskID and UserID in both Users Tasks and table, but I get an error
SELECT COMPANY.UserID, COMPANY.TaskID, (FirstName+' '+LastName) AS FullName, TASKS.TaskSubject, USERS.UserID
FROM USERS, TASKS, COMPANY
INNER JOIN COMPANY.UserID = USERS.UserID
INNER JOIN COMPANY.TaskID = TASKS.TaskSubject
USERS:
UserID FirstName LastName
1 John Green
2 Graham Dale-Jones
3 Francois Peters
4 Danika Snow
5 Jennifer Booth
6 Erin Harvey
7 Caleb Jackson
TASKS:
TaskID TaskSubject TaskManager
101 Install
102 Upgrade
103 Troubleshoot
104 Assign
COMPANY:
CompanyID TasksID UserID
1 101 1
1 101 2
2 102 2
3 103 3
4 103 4
5 104 7
Thank you for the help
Try the following, you first need to learn how to join. What you are using is implicit join (on where) which is not best practice, instead you should always use explicit join.
SELECT
c.UserID,
c.TaskID,
concat(FirstName, ' ' ,LastName) AS FullName,
t.TaskSubject,
u.UserID
FROM users u
INNER JOIN company c
on c.UserID = u.UserID
INNER JOIN tasks t
on c.TaskID = t.TaskId
Here is the demo.

Filtering keyword from second table

I have two tables
Employees
id employee_name JobTitle
---------------------------
1 John CEO
2 Ely MANAGER
3 Marcus MANAGER
4 Steve CEO
5 Fritz ASSISTANT
6 Orly ANALYST
7 Carlo ANALYST
7 Lee MANAGER
JobTitle Filter
filter_id JobTitle_keyword
---------------------------
1 CEO
2 MANAGER
Is it posible to use JobTitle_keyword as the filtering keyword?
so if I run the query... the result would be like this.
id employee_name JobTitle
---------------------------
1 John CEO
2 Ely MANAGER
3 Marcus MANAGER
4 Steve CEO
7 Lee MANAGER
Yes, you can do this in MySQL by using join.
SELECT t1.*
FROM Employees t1
JOIN JobTitleFilter t2
ON t1.jobtitle = t2.jobtitle_keyword
Check it working in this SQL fiddle.
You need to join the tables:-
SELECT emp.*
FROM Employees emp
INNER JOIN JobTitleFilter Job
ON emp.JobTitle = Job.JobTitle_Keyword
OR
Select emp.id,emp.employee_name,emp.JobTitle
FROM Employees as emp,JobTitle as Job
Where emp.JobTitle= Job.JobTitle_Keyword
SQL FIDDLE
try out this..
SELECT id,employee_name,JobTitle
FROM Employees e
INNER JOIN JobTitleFilter j ON e.JobTitle = j.JobTitle_Keyword
or you can also try this one..
SELECT id,employee_name,JobTitle
FROM Employees e
where e.JobTitle in (Select JobTitle_Keyword from JobTitleFilter)
SQL FIDDLE

how to get multiple items in one line in mysql?

I have an employees table, which has all the information about employees, including the manager_id, for example:
id name manager_id
1 Joe 5
2 Mary 5
3 Bill 5
4 Jane 6
5 Matt 6
6 Walt 7
I would like to get a list of people, and for each one all their direct reports. Is it possible to create a query to give me the following output:
Employee Direct Reports
Joe
Mary
Bill
Jane
Matt Joe, Bill, Mary
Walt Jane, Matt
This way:
SELECT s.name AS employee, group_concat( e.name )
FROM employees s
LEFT OUTER JOIN employees e ON s.id = e.manager_id
GROUP BY s.id
You have to join the table with itself. And you need to use left join, so that you get the employees who don't manage anyone.

How to get hierarchy of employees for a manager in mysql?

I have an employee table, with the following data.
For a particular manager, i want to get the list of all employees, following the hierarchy of the manager.
id name manager
1 John 6
2 Gill 7
3 Ben 2
4 Roy 8
5 Lenin 6
6 Nancy 7
7 Sam 0
8 Dolly 3
For example, i have to get the employees under manager Sam(7). As you can see, Sam does not have any manager, but he is the manager for employees Gill and Nancy, who are the managers for employees Ben and John, Lenin respectively.
So i ran a query like this:
select * from employee where manager=7;
I get the result as 2 rows, Gill and Nancy.
But now, i also want to show the employees Ben and John, Lenin in the output, as they both are under the managers Gill and Nancy, who are under Sam.
How can i structure the query to show the employees hierarchically for a manager? In other words, how can i show all Gill, Nancy, Ben, John and Lenin under the manager Sam ?
Adding a IN subquery to your WHERE clause will handle that. For example:
SELECT * FROM employee WHERE manager = 7 OR manager IN (SELECT id FROM employee WHERE manager = 7)
For a better visuality you can also use this query:
select e1.name, e2.name, e3.name
from employee e1
left join employee e2 on e1.id = e2.manager
left join employee e3 on e2.id = e3.manager
where e2.manager = 7
whereas you have to add another left join for each hierarchy. That is a downside on the one hand, on the other hand it's easier to handle than in Sean's answer.
On the pro side you get an output like this:
name name1 name2
---------------------------------
Sam Gill Ben
Sam Nancy John
Sam Nancy Lenin
which is much more eye friendly and you can easily put it in an even more eye friendly form via PHP or something.