Northwind database | SQL query that returns employees and who they report to


Northwind database | SQL query that returns employees and who they report to



I am trying to write a query for the Northwind database that lists the employees and their manager's names, without including employees who have no one to report to.



Here is what I have so far:


SELECT employees.firstname, employees.lastname, superior.firstname, superior.lastname
FROM employees
LEFT OUTER JOIN employees superior ON employees.reportsto = superior.employeeID
WHERE employees.reportsto <> null;



This query runs, but nothing appears.





Sorry about the checking and unchecking the answer buttons for you guys repeatedly! I thought that I could pick more than one answer. 😥
– Help pls
Apr 17 '17 at 13:47





Tip of today: Declare table aliases for both employees instances, to make the code clearer.
– jarlh
Apr 17 '17 at 14:01




4 Answers
4



You should try:


SELECT employees.firstname, employees.lastname, superior.firstname, superior.lastname
FROM employees
LEFT OUTER JOIN employees superior ON employees.reportsto = superior.employeeID
WHERE employees.reportsto IS NOT NULL --or "<> NULL" when ANSI_NULLS is set to OFF ("!=" is specific to SQL server)



If you are using sql server, the default is to set ANSI_NULLS ON, you need to use IS/IS NOT to compare with NULL


sql server


set ANSI_NULLS


IS/IS NOT


NULL





It is about without including employees who have no one to report to so it should be IS NOT NULL
– Randika Ratnayake
Apr 17 '17 at 13:41





@RandikaRatnayake, my bad eyesight
– LONG
Apr 17 '17 at 13:42





Thank you very much!
– Help pls
Apr 17 '17 at 13:45



I got it, never ever ever try to compare a value to null. The proper answer is:


WHERE employeesAM.reportsto **is not** null;





you could use = or <> (!=) to compare with NULL only if you set the query option SET ANSI_NULLS to OFF
– LONG
Apr 17 '17 at 13:50


=


<>


!=


NULL


SET ANSI_NULLS



Try IS NOT NULL:


SELECT employees.firstname, employees.lastname, superior.firstname, superior.lastname
FROM employees
LEFT OUTER JOIN employees superior ON employees.reportsto = superior.employeeID
WHERE employees.reportsto IS NOT NULL;



Explanation:
NULL has no value, and so cannot be compared using the scalar value operators.
https://stackoverflow.com/a/5658472/684030





Thank you very much!
– Help pls
Apr 17 '17 at 13:45





Pleasure. Mark as the answer if that help you :)
– Randika Ratnayake
Apr 17 '17 at 13:47





Sorry! The other person beat you to it by 4 minutes, but I really appreciate the help! If I could pick multiple answers I absolutely would, but thanks again! :)
– Help pls
Apr 17 '17 at 13:48





All good no fuss :)
– Randika Ratnayake
Apr 17 '17 at 14:01


SELECT empa.employeeid,
empa.firstname,
empa.lastname,
empb.firstname AS 'Reports to Manager'
FROM employees empa
INNER JOIN employees empb
ON empa.reportsto = empb.employeeid






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages