Some tasks can be performed in two ways, both by joins and subqueries. Under what situations should we opt for subqueries?
Write your query using a subquery instead of a join when it is easier for you to understand what the purpose or intent of the query is. Remember, you not only have to write it, you may have to come back six months later and figure out what it's doing! I'll demonstrate with two examples:
Example 1
Do you prefer:
select a.foo, a.bar
from table1 a
inner
join table2 b
on a.id = b.id
and b.qux = '937'
group
by a.foo, a.bar
or:
select foo, bar
from table1
where id in
( select id
from table2
where qux = '937' )
Example 2
Do you prefer:
select a.foo, a.bar
from table1 a
left outer
join table2 b
on a.id = b.id
and b.qux = '937'
where b.id is null
or:
select foo, bar
from table1
where not exists
( select 1
from table2
where id = table1.id
and qux = '937' )
Ordinarily, when writing SQL, you should not be concerned with performance. Instead, you should focus all your intellect on ensuring that you get the correct results, and let the database optimizer figure out how to satisfy the query. So the first consideration is whether the join and subquery techniques actually do both produce the same correct results in either example.
The other consideration is maintainability. The first example returns rows from table1 with matching qualified rows in table2, while the second example returns only rows without qualified matches. Now consider how you would have to modify each query in both examples to add another condition, to return only those rows from table1 that also have a qualifying matching row in table3. You may discover that the join technique is severely limiting.
分享到:
相关推荐
Having useful indexes speeds up finding individual rows in a table, as well as finding the matching rows needed to join two tables. What You Will Learn After completing this lesson, you will be able ...
事务是指一系列的数据库操作,作为一个单元执行,Either all operations succeed or none of them do. MySQL 中可以使用 START TRANSACTION 语句来开启事务,使用 COMMIT 语句来提交事务,使用 ROLLBACK 语句来回滚...
WHERE table1.column_name IS NULL OR table2.column_name IS NULL; ``` **5. 交叉连接 (Cross Join):** - **定义:** 交叉连接(CROSS JOIN)返回所有可能的组合,即笛卡尔积。 - **语法示例:** ```sql ...
15. **子查询(Subquery)**:在其他SQL语句中嵌套的查询,用于返回单个值、行或行集,然后用作外部查询的一部分。 这些只是SQL语言的一部分基础知识,实际使用中还涉及到更复杂的操作,如视图、存储过程、触发器、...
HAVING COUNT(DISTINCT C.C#) = (SELECT COUNT(*) FROM (SELECT C# FROM SC WHERE S# = '001') AS SubQuery); ``` #### 13. 把“SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩 - **知识点**: 使用`...
除了基础的查询和数据操纵外,SQL还支持更高级的功能,如连接(JOIN)、子查询(Subquery)、视图(View)、存储过程(Stored Procedure)等,这些功能可以实现更复杂的数据分析和处理需求。 ##### JOIN 操作 JOIN...
12. 取第21-30条记录的SQL语句:可以使用ROWNUM和SUBQUERY来实现,例如:CREATE TABLE t(a NUMBER, ...); SELECT * FROM (SELECT a, b, c, d FROM t ORDER BY c) WHERE ROWNUM BETWEEN 21 AND 30;
最后,子查询(Subquery)是在一个查询中嵌套另一个查询,用于执行复杂的逻辑,如筛选满足特定条件的子集。 总的来说,这个教程覆盖了SQL的基础操作,是初学者学习SQL的良好起点。通过理解和实践这些语句,你可以...
这个实验涵盖了数据库查询的多个方面,包括CREATE VIEW语句、SELECT语句、WHERE子句、ORDER BY子句、DISTINCT关键字、BETWEEN运算符、LIKE运算符、NVL函数、AVG函数、MAX函数、SUBQUERY语句、WITH子句等。
从最优到最差的顺序为:SYSTEM > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL。其中,ALL表示全表扫描,是最不理想的。 5. **...
SUBQUERY(子查询)用于将一个查询的结果作为另一个查询的条件。例如,检索用户表中 username 为 '李老四' 的记录,并将其作为另外一个查询的条件: SELECT * FROM user WHERE username IN (SELECT username FROM ...
- 子查询(Subquery): SQL语句中包含的另一个完整的SELECT语句。 ### 题目三:获取每个部门平均薪资及高于平均薪资的员工 **题目描述**: 首先计算每个部门的平均薪资,然后找出那些薪资高于本部门平均薪资的员工...
5. **表格连接**:包括`JOIN`、`LEFT JOIN`(左连接)、`RIGHT JOIN`(右连接)、`FULL JOIN`(全连接)和`CROSS JOIN`(交叉连接),用于合并来自两个或更多表的数据。 6. **子查询**(Subquery):嵌套在其他SQL...
理解JOIN操作,包括内连接(INNER JOIN)、左连接(LEFT JOIN)、右连接(RIGHT JOIN)和全连接(FULL JOIN),以及如何使用子查询(Subquery)来获取更复杂的数据。 3. 数据类型:熟悉SQL支持的各种数据类型,如...
优化数据库还需要考虑索引的合理创建和维护,避免全表扫描,合理设计数据模型,减少JOIN操作,以及使用存储过程和视图等技术来提高效率。同时,定期分析和优化数据库的统计信息,以及根据业务需求调整内存配置,如...
4. **子查询或子集合映射(Subquery or Subcollection Mapping)**: 在映射文件中,可以使用子查询来获取关联数据,这样在原始查询中就包含了所需的信息,减少额外的查询。 5. **使用MyBatis的`<resultMap>`和`...
13. 子查询(Subquery):子查询是在另一个SQL查询内部嵌套的一个查询。它可以返回单个值、一组值或结果集,用于在WHERE子句或HAVING子句中进一步过滤数据。 14. UNION和UNION ALL运算符:UNION和UNION ALL运算符...
13. 连接(Joins):用于将两个或多个表根据共同的列连接起来,有内连接(INNER JOIN)、外连接(LEFT JOIN, RIGHT JOIN, FULL JOIN)等类型。 14. 子查询(Subquery):在另一个查询语句的WHERE子句中使用,有时也用于...
OR EXISTS ( SELECT 1 FROM SC WHERE SubQuery.SNO = SC.SNO AND SC.CNO = '课程2编号' AND SC.SCGRADE = MaxScore ); ``` 以上问题及解答覆盖了SQL基础查询的多个方面,包括简单的条件查询、连接查询、分组统计...
复合比较搜索条件(Compound Comparison Search Condition)通常使用AND和OR逻辑操作符来组合多个条件,以进一步缩小查询结果。范围搜索条件(Range Search Condition)用于限定特定范围内的值,如BETWEEN关键字。集...