SQL query to show Name and Department in a table - sql

Can you please help me, I don't know how to create this query. I am a front end dev.
Expected result should be table with 2 columns
Name(one) DepartmentName(many)
The tables and their relationship are shown in this image:

You can do this with a couple of INNER JOINS. You don't need to reference the Location table in this query because Employee.LocationId is the same as EmployeeDepartment.LocationId.
This simple query will return all employee names and all department names they are related to. The employee name may be repeated, as this query puts only one department name in the column, so if an employee is in two departments, you get the employee name twice.
SELECT
EmployeeName = e.Name
,DepartmentName = d.Name
FROM Employee e
INNER JOIN EmployeeDepartment ed ON ed.LocationId = e.LocationId
INNER JOIN Department d ON d.id = ed.DepartmentId
This query is a bit more complicated, but returns each employee name only once, and the department names will be a comma-separated string of names. This is accomplished by using STUFF() in conjunction with FOR XML.
;WITH EmployeesAndDepartments AS
(
SELECT
EmployeeName = e.Name
,DepartmentName = d.Name
FROM Employee e
INNER JOIN EmployeeDepartment ed ON ed.LocationId = e.LocationId
INNER JOIN Department d ON d.id = ed.DepartmentId
)
SELECT
ead.EmployeeName
,Departments = STUFF((
SELECT ',' + DepartmentName
FROM EmployeesAndDepartments
FOR XML PATH('')
) , 1, 1, ''
)
FROM EmployeesAndDepartments ead
GROUP BY ead.EmployeeName

This should work
SELECT
e.Name
, d.Name AS DepartmentName
FROM
Employee e
LEFT OUTER JOIN
EmployeeDepartment ed
ON e.LocationId = ed.LocationId
LEFT OUTER JOIN
Department d
ON ed.DepartmentId = d.id
Note that the use of LEFT OUTER JOIN will return the employee even if they don't have a corresponding record in EmployeeDeparatment and/or Department. If you only want to retrieve employees that do have corresponding EmployeeDepartment and Department records, use INNER JOIN instead

Related

find all employees' names who has a manager that lives in the same city as them

I am trying to figure out how to "find all employees' name who has a manager that lives in the same city as them." For this problem, we have two tables. We need to make a query.
"employee"
The employee table that we can refer to has both normal employees and managers
employeeid
name
projectid
city
1
jeff
1
new york
2
larry
1
new york
3
Linda
2
detroit
4
tom
2
LA
"Managertable"
Our manager table which we can refer to with mangerid = employeeid
projectid
mangerid
1
2
2
3
Right now I have found a way to get just the employees and filter out the managers, but now I am trying to figure out the next step to get to the comparison of managers and employees. Would this just be another subquery?
SELECT name
FROM employee e
WHERE employeeid not in(
SELECT mangerid
FROM Managertable pm
INNER JOIN employee e
ON pm.mangerid= e.employeeid);
Expected result :
employee name
jeff
I think the easient way to achieve this would be like this:
SELECT
e.*
FROM employee e
inner join Managertable mt on e.projectid = mt.projectid
inner join employee manager on mt.mangerid = manager.employeeid
WHERE
e.city = manager.city
and e.employeeid <> manager.employeeid;
One approach is a correlated subquery in which we look up the employee's manager's city.
select e.name
from employee e
where city =
(
select m.city
from managertable mt
join employee m on m.employeeid = mt.managerid
where mt.projectid = e.projectid
and m.employeeid <> e.employeeid
);
The same thing can be written with an EXISTS clause, if you like that better.
Based off the table structure you're showing, something like this might work
First find the employee ids of employees who have managers in the same city, then join it back on employee to retrieve all data from the table
;WITH same_city AS (
SELECT DISTINCT e.employeeid
FROM employee AS e
INNER JOIN managertable AS mt ON e.projectid = mt.projectid
INNER JOIN employee AS m ON mt.managerid = e.employeeid
WHERE e.city = m.city
)
SELECT e.*
FROM employee
INNER JOIN same_city AS sc ON e.employeeid = sc.employeeid
I don't see how projectid is relevant in your question because you didn't mention that as a requirement or restriction. Here's a method using a CTE to get the managers and their cities, then join to it to find employees who live in the same city as a manager.
with all_managers as (
select distinct m.managerid, e.city
from manager m
join employee e
on m.managerid = e.employeeid
)
select e.name
from employee e
join all_managers a
on e.city = a.city
and e.employeeid <> a.managerid;
name
jeff
But it you want us to assume that an employee reports to only that manager as listed in the projectid, then here's a modification to ensure that is met:
with all_managers as (
select distinct m.managerid, e.city, e.projectid
from manager m
join employee e
on m.managerid = e.employeeid
)
select e.name
from employee e
join all_managers a
on e.city = a.city
and e.projectid = a.projectid
and e.employeeid <> a.managerid;
View on DB Fiddle
You just need two joins:
one between "managers" and "employees" to gather managers information
one between "managers" and "employees" to gather employees information with respect to the manager's projectid and city.
SELECT employees.name
FROM managers
INNER JOIN employees managers_info
ON managers.mangerid = managers_info.employeeid
INNER JOIN employees
ON managers.projectid = employees.projectid
AND managers_info.employeeid <> employees.employeeid
AND managers_info.city = employees.city

Writing a SQL query to return data from multiple tables

I am working through a database textbook and I am stuck on a certain exercise.
Question 6 on page 74 of the book, and a copy of the database I am working on can be found here. On page 72 you can see portions of the two tables. I want to write an SQL query that will return the name of the department, the last name of the manager/head of the department and the last name of the employees who work there.
E.g.
Marketing Herring Holt
So I have been able to print the department name, the empId of the manager and the last names of the employees with the following SQL query.
SELECT department, manager, lastName
FROM Department d
INNER JOIN Employee e
ON e.dept = d.deptId;
Use self join to get the manager-employee relationship records
SELECT d.department,
e1.lastname As Employee,
e.lastname As Manager
FROM (employee AS e
INNER JOIN department AS d
ON d.deptid = e.dept )
INNER JOIN employee AS e1
ON e1.supervisor = e.empid
EDIT: The exact solution to the question asked
SELECT d.department, m.lastName, e.lastName
FROM (employee AS e INNER JOIN department AS d ON d.deptId = e.dept)
INNER JOIN employee AS m ON d.manager = m.empId;

Database Values displaying NULL

I have two databases.
Table 1 (Student) has fields StudentID, EmployeeClass, StudentName. Here, EmployeeClass is varchar(5).
Table 2 (Employee) has fields EmployeeID, EmployeeName, Description, and EmployeeClass. Employee Class is nvarchar(5).
when i am writing Select Query as:
SELECT S.StudentID,
S.EmployeeClass,
S.StudentName
FROM Student AS S
LEFT OUTER JOIN Employee AS E
ON CAST(S.EmployeeClass AS VARCHAR(5)) = E.EmployeeID
WHERE E.Description = 'ABC'
All the values From employee table are showing as NULL? How to fix it
I think the join condition is wrong in your script.column 'EmployeeClass' refers to 'EmployeeID' in your script.
SELECT S.StudentID,
S.EmployeeClass,
S.StudentName
FROM Student AS S
LEFT JOIN Employee AS E
ON cast(S.EmployeeClass as varchar(5))= E.EmployeeClass
AND E.Description = 'ABC'

How to include rows with missing data in SQL Server 2008

Let's say I have a table a table of employees and another employee_address.
Additionally, I have a table of employee_email_address (employees don't have to have an email)
select
employee.emp_id,
employee_address.address,
employee_email_address.email
from
employees
inner join
employee_address on employee.emp_id = employee_address.emp_id
inner join
employee_email_address on employee_email_address.emp_id = employee.emp_id
but what this query is giving me is only the employees who have an address AND an email address....
How can I include rows for which there is no email_address for the employees? (Note, the email address is not null)
Thanks.
Use left join instead of inner join.
Try this query:
Select e.emp_id, ea.address, eea.email
from employees e left join
employee_address ea
on ea.emp_id = e.emp_id left join
employee_email_address eea
on eea.emp_id = e.emp_id;

SQL JOIN : Help using join inisted subquery

I have 2 tables,
The first one is called emp, and has 2 columns called id and name
The second one is called dep, and has columns called id and empid and nameOfDep
if I want to list all emp that have X dep, but don't have Y dep
This is an example I use
Select e.id, e.name
from emp e
where e.id in (Select empid from deptid where deptid=X)
and e.id not in (Select empid from deptid where deptid=Y);
How I can make it using JOIN instead of with subqueries?
An IN can be converted into an INNER JOIN. A Not IN can be converted to LEFT JOIN / NULL Test. Sometimes called an ANTI JOIN.
SELECT e.id,
e.name
FROM emp e
INNER JOIN deptid D_X
ON e.empid = d_x.empid
AND deptid = 'X'
LEFT JOIN deptid D_Y
ON e.empid = d_Y.empid
AND deptid = 'Y'
WHERE d_Y.empid IS NULL
Also I'm making the assumption that when you wrote deptid = X that you meant X to be a literal string and not a field name
SELECT e.id, e.name
FROM emp e
INNER JOIN dep d ON (e.deptID = d.deptID AND d.deptID NOT y)
Add the Department ID to the employee record and then join on that.
EDIT
My bad, updated.
EDIT
Helps to read, go with Conrad's answer.