sql语句查询计算机系,SQL查询语句基础
1.查詢全體學生的學號與姓名
select sno,sname
from student;
3.查詢全體學生的詳細信息
select *
from student;
4.查詢全體學生的姓名及其出生年份
select sname,2004-sage
from student;
5.查詢全體學生的姓名,出生年份和所在院系,要求用小寫字母表示所有系名
select sname,'year of birth:',2004-sage,lower(sdept)
from student;
6.查詢選修課程的學生學號,DISTINCT去掉重復行
select DISTINCT sno
from SC;
select sno
from SC;
等價于
select ALL sno
from SC;
7.查詢計算機科學系全體學生的名單
select sname
from student
where sdept='cs';
8.查詢所有年齡在20歲以下的學生姓名及其年齡
select sname,sage
from student
where sage<20;
9.查詢考試成績有不及格的學生的學號
select DISTINCT sno
from sc
where grade<60;
10.查詢年齡在20~23歲(包括20歲和23歲)之間的學生的姓名,系別和年齡
select sname,sdept,sage
from student
where sage between 20 and 23;
11查詢年齡不在20~23歲之間的學生的姓名,系別和年齡
select sname,sdept,sage
from student
where sage not between 20 and 23;
12.查詢計算機科學系cs,數學系ma和信息系is學生的姓名和性別
select sname,ssex
from student
where sdept in('cs','ma','is');
13.查詢不是計算機科學系cs,數學系ma和信息系is學生的姓名和性別
select sname,ssex
from student
where sdept not in('cs','ma','is');
14.查詢學號為200215121的學生的詳細情況
select *
from student
where sno like '200215121';
等價于
select *
from student
where sno='200215121';
15.查詢所有姓劉的學生的姓名,學號和性別
select sname,sno,ssex
from student
where sname like '劉%';
16.查詢所有不姓劉的學生的姓名,學號和性別
select sname,sno,ssex
from student
where sname not like '劉%';
17.查詢姓歐陽且全名為3個漢字的學生的姓名
select sname
from student
where sname like '歐陽_';
19.查詢DB_Design課程的課程號和學分
select cno,ccredit
from course
where cname like 'DB_Design'ESCAPE'';
ESCAPE''表示""為換碼字符。這樣匹配串中緊跟著""后面的字符"_"不再具有通配符的含義,轉義為普通的"_"字符
20.查詢以"DB_"開頭,且倒數第3個字符為i的課程的詳細情況
select *
from course
where cname like 'DB_%i__'ESCAPE'';
21.查詢缺少成績的學生的學號和相應的課程號
select sno,cno
from sc
where grade is null;
22.查詢計算機科學系年齡在20歲以下的學生姓名
select sname
from student
where sdept='cs' and sage<20;
23.查詢計算機科學系cs,數學系ma和信息系is學生的姓名和性別
select sname,ssex
from student
where sdept='cs' or sdept='ma' or sdept='is';
24.查詢選修了3號課程的學生的學號及其成績,查詢結果按分數的降序排序
select sno,grade
from sc
where cno='3'
order by grade DESC;
25.查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系的學生按年齡降序排列
select *
from student
order by sdept,sage DESC;
26.查詢學生總人數
select COUNT(*)
from student;
27.查詢選修了課程的學生人數
select COUNT(DISTINCT sno)
from sc;
28.計算1號課程的學生的平均成績
select AVG(grade)
from sc
where cno='1';
29.查詢選修1號課程的學生的最高分數
select MAX(grade)
from sc
where cno='1';
30.查詢學生200215012選修課程的總學分數
select SUM(ccredit)
from sc,course
where sno='200215012' and sc.cno=course.cno;
31.求各個課程號及相應的選課人數
select cno,count(sno)
from sc
group by cno;
32.查詢選修了3門以上課程的學生學號
select sno
from sc
group by sno
having count(*) >3;
33.查詢每個學生及其選課情況
select student.*,sc.*
from student,sc
where student.sno=sc.sno;
35.查詢每一門課的間接先修課
select first.cno=second.cpno
from course first,course second
where first.cpno =second.cno;
36.
select student.sno,sname,ssex,sage,sdept,cno,grade
from student left out join sc on (student.sno=sc.sno);
37.查詢選修2號課程且成績在90分以上的所有學生
select student.sno,sname
from student,sc
where student.sno=sc.sno and
sc.cno='2' and sc.grade>90;
38.查詢每個學生的學號,姓名,選修的課程名及成績
select student.sno,sname,cname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno;
39.查詢與劉晨在同一系學習的學生
select sno,sname,sdept
from student
where sdept in
(select sdept
from student
where sname='劉晨');
等同于
select s1.sno,s1.sname,s1.sdept
from student s1,student s2
where s1.sdept=s2.sdept and
s2.sname='劉晨';
40.查詢選修了課程名為“信息系統”的學生學號和姓名
select sno,sname
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course
where cname='信息系統'
)
);
等價于
select student.sno,sname
from student,sc,course
where student.sno=sc.sno and
sc.cno=course.cno and
course.cname='信息系統';
41.找出每個學生超過他選修課程平均成績的課程號
select sno,cno
from sc x
where grade>=(select AVG(grade)
from sc y
where y.sno=s.sno
);
42.查詢其他系中比計算機科學系某一學生年齡小的學生姓名和年齡
select sname,sage
from student
where sage
from student
where sdept='cs'
)
and sdept <> 'cs';
43.查詢其他系中比計算機科學系所有學生年齡都小的學生姓名及年齡
select sname,sage
from student
where sage < all
(select sage
from student
where sdept='cs'
)
and sdept <> 'cs';
44.查詢所有選修了1號課程的學生姓名
select sname
from student
where EXISTS
(select *
from sc
where sno=student.sno and cno='1');
使用存在量詞EXISTS后,若內層查詢結果非空,則外層的WHERE子句返回真值,否則返回假值
45.查詢沒有選修1號課程的學生姓名
select sname
from student
where not exists
(select *
from sc
where sno=student.sno and cno='1');
46.查詢選修了全部課程的學生姓名
select sname
from student
where not exists
(select *
from course
where not exists
(select *
from sc
where sno=student.sno
and cno=course.cno));
47.查詢至少選修了學生200215122選修的全部課程的學生號碼
select distinct sno
from sc scx
where not exists
(select *
from sc scy
where scy.sno='200215122' and
not exists
(select *
from sc scz where scz.sno=scx.sno and scz.cno=scy.cno));
48.查詢計算機科學系的學生及年齡不大于19歲的學生
select *
from student
where sdept='cs'
union
select *
from student
where sage<=19;
union會自動去掉重復元組,而union all操作符可以保存重復元組
49.查詢選修了課程1或者課程2的學生集合的并集
select sno
from sc
where cno='1'
union
select sno
from sc
where cno='2';
50.查詢計算機科學系的學生與年齡不大于19歲的學生的交集
select *
from student
where sdept='cs'
intersect
select *
from student
where sage<=19;
總結
以上是生活随笔為你收集整理的sql语句查询计算机系,SQL查询语句基础的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MATLAB在线网页链接使用
- 下一篇: 教你如何找到线程插入式木马