以下来自此页面的Hacker Rank SQL(基本)认证,


块1
SELECT ei.employee_ID, ei.name
FROM employee_information AS ei, Last_quarter_bonus AS lqb
WHERE ei.division = 'HR'
AND lqb.bonus >= 5000;
块2
SELECT ei.employee_ID, ei.name
FROM employee_information AS ei, Last_quarter_bonus AS lqb
WHERE ei.division = 'HR'
AND lqb.bonus >= 5000
AND ei.employee_ID = lqb.employee_id;
这两个查询之间的唯一区别是最终线,第2行2:
和ei.employee_id = lqb.employee_id; ,
但我不知道为什么这条线是必要的。
代码的第一个块应在我眼中填充。
我不明白第5行的必要性是什么。
在我看来,第3行和第4行已经使第5行是正确的,而无需编写第5行。
那么为什么我必须编写第5行才能获取查询以按我想要的方式编译?
为什么这两个查询吐出不同的结果?
The following is from Hacker Rank SQL (Basic) Certification from this page, https://www.hackerrank.com/skills-verification


BLOCK 1
SELECT ei.employee_ID, ei.name
FROM employee_information AS ei, Last_quarter_bonus AS lqb
WHERE ei.division = 'HR'
AND lqb.bonus >= 5000;
BLOCK 2
SELECT ei.employee_ID, ei.name
FROM employee_information AS ei, Last_quarter_bonus AS lqb
WHERE ei.division = 'HR'
AND lqb.bonus >= 5000
AND ei.employee_ID = lqb.employee_id;
The only difference between these two queries is the final line, line 5 of BLOCK 2:
AND ei.employee_ID = lqb.employee_id;
But I don't know why that line is necessary.
The first block of code should compile fine in my eyes.
I don't understand what the necessity of line 5 is.
It seems to me that line 3 and line 4 already make line 5 true without having to write line 5.
So why do I have to write line 5 to get the query to compile the way I want it to?
Why do these two queries spit out different results?
发布评论
评论(3)
第五行是必要的。在问题中,它要求您查看两个表并编写一个语句,该语句将过滤输出以显示HR中的任何员工,并获得5000或更多的奖金。
除了语法偏好之外,
当您在第二块中进行此操作时,您要寻找三个条件,而第一块中的两个条件。您有:
以及
因为SQL是愚蠢的,所以您必须准确地告诉它您要它做什么。如果不关联两个表,它就无法关联两个搜索。
如果您在一个表中有所有这些信息,则可以在第1块中使用两个条件(无需加入)并获得您要寻找的结果,但是由于有两个表,您必须包括它。
现在,至于语法,当您声明一个加入时,您通常会在两个表之间分配哪个列。因此,如果您要用JOIN编写此语句,则最后一个条件将烤到JOIN命令中,而不是在底部(从数据库中选择 * AS DB1 JOIN DATABASE2 as db1上的DB2。这将使它看起来像这样:
当然,应该更简化代码的结构,但是我为声明中的每个动作制定了一条新的行,以便我可以更好地解释每个组件的作用。
It is necessary for the fifth line. In the question, it is asking you to look at both tables and write a statement that will filter the output to display any employees that are in HR and make a bonus of 5000 or more.
Syntax preference aside,
When you go about doing this in block two, you have three conditions you're looking for compared to two in block one. You have:
AND
Because SQL is dumb, you have to tell it exactly what you want it to do. It can't relate the two searches without relating the two tables.
If you had all this information in one table, you'd be able to use the two conditions in block 1(without the need of a join) and get the result you're looking for but since there are two tables, you have to include it.
Now as for syntax, when you declare a join you typically assign which column is shared among both tables. So if you were to write this statement with a join, the last condition would be baked into the join command rather than being at the bottom(SELECT * FROM database1 AS db1 JOIN database2 AS db2 ON db1.commonColumn=db2.commonColumn). This would make it look like this:
Ofcourse the structure of the code should be more streamlined but I made a new line for each action in the statement so I could better explain what each component does.
这是使用显式加入的块2查询。这是评论中提到的首选语法。
这里的条件在ON子句中指定。需要此条件仅包括
employee_information
和last_quarter_bounus
从employee_information
和afferee相同的情况下加入行。
Here is the BLOCK 2 query using an explicit JOIN. This is the preferred syntax as mentioned in the comments.
Here the condition is specified in the ON clause. This condition is needed to only include rows joined where the employee is the same from
employee_information
andlast_quarter_bounus
.该线表示加入条件。
查询涉及2个表,如您在“来自子句”中所见。桌子名称之间的逗号将导致交叉联接(在这种情况下,这可能并不那么明显,因为只有一个逗号)。
第二个查询使用内部连接,这也不是显而易见的,因为从子句看起来相同(表名,逗号 - > cross join)与第一个查询中一样。但是,条件ei.employee_id = lqb.employee_id强制执行内部联接,这将在结果集中为您提供更少的行。
为了使SQL代码变得更清晰,许多人建议加入... on ...语法内连接,然后交叉加入语法进行交叉加入。
等效:
对于内在的加入...
您可以看到,在内部联接的情况下,如果您仅在“来自子句”中使用表格(即表名和逗号),则该子句将在连接条件下变得混乱(您加入的桌子越多,这种效果就会越好)。
dbfiddle
This line represents the join condition.
The query involves 2 tables, as you can see in the FROM clause. The comma between the table names will cause a CROSS JOIN (which is maybe not so obvious in this case, as there is just a comma).
The second query uses an INNER JOIN, which is also not obvious, as the FROM clause looks the same (table names, comma -> cross join) as in the first query. However, the condition ei.employee_ID = lqb.employee_id enforces an inner join, which will give you less rows in the result set.
In order to make the SQL code clearer, many people recommend JOIN ... ON ... syntax for inner joins, and CROSS JOIN syntax for cross joins.
Equivalents:
and for inner joins...
You can see that in the case of the inner join, the WHERE clause will become cluttered with join conditions if you use just a list of tables (ie table name and commas) in the FROM clause (the more tables you join, the worse this effect becomes).
DBfiddle here.