当前位置:优学网  >  在线题库

这是什么样的SQL查询?

发表时间:2022-07-22 00:11:35 阅读:242

我理解子查询是什么(也称为内部查询/嵌套查询).

子查询,也称为嵌套查询或子选择,是嵌入在另一个SQL查询的WHERE或HAVING子句中的选择查询.

示例-

SELECT * FROM customers WHERE cust_id IN (SELECT DISTINCT cust_id FROM orders 
                      WHERE order_value > 5000);

我更关心的是理解以下查询的类型(名称)-

SELECT ta.col_a1, ta.col_a2, temp.col_tmp_a FROM table_a ta, (
    SELECT tb.col_b1, tb.col_b2, tc.col_c1 FROM table_b tb, table_c tc 
        WHERE tb.col_a1 = tc.col_c2 ) AS temp
    WHERE temp.col_b1 = ta.col_a1

如果我是对的,那么可以使用内部连接更好地编写上述查询,以获得更好的性能.但性能不是我关心的问题,我只想知道这种查询的名称.如果有人知道这个名字,请回答.

🎖️ 优质答案
  • it is still just called a Subquery, just instead as it being used to get specific clause value to filter the list, it is used effectivly as a table and allows you to select columns from the nested query just as you would from a table. Hope that answers your question.

  • It is called a derived table and below are the details

    Derived tables are the tables which are created on the fly with the help of the Select statement. Derived table expression appears in the FROM clause of a query. In derived table server create and populate the table in the memory, we can directly use it and we also don’t require to drop the table. But scope of derived table is limited to the outer Select query who created it. Derived table can’t be used further outside the scope of outer select query.

  • 这种类型的查询称为"表表达式",也称为"派生表"或"内联视图",具体取决于行话和数据库文档.它们代替查询中的表/视图.

    为了补充这个问题,我目前识别的子查询类型有:

    -标量子查询:在选择列表中替代标量的查询. -表表达式/派生表/内联视图:如上所述. -独立[递归]CTE:在主查询本身之前指定的查询定义. -依赖[Recursive]CTE:在主查询本身之前指定的查询定义,它依赖于另一个CTE. -非相关子查询:可以独立于查询的其余部分运行的子查询. -相关子查询:依赖于另一个表的值并需要相应执行的子查询. -横向子查询:位于表表达式相同位置的查询,但与之前的表相关.

    请参见https://stackoverflow.com/questions/50990469/how-many-types-of-sql-subqueries-are-there.

  • 相关问题