MIcrosoft SQL, query with ANY joins

I am having trouble displaying one of the columns in this query:

SIN, last name, and salary of employees who earn more than $80,000, if they are managers show the branch name of their branch in a fourth column (which should be NULL for most employees), order by salary in decreasing order
The restrictions is I have to create without any joins

Currently My output is this:

sin              lastName      salary      branchName
55700      Edwards              99289      NULL
95246      Garcia              98773      NULL

the problem is that the branchName for ALL rows are NULL
This is what I have done so far:
SELECT E.sin, E.lastName, E.salary, b.branchName
  FROM Employee E, Branch B
  WHERE B.managerSIN = E.sin AND E.sin <> B.managerSIN
UNION
SELECT E.sin, E.lastName, E.salary, null
  FROM Employee E
 WHERE NOT EXISTS (
                   SELECT *
                     FROM Branch B
                    WHERE E.salary < 80000
                  )
ORDER BY salary DESC

Open in new window


Here are the relevant tables:
Employee = {sin, firstName, lastName, salary, branchNumber(FK-Branch)}
Branch = {branchNumber, branchName, managerSIN(FK-Employee), budget}
where FK = foreign key and underline = primary key
Bob TianAsked:
Who is Participating?
 
Pawan KumarConnect With a Mentor Database ExpertCommented:
@_agx_  -- No It will do the CROSS JOIN.

SELECT E.sin
      , E.lastName
      , E.salary
      , b.branchName
FROM   Employee E , Branch B
WHERE  ( B.managerSIN = E.sin AND E.salary > 80000 ) OR ( B.managerSIN IS NULL )
ORDER BY E.salary DESC
1
 
_agx_Connect With a Mentor Commented:
If I'm understanding correctly, I think you just need an OUTER JOIN.
SELECT E.sin
	, E.lastName
	, E.salary
	, b.branchName
FROM   Employee E LEFT JOIN Branch B ON B.managerSIN = E.sin 
WHERE  E.salary > 80000
ORDER BY E.salary DESC

Open in new window

1
 
Bob TianAuthor Commented:
The problem is that I am not allowed to use any join operations, so an outer join would not be possible
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
_agx_Commented:
I am not allowed to use any join operations

Why not? You're already using a JOIN in the first query of the UNION...
1
 
_agx_Connect With a Mentor Commented:
FROM Employee E, Branch B
  WHERE B.managerSIN = E.sin

That's a JOIN, albeit using the old ANSI-89 syntax, which is strongly discouraged.
1
 
_agx_Commented:
This is not possible without some sort of JOIN.
1
 
Pawan KumarConnect With a Mentor Database ExpertCommented:
Try this , It is possible with out join keyword.

SELECT E.sin
      , E.lastName
      , E.salary
      , b.branchName
FROM   Employee E , Branch B
WHERE  B.managerSIN = E.sin AND E.salary > 80000
ORDER BY E.salary DESC
1
 
_agx_Commented:
@Pawan - That excludes any records where the managersin is null, due to the inner join.
1
 
_agx_Commented:
No It will do the CROSS JOIN.

I meant your first pass. Though the second is still a little off - missing an extra salary filter.  

I would not recommend using ANSI-89 syntax. Not without a very good reason. That style is essentially deprecated, and is less clear IMO. However the sql is written, you still need some sort of JOIN.  May as well do it the correct right way and use SQL-92+ syntax, ie OUTER JOIN :-)
1
 
Pawan KumarDatabase ExpertCommented:
Yes sir you are correct. Its an old syntax and should be avoided. :)

I never use it. Its just for the question.

Regards,
Pawan
1
 
Pawan KumarDatabase ExpertCommented:
@Bob Tian - Please check if my suggestion works for you ?

Regards,
Pawan
1
 
Bob TianAuthor Commented:
Hello,

I have managed to get this working, thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.