分享一些简单的SQL查询面试题

分享一些简单的sql查询面试题

/*   
    3. 在studentinfo数据库中,用ddl进行下列查询操作  
*/  
/*--单表查询-- */  
--1)查询全体学生的学号、姓名、性别、出生日期及所在系。  
use studentinfo;  
select * from student;  
  
2)查询全体学生的选课情况,其成绩列值都加5,并为各列设置中文的别名。  
select sno '学号',cno '课程号',grade+5 '成绩'  
from sc;  
  
3)显示所有选课学生的学号,并去掉重复行。  
select distinct sno from student;  
  
4)查询学生选课成绩在80~90分之间的学生的学号、课程号、成绩。  
select * from sc where grade between 80 and 90;  
  
5)查找姓名以s开头的所有学生的学号、姓名。  
select sno,sname from student   
where sname like 's%';  
  
6)查询有考试成绩(即成绩不为空值)的学生的学号、课程号。  
select sno,cno from sc   
where grade is not null;  
  
7)查询计算机系(计科0701)年龄在18岁以上的学生的学号、姓名。  
select sno,sname from student   
where sage>18 and sdept='cs0701';  
  
--8)求选修课程超过2门课的学生学号、平均成绩和选课门数,并按平均成绩降序排列  
select sno,avg(grade) avggrade,count(cno) '选课门数'  
from sc  
group by sno having count(*)>=2  
order by avggrade desc;  
  
/*--查询及更新-- */  
  
--9)查询每个学生的学号、姓名、选修的课程名、成绩。(连接查询)  
select student.sno,sname,cname,grade from student,course,sc   
where student.sno=sc.sno and course.cno=sc.cno;  
  
--10)查询与sue在同一个系学习的所有学生的学号和姓名。(嵌套查询)  
select sno,sname from student   
where sdept in(  
    select sdept from student   
    where sname='sue'  
    );  
  
--11)查询选修了课程名为english的课程并且成绩大于80分的学生学号、姓名。(可改为嵌套查询)  
select student.sno,sname from student,course,sc   
where student.sno=sc.sno and course.cno=sc.cno and cname='english' and grade>80;  
  
/* 12)向student表中添加一个学生记录,学生学号为20010115,姓名为stefen ,性别为男,  
年龄为25岁,所在系为艺术系art。*/  
insert into student  
values('20010115','stefen','男',25,'艺术系art');  
  
--13)向sc表中添加一个学生的选课记录,学生学号为20010115,所选的课程号为c2。  
insert into sc(sno,cno)  
values('20010115','c2');  
  
--14)将姓名为sue的学生所在系改为计算机系。  
update student set sdept='计算机系'   
where sdept in(  
    select sdept from student   
    where sname='sue'  
    );  
      
--15)将选了课程名为database课程的学生成绩加10。  
update sc set grade=grade+10  
where cno in(  
    select cno from course   
    where cname='database'  
    );  
  
--16)删除所有成绩为空值的选修记录。  
delete from sc   
where grade is null;  
  
--17)删除计算机系选修成绩不及格的学生的选修记录。  
delete from sc   
where cno in(  
    select sc.cno from student,sc   
    where sdept='计算机' and grade<60 and student.sno=sc.sno  
    );  
(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐