目的:
掌握简单数据查询操作。
内容:
使用SQL语句完成各种查询条件的查询操作
步骤:
1、 以实验一中XSGL数据库为基础,对各表中的数据进行不同条件的查询;
包括的运算:投影、选择、比较运算符、逻辑运算符、字符匹配运算符、匹配列表范围、算术运算符、内部函数、排序、分组、分组函数使用
-
- 查询全体学生的学号和姓名
select sno,sname from student;
-
- 查询全体学生的详细记录
select * from student;
-
- 查询所有选修过课程的学生学号(不重复)
select DISTINCT sno from sc;
-
- 查询考试不及格的学生学号(不重复)
select distinct sno from sc where grade < 60;
-
- 查询sdept不为’CS’的学生性别、年龄、系别
select ssex,sage,sdept from student where sdept != 'CS';
-
- 查询年龄18-20岁的学生学号、姓名、系别、年龄;
select sno,sname,sdept,sage from student where sage in (18,19,20);
-
- 查询姓刘或姓李的学生情况
select * from student where sname like '刘%' or sname like '李%';
-
- 查询姓刘且名字为两个字的学生情况
select * from student where sname like '刘_';
-
- 查询1983年以后出生的学生姓名。
select sname from student where (2022-sage)>1983;
-
- 查询全体学生情况,查询结果按所在系升序排列,对同一系中的学 生按年龄降序排列。
select * from student order by sdept,sage desc;
-
- 查询学生总人数。
select count(*) from student;
-
- 查询选修了课程的学生人数。
select count(distinct sno) from sc;
-
- 查询选修了7号课程的学生总人数和平均成绩
select count(distinct sno),avg(grade) from sc where cno=7;
-
- 查找每门课的选修人数及平均成绩
select cno,count(cno),avg(grade) from sc group by cno;
-
- 查找没有先修课的课程情况
SELECT * FROM Course
WHERE course.cno NOT IN(SELECT cno FROM sc);
- 以实验一的MySPJ数据库为基础,用SQL语句完成如下查询:
(1)求供应工程J1零件的供应商号码SNO;
select distinct sno from spj where jno='J1';
(2)求供应工程J1零件P1的供应商号码SNO;
select distinct sno from spj where jno='J1' and pno='P1';
(3)求供应工程J1零件为红色的供应商号码SNO;
select DISTINCT sno from spj,p where spj.jno='J1' and spj.pno=p.pno and color='红';
(4)求没有使用天津供应商生产的红色零件的工程号JNO;
select distinct jno from spj where jno not in (select jno from spj,s,p where s.city='天津' and color = '红' and spj.sno=s.sno and spj.pno=p.pno);

831

被折叠的 条评论
为什么被折叠?



