当前位置:网站首页>SQL Server 2016 learning record - nested query
SQL Server 2016 learning record - nested query
2022-07-28 10:36:00 【Hard work Zhang Zhang】
Learning goals :
One 、 The basic concept of nested query
Two 、 Classification of nested queries ( Predicate classification )
1、 with IN Subquery of predicate
2、 Subqueries with comparison operators
3、 with ANY or ALL Subquery of predicate
4、 with EXISTS Subquery of predicate
Learning content :
One 、 The basic concept of nested query
Query block : One select The statement becomes a query block
nested queries : Nesting a query block in the... Of another query block where Clause or having Query in the condition of phrase 

Restrictions on subqueries :
Two 、 Classification of nested queries ( Predicate classification )
1、 with IN Subquery of predicate
Between parent query and child query IN Connect , The subquery result is a collection
-- Query and “ Liu Chen ” Students in the same department
--1. determine “ Liu Chen ” Family name
--2. Find all in IS Students in the Department
select sno,sname,sdept from student
where sdept In
(select sdept from student
where sname=' Liu Chen ');

Use self connection to realize the above question :
select s1.sno,s1.sname,s1.sdept from student s1,student s2
where s1.sdept=s2.sdept and s2.sname=' Liu Chen ';
-- Query the elective course named “ The information system ” Student ID and name of
select sno,sname from student
where sno In
(select sno from sc
where cno In
(select cno from course
where cname=' The information system ')
);

2、 Subqueries with comparison operators
Scope of application : When you know exactly that the inner query returns a single value , Available comparison operators and ANY or ALL Predicates are used in conjunction with
-- Find out the course number that each student has exceeded the average score of his elective course
select sno,cno from sc x
where grade >
(select avg(grade) from sc y
where y.sno=x.sno);
-- perhaps The inner layer can not be aliased
select sno,cno from sc x
where grade >
(select avg(grade) from sc
where sno=x.sno);

The subquery must follow the comparison character
3、 with ANY or ALL Subquery of predicate
ANY Any value
ALL All worth
-- Query the ratio of other systems IS Student number and name of all students in the Department who are older
select sname,sage from student
where sage > ALL
(select sage from student
where sdept='IS');

4、 with EXISTS Subquery of predicate
with EXISTS The subquery of the predicate does not return any actual data , He only produces logical truth ‘true’, Or a logical false value ‘false’
-- Query all non elective courses 1 Student name of course No
select * from student s;
select * from sc;
select sname from student s
where not exists
(select * from sc
where s.sno=sno and cno=1);

-- Check the names of the students who have taken all the courses
select sname from student s
where not exists
(select * from course c
where not exists
(select * from sc
where s.sno=sno and c.cno=cno));
边栏推荐
- 2. Output one of the repeated numbers in the array
- Idea packages jar packages and runs jar package commands
- 10. The penultimate node in the linked list
- 多线程与高并发(三)—— 源码解析 AQS 原理
- 15. Judge whether the target value exists in the two-dimensional array
- Inside story of Wu xiongang being dismissed by arm: did the establishment of a private investment company harm the interests of shareholders?
- (1) Summary of machine learning concepts
- 13. Hash table - the first common node of two linked lists
- 8. Numbers that appear more than half of the time in the array
- uni-app项目目录、文件作用介绍 及 开发规范
猜你喜欢

机器学习--手写英文字母1--分类流程

15. Judge whether the target value exists in the two-dimensional array

SQL Server 2016 learning records - single table query

Aqua Data Studio 18.5.0 export insert statement

【微信小程序】项目实战—抽签应用

Excel word 简单 技巧 整理(持续更新 大概~)

7、MapReduce自定义排序实现

数据库安全 --- 创建登录名 用户+配置权限【笔记】

多线程与高并发(三)—— 源码解析 AQS 原理

机器学习--手写英文字母2--导入与处理数据
随机推荐
Context values traps and how to avoid or mitigate these traps in go
ZTE: 5nm 5g base station chip is being introduced!
ACM寒假集训#7
6. Double pointer -- the sum of the two numbers of the incremental array is equal to the target number
ACM winter vacation training 7
uni-app项目目录、文件作用介绍 及 开发规范
配置树莓派,过程和遇到问题
brief introduction
吴雄昂遭Arm罢免内幕:建私人投资公司,损害了股东利益?
Uni app project directory, file function introduction and development specification
胡润发布2020中国芯片设计10强民营企业:华为海思竟然没有上榜!
机器学习--手写英文字母1--分类流程
阿里云镜像地址
gcc: error trying to exec 'as': execvp: No such file or directory
2019年9月PAT甲级题目
在mt6735中添加新的开机logo与开\关机动画
Read write separation standby backup error
Chapter 1: cross end development of small programs of uniapp ----- create a uniapp project
Summary of key points of bank entry examination
Database security - create login user + configure permissions [notes]