SQL query that will add a column returning a value when two other columns match (similar to vlookup in excel) - sql

I have an employee table that lists all employees and the ID of their manager. The Manager ID refers back to the employee id in this table. I would like to add another column that returns the name of the manager since no one knows their ID numbers.
Current table is basically:
ID Name ManagerID
31 John Smith 10
32 Barb Jones 10
33 Craig Adams 32
I would like to add another column that looks up looks up the manager ID in the ID field and returns the assocaited name like below:
ID Name ManagerID ManagerName
31 John Smith 10 Ted Fish
32 Barb Jones 10 Ted Fish
33 Craig Adams 32 Barb Jones

You can use self-join here.
select e.ID, e.Name, m.ManagerID, e.Name as ManagerName
from employee e
inner join employee m
on e.ID = m.ManagerID;

Related

Pandas - join two dataframes based on one column from table and combining two columns from another table

I am trying to join two tables based on one column from one table and two column from other table
Table a
name, designation
Mr. james john, manager
Mr. jim james, tester
Mr. abe james, developer
Table b
first name, last name, emp id
james,john,1
jim, james,2
abe,james,3
I want to join table a name column with table b combining "Mr. "+first name+last name.
Here's a way to do what your question asks:
res = a.join(b.assign(name='Mr. ' + b['first name'] + ' ' + b['last name']).set_index('name'), on='name')
Input:
dataframe a:
name designation
0 Mr. james john manager
1 Mr. jim james tester
2 Mr. abe james developer
dataframe b:
first name last name emp id
0 james john 1
1 jim james 2
2 abe james 3
Output:
name designation first name last name emp id
0 Mr. james john manager james john 1
1 Mr. jim james tester jim james 2
2 Mr. abe james developer abe james 3

Access SQL concatenating items by order

I have a need to concatenate name information from two tables in a specific order. So far, all I have is the SQL to retrieve the data. I need help with the SQL to output a name string in the correct order.
example data:
TBL_TITLE_ORDER
ORDER_ID
ORDER_SEQUENCE
TITLE_ID
FIRSTNAME_ID
20
1
30
20
2
456
20
3
33
21
1
31
21
2
32
TBL_TITLE
TITLE_ID
TITLE_NAME
30
Mr
31
Mrs
32
Jones
33
Smith
TBL_FIRSTNAME
FIRSTNAME_ID
NAME
456
John
SELECT TBL_TITLE.TITLE_NAME, TBL_FIRSTNAME.NAME
FROM (TBL_TITLE_ORDER LEFT JOIN TBL_TITLE ON TBL_TITLE_ORDER.TITLE_ID = TBL_TITLE.TITLE_ID) LEFT JOIN TBL_FIRSTNAME ON TBL_TITLE_ORDER.FIRSTNAME_ID = TBL_FIRSTNAME.FIRSTNAME_ID
WHERE (TBL_TITLE_ORDER.ORDER_ID) = 20
ORDER BY TBL_TITLE_ORDER.ORDER_SEQUENCE;
The output I need is a complete name string in the proper order sequence. There may or may not be a record in the TBL_FIRSTNAME table.
What I have so far:
TITLE_NAME
NAME
Mr
John
Smith
Required output:
Mr John Smith
Mrs Jones

How to get employees listed under their manager? SQL

I have a database:
Staff Name
1 Blake
2 Jake
Mgr Emp
2 6
2 8
3 5
4 7
Is it possible to search up all employees but list them under the manager?
Select all:
Staff Name Mgr
1 Blake null
2 Jake null
6 Scott 2
8 Jack 2
3 Clark null
5 Martin 3
4 Smith null
7 Scott 4
And is there a way to look up the team when a staff is searched?
Search: "Scott"
Returns:
Staff Name Mgr
2 Jake null
6 Scott 2
8 Jack 2
http://rextester.com/OAREI7219 is solution
CREATE TABLE Managers (
MgrID int ,
EmpID int
)
CREATE TABLE Staff (
EmpID int,
name nvarchar(50)
)
Query is ;
SELECT
s.EmpID AS Staff,
s.name AS Name,
m.MgrID
FROM Staff s LEFT JOIN Managers m ON s.EmpID = m.EmpID ;
GO
Assuming that you have:
a table "staff" with all the people in the company. For clarity I will rename the "Staff" column to "id".
a many to many table "staff_manager" to link employees with their managers. For clarity I will rename the "Mgr" column to "manager_id" and the "Emp" to "employee_id".
all tables named lowercase and plural.
You may do:
SELECT employees.id, employees.name, managers.id, managers.name
FROM staff as employees
LEFT JOIN employee_managers ON (employee_managers.employee_id = employees.id)
LEFT JOIN staff as managers ON (managers.id = employee_managers.manager_id)
for the second query you add a where clause to the above query:
WHERE managers.name = "Scott" OR employees.name = "Scott"
NOTE: If you may change the model of your database, I would suggest to
set a UNIQUE constrain on the employee_id of the staff_managers table,
so that one employee can have ONLY ONE manager.
To get All Employees with their managers. Try
Select staff.Id,Staff.name,managers.mgr
From staff left join managers on staf.id=managers.emp
To search about employee and get his team with the manager also. Try
— Search: 'scott'
Select staff.Id,Staff.name,managers.mgr
From staff left join managers on staf.id=managers.emp
Where managers.mgr =(select t.mgr from managers as t where t.emp='scott')
Union all
Select staff.Id,Staff.name,managers.mgr
From managers left join staff on staf.id=managers.mgr
Where managers.emp='scott'

find names of table who is in the same department sql

I was asked during an interview about this SQL question.
Given Employee table
LastName DepartmentID
Rafferty 31
Jones 33
Heisenberg 33
Robinson 34
Smith 34
Daniel 34
Williams NULL
and Department table
DepartmentID DepartmentName
31 Sales
33 Engineering
34 Clerical
35 Marketing
How could I find ALL of the employees who is at the same department as 'Robinson'? (through a generic way)
Is join (inner join) something that he wanted me to do?
You can use IN:
select *
from employee
where departmentId in (
select departmentId
from employee
where LastName = 'Robinson'
);
Using JOIN:
select e.*
from employee e
join (
select distinct departmentId
from employee
where LastName = 'Robinson'
) e2 on e1.departmentId = e2.departmentId;

How to find all the employees under one manager using a subquery? SQL

does anyone know how to create a subquery to find all the employees under one manager? For example if I want to find someone that works on susan jones how would I create the query?
Manager Table:
Employee ID Manager_ID Name Title
1 NULL James Smith CEO
2 1 Ron Johnson Sales Manager
3 2 Fred Hobbs Sales Person
4 1 Susan Jones Support Manager
select employeename,managerid
from manager
where manager_id is not null
group by employeename ,manager_id
Well, supposing the table for employees is simply named Employee and its key is named 'Employee_ID' (like in the Manger table), then you just need to use a simple Join:
SELECT e.* FROM Employee AS e JOIN Manger AS m ON e.Employee_ID=m.Employee_ID WHERE m.Name='Susan Jones'