Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Queries in SQL

Tags:

sql

I'm trying to create a query that will display the name of the staff who are not mentoring any other staff. It should also be ordered by the surname.

So far, I've got this:

SELECT a.name, m.mentor
FROM accountant AS a 
LEFT OUTER JOIN accountant AS m ON a.mentor = m.staff_id
WHERE m.mentor = NULL
ORDER BY m.surname;

When I run the query it doesn't return any results.

Any help would be nice.

like image 398
user2949666 Avatar asked Dec 02 '25 05:12

user2949666


2 Answers

Try To Use IS Null Not = Null

SELECT a.name, m.mentor
FROM accountant AS a LEFT OUTER JOIN accountant AS m
ON a.mentor = m.staff_id
WHERE m.mentor is NULL ///  here
ORDER BY m.surname;
like image 157
Mahmoude Elghandour Avatar answered Dec 03 '25 19:12

Mahmoude Elghandour


SELECT a.name, m.mentor
FROM accountant AS a LEFT OUTER JOIN accountant AS m
ON a.mentor = m.staff_id
WHERE m.mentor IS NULL
ORDER BY m.surname;

You need to use IS NULL, you cant check if the value is equals(=) to NULL

like image 26
Christian Phillips Avatar answered Dec 03 '25 19:12

Christian Phillips



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!