📜  嵌套子查询,关联子查询和联接操作之间的区别

📅  最后修改于: 2021-08-27 06:56:07             🧑  作者: Mango

加盟运营:
连接操作是一种二进制操作,用于根据两个或多个表之间的公共字段合并数据或行。内联接,左联接,右联接,全联接是不同类型的联接。

例子 –

Orders (OrderID, CustomerID, OrderDate);
Customers (CustomerID, CustomerName, ContactName, Country);

查找已订购客户的详细信息。

SELECT * from Customers JOIN Orders 
ON Orders.CustomerID=Customers.CustomerID;

子查询
当一个查询包含在另一个查询中时,外部查询称为“主查询”,而内部查询称为“子查询”。

  • 嵌套查询–
    在嵌套查询中,内部查询首先运行,并且仅运行一次。外部查询是根据内部查询的结果执行的。因此,内部查询用于执行外部查询。
    例子 –
    Orders (OrderID, CustomerID, OrderDate);
    Customers (CustomerID, CustomerName, ContactName, Country);

    查找已订购客户的详细信息。

    SELECT * FROM Customers WHERE 
    CustomerID IN (SELECT CustomerID FROM Orders);
  • 相关查询–
    在“相关查询”中,首先执行外部查询,并为每个外部查询行执行内部查询。因此,内部查询使用外部查询中的值。
    例子 –
    Orders (OrderID, CustomerID, OrderDate);
    Customers (CustomerID, CustomerName, ContactName, Country);

    查找已订购客户的详细信息。

    SELECT * FROM Customers where 
    EXISTS (SELECT CustomerID FROM Orders 
    WHERE Orders.CustomerID=Customers.CustomerID);

连接操作和子查询的应用:
首先,要了解嵌套子查询,关联子查询和联接操作之间的区别,我们必须了解在哪里使用子查询以及在哪里使用联接。

  • 当我们想从多个表中获取数据时,我们使用联接操作。
    示例:让我们考虑以下两种关系:
    Employee (eId, eName, eSalary, dId);
    Department (dId, dName, dLocation);

    现在,我们必须找到在伦敦办事处工作的员工姓名和部门名称。在这里,我们必须显示雇员表中的eName和部门表中的dName。因此,我们必须使用联接操作。

    SELECT e.eName, d.dName from Employee e, 
    Department d 
    where e.dId=d.dId and d.dLocation="London";
  • 当我们想从一个表中获取数据并且条件基于另一张表时,我们可以使用Join或Subquery。现在,我们必须找到在伦敦办事处工作的员工姓名。

    在这里,我们只需要显示employee表中的eName,因此我们可以使用Join Operation或Subquery
    使用联接操作–

    SELECT e.eName from Employee e, Department d 
    where e.dId=d.dId and d.dLocation="London";

    使用子查询–

    SELECT eName from Employee 
    where dId=(SELECT dId from Department where dLocation="London");

    了解联接和子查询之间的基本区别之后,现在我们将了解嵌套子查询,关联子查询和联接操作之间的区别。

嵌套查询,关联查询和联接操作之间的区别:

Parameters Nested Query Correlated Query Join Operation
Definition In Nested query, a query is written inside another query and the result of inner query is used in execution of outer query.  In Correlated query, a query is nested inside another query and inner query uses values from outer query.                     Join operation is used to combine data or rows from two or more tables based on a common field between them.INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN are different types of Joins.
Approach Bottom up approach i.e. Inner query runs first, and only once. Outer query is executed with result from Inner query. Top to Down Approach i.e. Outer query executes first and for every Outer query row Inner query is executed. It is basically cross product satisfying a condition.
Dependency Inner query execution is not dependent on Outer query. Inner query is dependent on Outer query. There is no Inner Query or Outer Query. Hence, no dependency is there.
Performance  Performs better than Correlated Query but is slower than Join Operation. Performs slower than both Nested Query and Join operations as for every outer query inner query is executed. By using joins we maximize the calculation burden on the database but  joins are better optimized by the server so the retrieval time of the query using joins will almost always be faster than that of a subquery.