Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
This is the exercise and i dont know how to solve it(is with mysql)

--Returns a list with all the employees along with the data of the
departments where they work. The list must include employees who do not have
no associated department and departments that do not have any employees
associated. Sort the listing alphabetically by department name


What I have tried:

 i tried this but it doest work 

SELECT employee.name, department.name
FROM employee and INNER JOIN department d
ON d.code=e.department_code
WHERE e.department_code AND d.code IS NULL
ORDER BY d.name DESC; 
Posted
Updated 17-Mar-22 4:56am
Comments
CHill60 15-Mar-22 13:42pm    
Instead of saying "it doesn't work" share the sample data, the results you would expect from that data and the actual results you got. We don't have access to your database!

Firstly your syntax is incorrect
SQL
FROM employee and INNER JOIN department d
should be
SQL
FROM employee LEFT JOIN department d -- typo corrected 
Your next problem is that you have been told to include employees who have no associated department - which means you should not use an INNER join but use a LEFT OUTER JOIN instead. You will get a department.name of NULL where there is no associated department - deal with that in your display layer

Edit: In response to the comments I have returned to give a more complete (and corrected) answer.
Firstly - read up on SQL syntax. You cannot have
SQL
SELECT employee.name, department.name
if you having given your tables aliases - that d after department is the alias. You also cannot have
SQL
ON d.code=e.department_code
unless you also have the alias e - there is no alias after employee.

Next problem
SQL
WHERE e.department_code AND d.code IS NULL
e.department code is not a boolean so I guess you were trying to say
SQL
WHERE e.department_code IS NULL AND d.code IS NULL
Which it never could be because you did an inner join on those very columns. This article may help to explain why INNER join would never work in this case Visual Representation of SQL Joins[^]

However, my suggestion of using a LEFT OUTER join was erroneous - I should have suggested a FULL OUTER JOIN instead. Why? Because you want everything from both tables - something like this
SQL
SELECT e.name, d.name
FROM @employee  e
FULL OUTER JOIN @department d
ON d.code=e.department_code
ORDER BY d.name DESC;
Note there is no WHERE clause here - we want everything
 
Share this answer
 
v3
Comments
Luc Pattyn 15-Mar-22 14:55pm    
close, but no cigar...
apparently they also want to see empty departments?!?!
Maciej Los 15-Mar-22 15:42pm    
Take a look at the last 2 lines of solution. Ther you'll find a tip how to resolve your issue.
Luc Pattyn 15-Mar-22 15:50pm    
??they asked for employees with department,
plus employees without dep,
plus departments without employees.
So LEFT JOIN can't be the answer.
Maciej Los 15-Mar-22 15:58pm    
Sorry, Luc. I was thinking that it's OP's comment.
You're right and you're not :) Single left join does not resolve issue, but another will do.
CHill60 15-Mar-22 16:40pm    
True.
I also didn't put the ORDER BY clause in for them
In addiion with solution-1, as per question 'Sort the listing alphabetically by department name' should be ordered as ascending to sort alphabetically like.
SQL
ORDER BY d.name ASC;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900