[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

MIcrosoft SQL, query with ANY joins

Posted on 2016-11-03
13
Medium Priority
?
50 Views
Last Modified: 2016-11-04
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
0
Comment
Question by:Bob Tian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
13 Comments
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 1000 total points
ID: 41873191
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
 

Author Comment

by:Bob Tian
ID: 41873196
The problem is that I am not allowed to use any join operations, so an outer join would not be possible
0
 
LVL 52

Expert Comment

by:_agx_
ID: 41873199
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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 1000 total points
ID: 41873202
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
 
LVL 52

Expert Comment

by:_agx_
ID: 41873212
This is not possible without some sort of JOIN.
1
 
LVL 32

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 total points
ID: 41873357
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
 
LVL 52

Expert Comment

by:_agx_
ID: 41873717
@Pawan - That excludes any records where the managersin is null, due to the inner join.
1
 
LVL 32

Accepted Solution

by:
Pawan Kumar earned 1000 total points
ID: 41873720
@_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
 
LVL 52

Expert Comment

by:_agx_
ID: 41874196
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
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41874198
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
 
LVL 32

Expert Comment

by:Pawan Kumar
ID: 41874201
@Bob Tian - Please check if my suggestion works for you ?

Regards,
Pawan
1
 

Author Comment

by:Bob Tian
ID: 41874894
Hello,

I have managed to get this working, thanks for your help!
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question