SQL - 使用 UNION 进行连接 ??使用 JOIN 进行联合?
我在一次采访中被问到这个问题。 可以使用 UNION 关键字进行 JOIN 吗? 可以使用 JOIN 关键字进行 UNION 吗?
那是 - 1. 我应该在不使用 JOIN 关键字但使用 UNION 关键字的情况下获得与 JOIN 相同的输出? 2. 我应该在不使用 UNION 关键字但使用 JOIN 关键字的情况下获得与 UNION 相同的输出?
如果可能的话,你能给我一个如何做到这一点的例子吗?
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 get 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
面试是你展示自己作品的框架。记住:不要回答问题;)
想想新闻发布会:发言人并不打算回答记者提出的难题来让自己陷入困境。相反,他们正在寻找他们已经有了答案的问题,即他们想要发布的信息(仅此而已!)
如果我在面试中遇到这个问题,我会用它来展示我的关系代数知识,因为这就是我本来是带着这样做的意图去接受采访的;我对“在这里谈论关系代数”问题保持警惕,就是这样。
宽松地说,JOIN 相当于逻辑 AND,而 UNION 相当于逻辑 OR。因此,使用约定逻辑的类似问题可能是“你能使用 OR 进行 AND 操作吗?”和“你能用 AND 做 OR 吗?”答案将取决于您还可以使用什么,例如 NOT 可能会派上用场;)
我也很想讨论原始运算符集、计算完整性所需的运算符集以及运算符和速记集之间的差异出于实际目的所需。
试图直接回答这个问题会引发进一步的问题。 JOIN 在关系代数中意味着“自然连接”,而在 SQL 中它意味着
INNER JOIN
。如果问题具体与 SQL 相关,您是否必须回答所有JOIN
类型?UNION JOIN
怎么样?举一个例子,SQL 的外连接是著名的
UNION
。克里斯·戴特 (Chris Date) 的表达比我想象的要好:SQL 和关系理论,第一版,作者:CJ Date
如果您想在采访中表达“我讨厌空值”,这将是一个很好的讨论点!
这些只是浮现在脑海中的一些想法。关键的一点是,通过问这些问题,面试官向你提供了一个分支。你会挂什么? ;)
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 theJOIN
types? What aboutUNION JOIN
?To employ one example, SQL's outer join is famously a
UNION
. Chris Date expresses it better than I could ever hope to: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? ;)
由于这是一个面试问题,他们正在测试您对这两个功能的理解。
他们期望的可能答案是“通常不,你不能这样做,因为他们执行不同的操作”,并且你可以通过声明联合将行附加到结果集的末尾,其中作为联接添加更多列来更详细地解释这一点。
进行 Join 和 Union 工作的唯一方法是,行仅包含来自两个源之一的数据:
与以下内容相同:
或者仅使用类型匹配的一列执行此操作: 与以下情况
相同:
一种情况如果您在多个表上生成了数据,但您希望同时查看所有数据,那么您会这样做,但是我建议在这种情况下使用 UNION 而不是 FULL OUTER JOIN,因为查询正在执行您本来会执行的操作预计。
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:
Is the same as:
Or to do this with only one column where the types match:
Is the same as:
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.
你的意思是这样的吗?
(在 SQL Server 2008 上编写)
当两个
SELECT
语句具有相同的列数并且这些列具有相同的列数(或在最不相似的)数据类型。UNION
不关心两个SELECT
语句是否仅从单个表中选择数据,或者其中一个或两个已经是JOIN
不止一张桌子。Do you mean something like this?
(written on SQL Server 2008)
UNION
works when bothSELECT
statements have the same number of columns, AND the columns have the same (or at least similar) data types.UNION
doesn't care if bothSELECT
statements select data only from a single table, or if one or both of them are alreadyJOIN
s on more than one table.我认为这还取决于其他可用的操作。
如果我没记错的话,
UNION
可以使用FULL OUTER
连接来完成:I think it also depends on other operations available.
If I remember well,
UNION
can be done using aFULL OUTER
join: