数据库练习二

2021/5/15 19:25:43

本文主要是介绍数据库练习二,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

use Stu;
#(1)查询全体学生的学号和姓名。
select sno,sname from student;
#(2)查询全体学生的详细记录。
select * from student;
#(3)查询全体学生的姓名和出生年份。
select sname,2020-sage from student;
#(4)查询选修了课程的学生的学号。
select distinct sno from sc;
#(5)查询计算机系的学生的姓名。
select sname from student where sdept="计算机系";
#(6)查询年龄在20岁以下的计算机系的学生的学号和年龄。
select sno,sage from student where sdept="计算机系" and sage<20;
#(7)查询年龄在20—22岁的学生的学号。
select sno from student where sage between 20 and 22;
#(8)查询信息系、计算机系和外语系的学生的信息。
select * from student where sdept in('计算机系','信息系','外语系');
#(9)查询姓“王”的学生的信息。
select * from student where sname like '王%';
#(10)查询选修了3号课程的学生的学号及成绩,结果按分数的降序排列。
select sno,grade from  sc where cno='3' order by grade DESC;
#(11)查询学生总人数。
select count(distinct sno)from student;
#(12)查询选修1号课程的学生的最高分。
select MAX(grade) from sc where cno='1';
#(13)查询选修了3门以上课程的学生的学号。
select sno from sc group by sno having count(*)>3; 
#(14)查询每个学生及其选修课程的情况。
select student.*,sc.* from student,sc where student.sno=sc.sno;
#(15)查询选修2号课程且分数在90分以上的所有学生。
select student.sno,sname
from student,sc 
where student.sno=sc.sno
and sc.cno='2' and sc.grade>90;
#(16)查询每个学生的学号、姓名、选课名及成绩。
select student.sno,sname,sc.cname,grade
from student,sc,course
where student.sno=sc.sno and sc.cno=course.cno;
#(17) 查询与“张三”在同一系的学生。
select s1.sno,s1.sname,s1.sdept
from student s1,student s2
where s1.sdept=s2.sdept and s2.sname='张三';
#(18) 查询选修了课程名为“数据库”的学生学号和姓名。
select student.sno,sname 
from student,sc,course 
where student.sno=sc.sno and
sc.cno=course.cno and
course.cname='数据库';
#(19) 查询没有选修1号课程的学生的姓名。
select sname 
from student
where not exists
      (select *
       from sc
       where sc.sno=student.sno and cno='1');
#(20) 查询选修了所有课程的学生的姓名。       
select sname
from student
where not exists
      (select *
       from course
       where not exists
             (select *
              from sc
              where sno=student.sno
                 and cno=course.cno));

 



这篇关于数据库练习二的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程