(1) set membership
Note denote the set of parentheses:
枚举集合
select * from s where sname in ('张军','王红');
子查询的查询结果,注意(sno,cno)这种用法,子查询中不用这么写
查询哪些同学没有选择哪些课程
select sno,cno
from s,c
where (sno,cno) not in (select sno,cno from sc);
Note: ① query result is a set of tuples when not a single finger, here in need rather than == comparison.
② enumerated set with (), such as (sno, cno), is a collection of sub-query results.
(2) sub-queries
① uncorrelated subquery
Subquery is not dependent on the parent query, execution starts sequentially from the innermost layer, from inside to outside.
--成绩在70分以上的学生信息
select sno, sname from studen
where sno in
(select sno from garde where score=70)
② correlated subquery
Subquery dependent parent query, from the outer first come first tuple (a tuple out) , and an inner layer based on its relevant inner query attribute value processing, it is true if the region where the tuple into the result set, remove a turn.
--查询选修了c1号课程的学生的学号和姓名。
select sno,sname from s
where exists
(select * from sc
where cno = 'c1'
and sno = s.sno)
The difference is that two different scopes tuples of variables.
(3) Comparative (symbol preceding clause, written) between the set
You know exactly when the subquery result is a single value, certain sub-query comparison operator followed.
SELECT sno,sname,dno FROM S
WHERE dno =
(SELECT dno
FROM S
WHERE sname= ‘ 刘晨’);
(4) some (subquery) and all (subquery)
查询比d1系某个学生年龄小的学生信息
SELECT sname,age FROM S
WHERE age < some (SELECT age FROM S WHERE dno= ' d1’)
AND dno <> ' d1' ;
查询比d1系任意学生年龄小的学生信息
SELECT sname,age FROM S
WHERE age < all(SELECT age FROM S WHERE dno= ' d1’)
AND dno <> ' d1' ;
或者
SELECT sname,age FROM S
WHERE age < (SELECT max(age) FROM S WHERE dno= ' d1’)
AND dno <> ' d1' ;
some, all functions may be used instead of the aggregate (4) the presence of predicate
找出平均成绩最高的学生学号
select sno from SC
group by sno having avg(score) >= all(select avg(score)
from SC
group by sno)
(5) exists (exist)
①exists basic usage
After the child is not relevant in the query, exist is correlated subquery .
It can be seen from the result of the determination exists a sub-set of the query whether there tuple , and do not care what tuples exist. Traditionally considered a target column expression often used *, because the subquery with EXISTS only concerned about whether there is a tuple, column names are given no practical significance. But * will bring query performance issues, recommended constant (select 1 column is 1, how many rows there are that many 1) after 1 exists with no brain .
By this question look at the tuple variable scope, sub-query sno refers sc in sno.
② represents all with exists
I chose to identify all classes of students, all of the students chose courses, equivalent to the absence of a course the student is not selected.
select sname from s
where not exists
(select cno from c where not exists
(select 1 from sc where sc.sno = s.sno and sc.cno = c.cno))
从所有的课程表c中选出一门课,这门课没有出现在sname的选课表上,以上情况不会发生即sname把课都选了。
Query the name of the student election at least a full course of study the student chosen number is 002.
p represents the predicate: 002 students enrolled in the course y, q predicate X students enrolled in courses y.
After the above deduction, find all the elective courses students take in 002 students is equivalent to find a student is not present 002 enrolled in a course, but he had not even signed.
select scx.sno from sc scx where not exists(
选出一名学生X
select 1 from sc scy where scy.sno = '002' and not exists(
选出002选的课程scy.cno
select 1 from sc scz where scz.sno = scx.sno and scz.cno = scy.cno))
如果学生X没有选择课程K的话返回 true