SQL how to connect Managers to employee
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
SQL: Employee, Manager, City Names
JOIN
returns all rows from tables where the key record of one table is equal to the key records of another tableINNER JOIN
returns only matching tuples/rows if there is a much on the right of he compared tables.
While LEFT OUTER JOIN
returns all matching tuples/rows from both compared tables.
I recommend that you use LEFT OUTER JOIN
in your case.
Related Topics
How to Check If More Than One Column Is Not Null Per Record
Check If a Column Contains Text Using SQL
Adding $ Dollar Sign on My Total Cost in SQL Server
Select Distinct Records on a Join
Er_Access_Denied_Error: Access Denied for User ''@'Localhost' (Using Password: No)
How to Insert Null into the Datetime Coulmn Instead 1900-01-01 00:00:00.000 in SQL Server
Insert Value into Table If Conidition Is Met At Least One Time
Find a Matching Value in Date Range and Return Value
How to Use Select Distinct and Concat in the Same SQL Statement
Job for Mysqld.Service Failed See "Systemctl Status Mysqld.Service"
How to Store Emoji Character in MySQL Database
Sql71501 - How to Get Rid of This Error
Selecting a Single Record from the Same Multiple Rows from a Single Table
Pg Copy Error: Invalid Input Syntax for Integer
Daily Report by Date With Mssql for Mutiple Column
How to Replace Single-Quote With Double-Quote in SQL Query - Oracle 10G