Microsoft SMSS - Joining tables on t2.primary key = t1.foreign_key - sql

I need to join two tables together to create a table with columns for employee id, employee name and their boss' name.
The 'hier' table
The 'employees' table
The query I wrote is almost working, putting an employee name in the right spot, but not the right employee:
SELECT em.emp_id, em.emp_name, em.emp_name AS boss_name
FROM employees em
LEFT JOIN hier h ON (h.boss_id = em.emp_name)
Which outputs:
I need to have each person's boss to have the right name, and in the case of Big Boss, 'N/A'. Like so:

You need a self join with Employee table
SELECT em.emp_id, em.emp_name, e1.emp_name AS boss_name
FROM employees em
LEFT JOIN employees em1 ON em.boss_id = em1.emp_id

Related

INNER JOIN and Count POSTGRESQL

I am learning postgresql and Inner join I have following table.
Employee
Id Name DepartmentId
1 John S. 1
2 Smith P. 1
3 Anil K. 2
Department
Department
Id Name
1 HR
2 Admin
I want to query to return the Department Name and numbers of employee in each department.
SELECT Department.name , COUNT(Employee.id) FROM Department INNER JOIN Employee ON Department.Id = Employee.DepartmentId Group BY Employee.department_id;
I dont know what I did wrong as I am new to database Query.
When involving all rows or major parts of the "many" table, it's typically faster to aggregate first and join later. Certainly the case here, since we are after counts for "each department", and there is no WHERE clause at all.
SELECT d.name, COALESCE(e.ct, 0) AS nr_employees
FROM department d
LEFT JOIN (
SELECT department_id AS id, count(*) AS ct
FROM employee
GROUP BY department_id
) e USING (id);
Also made it a LEFT [OUTER] JOIN, to keep departments without any employees in the result. And COALESCE to report 0 employees instead of NULL in that case.
Related, with more explanation:
Query with LEFT JOIN not returning rows for count of 0
Your original query would work too, after fixing the GROUP BY clause:
SELECT department.name, COUNT(employee.id)
FROM department
INNER JOIN employee ON department.id = employee.department_id
Group BY department.id; --!
That's assuming department.id is the PRIMARY KEY of the table, in which case it covers all columns of that table, including department.name. And you may want LEFT JOIN like above.
Aside: Consider legal, lower-case names exclusively in Postgres. See:
Are PostgreSQL column names case-sensitive?

SELECT or JOIN using same column twice

So I have three tables: Employee, Secretary and Manager
Given Schema
The Employee table has the following columns:
Employee_Number
Name
Home Address
Telephone Number
The Secretary table contains:
Secretary_Number
Employee_Number (linked with foreign key to Employee table)
Manager_Number (linked with foreign key to Manager table)
The Manager table contains:
Manager_Number
Employee_Number (linked with foreign key to Employee table)
What's required and what I tried
I am trying to do a JOIN so that I can see following columns:
Secretary's Number
Secretary's Name
Manager's Number
Manager's Name
I have the following join statement, which shows all the columns, and shows the Secretary's name and number, as well as the Manager Number:
SELECT
SECRETARY.SECRETARY_NUMBER,
SECRETARY.EMPLOYEE_NUMBER AS SECRETARY_EMPLOYEE,
EMPLOYEE.NAME AS SECRETARY_NAME,
SECRETARY.MANAGER_NUMBER,
MANAGER.EMPLOYEE_NUMBER AS MANAGER_EMPLOYEE,
EMPLOYEE.NAME AS MANAGER_NAME
FROM SECRETARY, MANAGER, EMPLOYEE
WHERE SECRETARY.MANAGER_NUMBER = MANAGER.MANAGER_NUMBER
AND SECRETARY.SECRETARY_NUMBER = EMPLOYEE.EMPLOYEE_NUMBER
AND MANAGER.EMPLOYEE_NUMBER = EMPLOYEE.EMPLOYEE_NUMBER;
Problem
But I can't get the Manager's Name to show up, or not repeat the same info as Secretary Name.
Any help would be greatly appreciated!
SELECT
s.SECRETARY_NUMBER,
s.EMPLOYEE_NUMBER AS SECRETARY_EMPLOYEE,
e.NAME AS SECRETARY_NAME,
s.MANAGER_NUMBER,
m.EMPLOYEE_NUMBER AS MANAGER_EMPLOYEE,
e2.NAME AS MANAGER_NAME
FROM
SECRETARY s
INNER JOIN
EMPLOYEE e
ON
e.EMPLOYEE_NUMBER = s.EMPLOYEE_NUMBER
INNER JOIN
MANAGER m
ON
m.EMPLOYEE_NUMBER = s.MANAGER_NUMBER
INNER JOIN
EMPLOYEE e2
ON
e2.EMPLOYEE_NUMBER = m.EMPLOYEE_NUMBER;

How to combine two columns from different tables that have a similar name but have different values in SQL Server

I have three tables (example) STAFF, STU, EMP.
I want to combine the column EMPID in table STAFF and table EMP into 1 column?
My previous query is like this,
SELECT *
FROM STU s
FULL OUTER JOIN STAFF st ON st.STAFFID = STUID
FULL OUTER JOIN EMP e ON s.STUID = st.EMPID
The result is like this
The expected result is just like the above screenshot, but I want to join EMPID into one column only.
UPDATE:
I tried using this query:
SELECT
stu.stuid, stu.stuname, stu.stucode,
s.staffid, s.staffname, s.staffcode,
emp.empname, emp.empcode,
COALESCE (emp.empid, staff.staffid) AS col
FROM
STU, Staff, EMP
FULL OUTER JOIN
STAFF s ON s.STAFFID = stu.STUID
FULL OUTER JOIN
EMP e ON stu.STUID = s.EMPID
but it displays an error like this
Use below query to get the desired result.
SELECT s.StuID, s.StuName, s.Stucode, st.StaffId, st.StaffName, st.Staffcode, isnull(st.EmpId, e.EmpId) EmpId, e.EmpCode, e.EmpName
FROM STU s FULL outer JOIN
STAFF st
ON st.STAFFID = STUID FULL OUTER JOIN
EMP e
ON s.STUID = st.EMPID
Note: You will get the one emp Id column as needed. If Staff emp id is not null then staff emp id will be displayed else employee emp id will be displayed

Query for a self join for employee table

Suppose I have an employee table. I have Name and Manager columns. Say there are 10 employees of which 2 are managers. So Name will have 10 names and Manager name would be in Manager column.
How to use self join? I am just learning self join
To perform a self join, you simply give the same table a different alias.
For example, in your employee table you would have a managerid - which stores the id of the manager.
Then to get the manager's name - you just self join to the employee table on managerid - using a different alias (I have used m in the example below):
For example, your table would look like this:
CREATE TABLE Employees (id INT, Name VARCHAR(20), ManagerId INT);
To get the Employee's Name and his/her Manager's Name, you would do something like this:
SELECT
e.Name AS EmployeeName,
ISNULL(m.Name, 'No Manager') AS ManagerName
FROM employee e
LEFT JOIN employee m on m.id = e.ManagerId
If you want to learn more about self joins - see here

SQL joining three tables and selecting information

I have 3 tables
Doctor - Staff_ID, Name, Position.
Consists_Of - Staff_Id, Team_Code.
Team - Team_Code, Telephone_No, Staff_ID
The team table consists of the team leader for each team who is also a doctor,
I need to return a table that has team_code, staff_Id, name, position
I have got
SELECT DISTINCT Team.team_code, Doctor.staff_ID, name, position
FROM Doctor, Team LEFT OUTER JOIN consists_of
ON Team.Team_code = consists_of.Team_code
But this is giving every each member of stay the team code, t1 and then t2 and then t3 and so on.
Any ideas?
What you need is use JOIN. The repetition occurred because you didn't specify on what field should Doctor and Team be joined
SELECT Team.team_code, Doctor.staff_ID, name, position
FROM Doctor
LEFT OUTER JOIN consists_of
ON Doctor.Staff_Id=consists_of.Staff_Id
LEFT OUTER JOIN Team
ON Team.Team_code = consists_of.Team_code
But why do you have Staff_Id in Team table?