SQL面试积累
2015-09-05 22:02
453 查看
以下题目都在MySQL上测试可行,有疏漏或有更优化的解决方法的话欢迎大家提出,我会持续更新的:)
有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录,写一段SQL语句,检索出每个学生缺考的科目。
A 学生表(student)
字段1 学生号(s_id)
字段2 学生名(s_name)
B 科目表(course)
字段1 科目号(c_id)
字段2 科目名(c_name)
C 成绩表(grade)
字段1 成绩号(g_id)
字段2 学生号(s_id)
字段3 科目号(c_id)
字段4 成绩(score)
select * from student join course left join grade on student.s_id=grade.s_id and course.c_id=grade.c_id where grade.score is null;
有如下表:
日期(rstime) 结果(result)
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果,该如何写sql语句?
日期 胜 负
2005-05-09 2 2
2005-05-10 1 2
select rstime,sum(case result when '胜' then 1 else 0 end)as 胜,
sum(case result when '负' then 1 else 0 end)as 负 from result group by rstime;
用一条SQL语句,查询出成绩表(grade)每门课都大于80分的学生姓名
name course score
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
select distinct name from grade where name not in (select distinct name from grade where score<=80)
原表:
courseid coursename score
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80
为了方便阅读,查询此表后的显示结果如下(及格分数为60分):
courseid coursename score mark
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass
select *,case when score>=60 then 'pass' else 'fail' end as 'mark' from temp;
学生表(stu),如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同,其他字段都相同的学生冗余信息。
create table temp as select 自动编号 from stu group by 学号,姓名,课程编号,课程名称,分数;
delete from stu where 自动编号 not in (select 自动编号 from temp);
学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联:(SQL)
1)写出建表语句;
2)写出SQL语句,查询选修了所有选修课程的学生;
3)写出SQL语句,查询选修了至少5门以上的课程的学生。
1、建表语句
S表:create table s(
id int not null primary key,
name varchar(20)
);
C表:create table c(
id int not null primary key,
cname varchar(20)
);
SC表:create table sc(
sid int not null,
cid int not null,
foreign key(sid) references s(id),
foreign key(cid) references c(id)
);
2、写出SQL语句,查询选修了所有选修课程的学生
select stu.id,stu.name from s where (select count(sid) from sc where sid = stu.id) = (select.count(id) from c);
3、写出SQL语句,查询选修了所有选修课程的学生
select stu.id,stu.name from s where (select count(sid) from sc where sid = stu.id)>=5;
有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录,写一段SQL语句,检索出每个学生缺考的科目。
A 学生表(student)
字段1 学生号(s_id)
字段2 学生名(s_name)
B 科目表(course)
字段1 科目号(c_id)
字段2 科目名(c_name)
C 成绩表(grade)
字段1 成绩号(g_id)
字段2 学生号(s_id)
字段3 科目号(c_id)
字段4 成绩(score)
select * from student join course left join grade on student.s_id=grade.s_id and course.c_id=grade.c_id where grade.score is null;
有如下表:
日期(rstime) 结果(result)
2005-05-09 胜
2005-05-09 胜
2005-05-09 负
2005-05-09 负
2005-05-10 胜
2005-05-10 负
2005-05-10 负
如果要生成下列结果,该如何写sql语句?
日期 胜 负
2005-05-09 2 2
2005-05-10 1 2
select rstime,sum(case result when '胜' then 1 else 0 end)as 胜,
sum(case result when '负' then 1 else 0 end)as 负 from result group by rstime;
用一条SQL语句,查询出成绩表(grade)每门课都大于80分的学生姓名
name course score
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
select distinct name from grade where name not in (select distinct name from grade where score<=80)
原表:
courseid coursename score
1 java 70
2 oracle 90
3 xml 40
4 jsp 30
5 servlet 80
为了方便阅读,查询此表后的显示结果如下(及格分数为60分):
courseid coursename score mark
1 java 70 pass
2 oracle 90 pass
3 xml 40 fail
4 jsp 30 fail
5 servlet 80 pass
select *,case when score>=60 then 'pass' else 'fail' end as 'mark' from temp;
学生表(stu),如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同,其他字段都相同的学生冗余信息。
create table temp as select 自动编号 from stu group by 学号,姓名,课程编号,课程名称,分数;
delete from stu where 自动编号 not in (select 自动编号 from temp);
学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联:(SQL)
1)写出建表语句;
2)写出SQL语句,查询选修了所有选修课程的学生;
3)写出SQL语句,查询选修了至少5门以上的课程的学生。
1、建表语句
S表:create table s(
id int not null primary key,
name varchar(20)
);
C表:create table c(
id int not null primary key,
cname varchar(20)
);
SC表:create table sc(
sid int not null,
cid int not null,
foreign key(sid) references s(id),
foreign key(cid) references c(id)
);
2、写出SQL语句,查询选修了所有选修课程的学生
select stu.id,stu.name from s where (select count(sid) from sc where sid = stu.id) = (select.count(id) from c);
3、写出SQL语句,查询选修了所有选修课程的学生
select stu.id,stu.name from s where (select count(sid) from sc where sid = stu.id)>=5;
相关文章推荐
- 黑马程序员-Java基础:集合(Collection)
- 数组中只出现1次的两个数字(面试题)
- 常见的链表面试题大汇总:
- 管理层必学!刘备如何面试诸葛亮?
- Java面试题之一---------字符串截取(字节分配)(编码)
- 黑马程序员之IO字符流及缓冲器
- 找到一个重复元素 - 面试题
- 黑马程序员——Java中的面向对象
- 黑马程序员之Collection类
- 黑马程序员之Map集合以及Collections静态方法
- BAT面试
- 临睡前的十分钟,决定未来职场的高度
- 程序员的十个层次
- 说我装13?过来,打屎你!(揭秘程序员装13面具)
- 程序员有趣的面试智力题
- 黑马程序员-Java基础:常用API
- 腾讯面试(三)
- 【年度总结】一个程序员的自我修养
- 黑马程序员--IO流(操作对象、管道流、RandomAccessFile)
- [翻译]程序员需要掌握的6项相关技能