加盟运营:
联接操作是一种二元操作,用于根据它们之间的公共字段组合来自两个或多个表的数据或行。 INNER JOIN、LEFT JOIN、RIGHT JOIN、FULL JOIN 是不同类型的连接。
例子 –
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);
现在,我们必须找到在 London Location 工作的员工姓名和部门名称。在这里,我们必须显示员工表中的 eName 和部门表中的 dName。因此我们必须使用Join Operation。
SELECT e.eName, d.dName from Employee e, Department d where e.dId=d.dId and d.dLocation="London";
- 当我们想从一个表中获取数据并且条件基于另一个表时,我们可以使用 Join 或 Subquery。现在,我们必须找到在 London Location 工作的员工姓名。
在这里,我们必须只显示员工表中的 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");
了解了Join 和Subqueries 的基本区别之后,现在我们将了解Nested Subquery、Correlated Subquery 和Join Operation 的区别。
嵌套查询、关联查询和连接操作的区别:
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. |