SQL语法考核
SQL语法多变,不敢保证唯一,也不敢保证全对,如果错误欢迎指出,即刻修改。
一、现有表结构如下图
TABLENAME:afinfo
|
Id |
name |
age |
birth |
sex |
memo |
|
1 |
徐洪国 |
37 |
1979-03-23 |
男 |
高中 |
|
2 |
王芳 |
26 |
1988-02-06 |
女 |
本科 |
|
3 |
李达康 |
24 |
1990-04-02 |
男 |
硕士 |
|
4 |
侯亮平 |
30 |
1984-09-12 |
女 |
博士 |
|
5 |
徐夫子 |
27 |
1987-12-30 |
男 |
大专 |
|
6 |
…… |
…… |
…… |
…… |
…… |
1)请编写sql语句对年龄进行升序排列
MySQL> select * from afinfo
-> order by birth;
2)请编写sql语句查询对“徐”姓开头的人员名单
mysql> select * from afinfo
-> where name like '徐%';
3)请编写sql语句修改“李达康”的年龄为“45”
mysql> update afinfo
-> set age=45
-> where name='李达康';
4)请编写sql删除王芳这表数据记录。
mysql> delete from afinfo
-> where name='王芳';
二、现有以下学生表和考试信息表
学生信息表(student)
|
姓名name |
学号code |
|
张三 |
001 |
|
李四 |
002 |
|
马五 |
003 |
|
甲六 |
004 |
考试信息表(exam)
|
学号code |
学科subject |
成绩score |
|
001 |
数学 |
80 |
|
002 |
数学 |
75 |
|
001 |
语文 |
90 |
|
002 |
语文 |
80 |
|
001 |
英语 |
90 |
|
002 |
英语 |
85 |
|
003 |
英语 |
80 |
|
004 |
英语 |
70 |
1)查询出所有学生信息,SQL怎么编写?
mysql> select * from student;
2)新学生小明,学号为005,需要将信息写入学生信息表,SQL语句怎么编写?
mysql> insert into student values('小明','005');
3)李四语文成绩被登记错误,成绩实际为85分,更新到考试信息表中,SQL语句怎么编写?
mysql> update exam,student
-> set exam.score=85
-> where student.code=exam.code
-> and student.name='李四'
-> and exam.subject='语文';
4)查询出各科成绩的平均成绩,显示字段为:学科、平均分,SQL怎么编写?
mysql> select subject 学科,avg(score) 平均分
-> from exam
-> group by subject;
5)查询出所有学生各科成绩,显示字段为:姓名、学号、学科、成绩,并以学号与学科排序,没有成绩的学生也需要列出,SQL怎么编写?
mysql> select s.name 姓名,s.code 学号,e.subject 学科,e.score 成绩
-> from student s
-> left join exam e
-> on s.code=e.code
-> order by 学号,学科;
6)查询出单科成绩最高的,显示字段为:姓名、学号、学科、成绩,SQL怎么编写?
mysql> select s.name 姓名,s.code 学号,
-> sum(if(e.subject='语文',e.score,0)) 语文成绩,
-> sum(if(e.subject='数学',e.score,0)) 数学成绩,
-> sum(if(e.subject='英语',e.score,0)) 英语成绩
-> from student s
-> left join exam e
-> on s.code=e.code
-> group by s.name,s.code;
表结构:
student(s_no,s_name,s_age,sex) 学生表
teacher(t_no,t_name) 教师表
course(c_no,c_name,t_no) 课程表
sc(s_no,c_no,score) 成绩表
基础表数据(个人铺的):根据题目需要自行再铺入数据
mysql> select s_no,avg(score)
-> from sc
-> group by s_no
-> having avg(score)>60;
3、查询所有同学的学号、姓名、选课数、总成绩。
mysql> select student.s_no,student.s_name,count(sc.c_no),sum(sc.score)
-> from student
-> left join sc
-> on student.s_no=sc.s_no
-> group by student.s_no,student.s_name;
4、查询姓李的老师的个数。
mysql> select count(*)
-> from teacher
-> where t_name like '李%';
5、查询没学过“叶平”老师课的同学的学号、姓名
mysql> select student.s_no,student.s_name
-> from student
-> join sc
-> on sc.s_no=student.s_no
-> where sc.score<60;
10、查询没有学全所有课的同学的学号、姓名。
mysql> set @ye_c_no=(select c_no from course,teacher where course.t_no=teacher.t_no and t_name=’叶平’);
mysql> delete from sc
-> where c_no=@ye_c_no;
16、向sc表中插入一些记录,这些记录要求符合一下条件:没有上过编号“003”课程的同学学号
mysql> select distinct s_no from sc
-> where c_no not in (select c_no from sc where c_no='003')
-> and s_no not in (select s_no from sc where c_no='003');
17、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分。
mysql> select c_no 课程ID,max(score) 最高分,min(score) 最低分
-> from sc
-> group by c_no;
