【SQL笔记】sql 练习题

1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名

name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

 select distinct name from table where name not in (select distinct name from table where fenshu<=80)
select name from table group by name having min(fenshu)>80
2. 学生表 如下:

自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69
删除除了自动编号不同, 其他都相同的学生冗余信息

delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by学号, 姓名, 课程编号, 课程名称, 分数)
3.一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合.

你先按你自己的想法做一下,看结果有我的这个简单吗?

SELECT * FROM group_table g1 JOIN group_table g2 WHERE g1.name < g2.name
4 使用sql语句改变下表结构

原始数据结构
+------+-------+-------+
| name | stage | score |
+------+-------+-------+
| A | 基 | 1 |
| B | 基 | 2 |
| C | 基 | 3 |
| A | 爬 | 1 |
| B | 爬 | 3 |
| C | 爬 | 2 |
| A | SQL | 1 |
| B | SQL | 1 |
| C | SQL | 1 |
+------+-------+-------+

目标数据结构
+------+------+------+------+
| name | 基 | 爬 | SQL |
+------+------+------+------+
| A | 1 | 1 | 1 |
| B | 2 | 3 | 1 |
| C | 3 | 2 | 1 |
+------+------+------+------+

SELECT name, 
    MAX(CASE WHEN stage='基' THEN score ELSE null END) AS '基',
    MAX(CASE WHEN stage='爬' THEN score ELSE null END) AS '爬',
    MAX(CASE WHEN stage='SQL' THEN score ELSE null END) AS 'SQL'
 FROM scores GROUP BY name
5.建表

创建学生表 Student
字段包括 学号,姓名, 性别,出生年月日, 班级

create table Student(
  sno varchar(20) primary key,
  sname varchar(20) not null,
  ssex varchar(10) not null,
  sbirthday datetime,
  calss varchar(20)
)

创建教师表 teacher
字段包括 教师编号,教师姓名, 教师性别,出生年月日, 职称, 所在部门

create table teacher(
  tno varchar(20) primary key,
  tname varchar(20) not null,
  tsex varchar(10) not null,
  tbirthday datetime,
  prof varchar(20) not null,
  depart varchar(20) not null
)

创建课程表 Course
字段包括 课程编号,课程名称, 教师编号

create table Course(
  cno varchar(20) primary key,
  cname varchar(20) not null,
  tno varchar(10) not null,
  foreign key(tno) references teacher(tno)
)

创建成绩表 Score
字段包括 学号,课程号,成绩

create table Score(
  sno varchar(20) primary key,
  cno varchar(10) not null,
  degree decimal,
  foreign key(sno) references Student(sno),
  foreign key(cno) references Course(cno)
);

练习题:
1 查询score表中的最高分的学生号码,课程号码

select sno,cno
from Score 
where degree = (
select max(degree) from Score
);

2 查询score表中至少有两名学生选修的并以3为开头的课程的平均成绩

select cno, avg(degree) 
from Score 
where con like '3%' 
group by con 
having count(1) >= 2;

3 查询分数大于70 小于90 的学生号码

// ' between 70 and 90 介于之间
select sno
from Score 
where degree between 70 and 90 / where degree>70 and degree<90;

4 查询所有学生的 sname, cno 和 degree 列数据

select sname, cno, degree
from Student, Score
on Student.sno = Score.sno;

5 查询 “95031” 班学生每门课平均分

select cno, avg(degree) 
from Score 
where sno in(
  select sno
  from Student 
  where class="95031"
) group by cno;

6 查询和学号 108, 101 学生同年出生的学生信息

// year() : 获取年份函数
select *
from Student 
where year(sbirthday) in (
  select year(sbirthday)
  from Student 
  where sno in(108, 101)
);

7 查询“张旭” 老师任课的学生成绩

select * 
from Score where cno (
  select cno 
  from Course where tno = (
    select tno
    from teacher 
    where tname = "张旭"
  ) 
)

8 查询选修某课程的同学人数大于5的教师姓名

select tname
from teacher where tno in(
select tno
from Course where cno in (
  select cno 
  from Score group by cno having count(1) > 5
)
)

9 查询编号为 ‘3-105’ 课程 且成绩至少高于选修编号‘3-245’ 同学的 Cno,Sno, Degree 再以Degree降序排序

// any:至少一个 desc:降序
select Cno, Sno, Degree 
from Score 
where cno='3-105' and degree > any(
  select degree from Score where cno = '3-245'
) order by degree desc;

10 查询所有老师和学生的 name sex birthday

//求并集
select tname tsex tbirthday from teacher
union
select sname ssex sbirthday from Student

11


image.png
select sno, cno, grade 
from Score, grade 
where degree between low and upp

你可能感兴趣的:(【SQL笔记】sql 练习题)