开发者

SQL - JOIN using UNION ?? UNION using JOIN?

I was asked this question during one of my interviews. Can you do JOIN using UNION keyword? Can you do UNION using JOIN keyword?

That is - 1. I should get same output as JOIN without using JOIN keyword, but using UNION Keyword? 2. I should ge开发者_如何学JAVAt same output as UNION without using UNION keyword, but using JOIN Keyword?

Can you give me an example of how to do this if possible?


An interview is the framework on which you set out your wares. Remember: don't answer questions ;)

Think of a press conference: the spokesperson is not looking to answer difficult questions from journos to catch themselves out. Rather, they are looking for questions to which they already have answers, being the information they want to release (and no more!)

If I faced this question in an interview, I would use it to demonstrate my knowledge of relational algebra because that's what I'd have gone into the interview with the intention of doing; I be alert for the "Talk about relational algebra here" question and this would be it.

Loosely speaking, JOIN is the counterpart of logical AND, whereas UNION is the counterpart of logical OR. Therefore, similar questions using convention logic could be, "Can you do AND using OR?" and "Can you do OR using AND?" The answer would depend on what else you could use e.g. NOT might come in handy ;)

I'd also be tempted to discuss the differences between the set of primitive operators, the set of operators necessary for computational completeness and the set of operators and shorthands required for practical purposes.

Trying to answer the question directly raises further questions. JOIN implies 'natural join' in relational algebra whereas in SQL it implies INNER JOIN. If the question specifically relates to SQL, do you have to answer for all the JOIN types? What about UNION JOIN?

To employ one example, SQL's outer join is famously a UNION. Chris Date expresses it better than I could ever hope to:

Outer join is expressly designed to produce nulls in its result and should therefore be avoided, in general. Relationally speaking, it's a kind of shotgun marriage: It forces tables into a kind of union—yes, I do mean union, not join—even when the tables in question fail to conform to the usual requirements for union (see Chapter 6). It does this, in effect, by padding one or both of the tables with nulls before doing the union, thereby making them conform to those usual requirements after all. But there's no reason why that padding shouldn't be done with proper values instead of nulls

SQL and Relational Theory, 1st Edition by C.J. Date

This would be a good discussion point if, "I hate nulls" is something you wanted to get across in the interview!

These are just a few thoughts that spring to mind. The crucial point is, by asking these questions the interviewer is offering you a branch. What will YOU hang on it? ;)


As this is an interview question, they are testing your understanding of both these functions.

The likely answer they are expecting is "generally no you cannot do this as they perform different actions", and you would explain this in more detail by stating that a union appends rows to the end of the result set where as a join adds further columns.

The only way you could have a Join and a Union work is where rows contain data from only one of the two sources:

SELECT A.AA, '' AS BB FROM A
UNION ALL 
SELECT '' AS AA, B.BB FROM B

Is the same as:

SELECT ISNULL(A.AA, '') AS AA, ISNULL(B.BB, '') AS BB FROM A
FULL OUTER JOIN B ON 1=0

Or to do this with only one column where the types match:

SELECT A.AA AS TT FROM A
UNION ALL 
SELECT B.BB AS TT FROM B

Is the same as:

SELECT ISNULL(A.AA, B.AA) AS TT
FROM A
FULL OUTER JOIN B ON 1=0

One case where you would do this is if you have data spawned over multiple tables but you want to see ti all together, however I would advise to use a UNION in this case rather than a FULL OUTER JOIN because of the query is doing what you would otherwise expect.


Do you mean something like this?

create table Test1 (TextField nvarchar(50), NumField int)
create table Test2 (NumField int)
create table Test3 (TextField nvarchar(50), NumField int)

insert into Test1 values ('test1a', 1)
insert into Test1 values ('test1b', 2)
insert into Test2 values (1)
insert into Test3 values ('test3a', 4)
insert into Test3 values ('test3b', 5)

select Test1.*
from Test1 inner join Test2 on Test1.NumField = Test2.NumField
union
select * from Test3

(written on SQL Server 2008)

UNION works when both SELECT statements have the same number of columns, AND the columns have the same (or at least similar) data types.
UNION doesn't care if both SELECT statements select data only from a single table, or if one or both of them are already JOINs on more than one table.


I think it also depends on other operations available.

If I remember well, UNION can be done using a FULL OUTER join:

Table a (x, y)

Table b (x, y) 

CREATE VIEW one
AS
SELECT a.x AS Lx
     , b.x AS Rx
     , a.y AS Ly
     , b.y AS Ry
FROM a FULL OUTER JOIN b
         ON  a.x = b.x
         AND a.y = b.y


CREATE VIEW unionTheHardWay
AS
SELECT COALESCE(Lx, Rx) AS x
     , COALESCE(Ly, Ry) AS y
FROM one
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜