数据库基础练习题

练习1:

Manager(管理员表):
mid 编号 int (主键)
mname 名字 varchar(20)
age 年龄 int
sex 性别 char(2)
password 密码 varchar(20)
address 地址 varchar(20)
phone 电话 varchar(20)
数据:
1 王子 18 男 123 北京 110
2 公主 20 女 456 上海 220
3 太子 23 男 789 南京 330
(14)查询公主的所有信息
select * from manager where name = ‘公主’;
(15)查询年龄在18-30之间的管理员姓名
select mname from manager where age between 18 and 30;
(16)查询表中所有的用户名和电话
select mname,phone from manager;
(17)查询性别是男,名字是王子的个人信息
select * from manager where name = ‘王子’ and sex=‘男’
(18)查询出地址在北京和上海的员工信息
select * from manager where address = ‘北京’ or address = ‘上海’

练习2:

scores
stuid int 学生id
java int java成绩
mysql int mysql成绩
stuname varchar(20) 学生姓名
数据:
limit (pageindex-1)*pagesize,pagesize;
1 67 78 张三
2 87 55 李四
3 66 90 王五
4 98 78 赵六
5 80 88 田七
需求:
(1)对java成绩进行降序排序
select java from scores order by java desc
(2)得到mysql成绩前三名
select mysql from scores limt 0,3
(3)得到java学生中最后一名的学生信息
select * from scores order by java asc limt 0,1;
(4)查询出两门成绩都优秀(>=80)的学生姓名
select * from scores where java >= 80 and mysql >=80;
(5)查询出成绩在90分以上(>=90)的学生信息
select * from scores where java >= 90 or mysql >=90;
(6)查询出每名学员的java,mysql,总成绩
select java+mysql from scores
(7)显示出每名学生的总分以及姓名
select java+mysql,name from scores

练习3:

测试数据:
郭敬明 1371234567 北京 java S1101 89 1979-04-05
张三丰 1372839201 上海 数据库 S1102 67 1967-09-07
赵敏 1387839201 山东 mysql S1103 99 1987-09-07
Student2
stuname 姓名 varchar(20)
telphone 电话 varchar(20)
address 住址 varchar(20)
subject 科目 varchar(20)
stuNo 学号 varchar(20)
score 成绩 int
birthday 出生日期 date
//1.要查询列 2.条件
a.查询住址为“山东”的学生姓名、电话、住址
select stuname,telphone,address from student2 where address = ‘山东’
b.查询名称中含有“数据库”字样科目信息
select * from student2 where subject like ‘%数据库%’;
c.查询电话中以“1387”开头的学生信息
select * from student2 where telphone like ‘1387%’;
d.查询姓姜的,三个字的学生信息
select * from student2 where stuname like ‘姜__’;
e.查询学号为S1101的指定java,mysql科目考试成绩
select score from student2 where stuNo = ‘S1101’ and subject =‘java’ or subject = ‘mysql’;
f.查询出80后学员信息
select * from student2 where birthday>‘1979-12-31’;
g.查询出家庭住址在北上广的学生名字
select stuname from student2 where address in(‘北京’,‘上海’,‘广州’)
h.显示成绩在第5-10名的学生名字和电话
select * from student2 order by score limt 4,6;
i.查询分数在80-90之间并且在北京的学生
select * from student2 where address = ‘北京’ and score between 80 and 90

练习4:聚合函数练习

表:scores2
年级 grade varchar(10)
学号 stuno varchar(20)
考试时间 examDate date
科目 subject varchar(20)
成绩 score int
学期 xueqi int
数据:
S1 S1101 2015-02-03 C 89 1
S2 S1103 2015-03-03 JAVA 90 2
S3 S1102 2015-07-03 C 100 1
1.查询学生总人数
select count() from scores2;
2.学号为S1101的学生第一学期考试总成绩,平均分
select sum(score),avg(score) from scores2 where stuno = ‘S1101’;
3.查询2013年3月22日科目“C”的最高分、最低分、平均分
select max(score),min(score),avg(score) from scores2 where examDate = ’ 2013-3-22’ and subject = ‘C’;
4.查询2013年3月22日科目“C”及格学生的平均分
select avg(score) from scores2 where score>60;
5.查询所有参加“C”科目考试的平均分
select avg(score) from scores2 where subject = ‘C’;
6.查看考java的人数
select count(
) from scores2 where subject = ‘java’;

练习5:分组练习

表名:student
年级(grade) varchar(10)
学生姓名(name) varchar(10)
学时(xueshi) int --每人单个学时
参加考试(isexam) char(1) 是/否、
课程(subject) varchar(10)
分数(score) int
数据:
1 张三 10 是 java 99
1 李四 10 否 java 0
2 王五 20 是 mysql 88
2 赵六 20 是 mysql 77
2 王五 20 是 java 99
2 赵六 20 否 java 0
1 张三 10 是 mysql 88
练习:
a:查询每个年级的总学时数,并按照升序排列
select grade,sum(xueshi) from student group by grade order by asc;
b:查询每个参加考试的学员的平均分
select name,avg(score) from student group by name
c:查询每门课程的平均分,并按照降序排列
select subject,avg(score) from student group by subject order by desc;

练习6:综合练习

Student
科目名称 subjectName varchar(20)
学生姓名 stuname varchar(20)
学生地址 address varchar(20)
学生性别 sex char(2)
电子邮件 email varchar(30)
年级 grade varchar(10)
出生日期 birthday date
考试日期 examDate date
成绩 scores int
数据:
JAVA 张三 北京 男 123@qq.com S1 1990-03-04 2013-5-6 89
html 李四 上海 男 S2 1993-08-04 2014-5-6 87
html 王五 北京 男 123@qq.com S2 1990-03-04 2015-4-6 90
1.查询S2的科目名称
select subjectName from student where grade = ‘S2’;
2.查询S2男同学的姓名和住址
select stuname,address from student where grade = ‘S2’ and sex = ‘男’;
3.查询无电子邮件的学生姓名和年级信息
select stuname,grade from student where email is null and email = ‘’;
4.查询出生日期在1993年之后的S2的学生姓名和年级信息
select stuname,grade from student where birthdat>’ 1992-12-31’ and grade = ‘S2’;
5.查询参加了日期为2013年2月15日的“HTML” 科目考试的成绩信息
select scores form syudent where examDate = ‘2013-2-15’ and subject = ‘HTML’;

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值