sql表下载地址:https://download.csdn.net/download/weixin_46098676/21121902
-- 有一Blog系统使用的数据库是myblog,现在有几个要求:
/*
1、创建数据库myblog。
2、根据实际应用情况在myblog中创建以下两张表:帐号(account)、文章类别(category)。
3、帐号(account),包括:编号(account_id)、用户名(username)、
密码(password)、邮箱号(email)、性别(gender)、博客名(title)
4、通过alter table给此表添加两个字段:昵称(nickname)、头像(headimg)
5、文章类别表(category)编号(category_id)、帐户编号(account_id)、类别名(title)、
描述(description)、文章数(article_num)、可访问性(visible)、创建时间(created_time)
*/
--1.给出成绩全部合格的学生信息(包含姓名、课程、分数),注:分数在60以上评为合格
/*表名:成绩表
姓名 课程 分数
张三 语文 81
张三 数学 75
李四 语文 56
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 49
*/
--2)表名:商品表
/*名称 产地 进价
苹果 烟台 2.5
苹果 云南 1.9
苹果 四川 3
西瓜 江西 1.5
西瓜 北京 2.4
*/
--给出平均进价在2元以下的商品名称
--3)表名:高考信息表
/*准考证号 科目 成绩
2006001 语文 119
2006001 数学 108
2006002 物理 142
2006001 化学 136
2006001 物理 127
2006002 数学 149
2006002 英语 110
2006002 语文 105
2006001 英语 98
2006002 化学 129
*/
--给出高考总分在600以上的学生准考证号
--4)表名:team
/*
ID(number型) Name(varchar2型)
1 a
2 b
3 b
4 a
5 c
6 c
要求:执行一个删除语句,当Name列上有相同时,只保留ID这列上值小的
例如:删除后的结果应如下:
ID(number型) Name(varchar2型)
1 a
2 b
5 c
请写出SQL语句。
*/
--5)表名:student
/*name course score
张青 语文 72
王华 数学 72
张华 英语 81
张青 物理 67
李立 化学 98
张燕 物理 70
张青 化学 76
*/
--查询出“张”姓学生中平均成绩大于75分的学生信息
--6书表(books)
/*
book_id,book_name,creatdate,Lastmodifydate,decription
001,三个人的世界,2005-02-02,2005-07-07,NULL
作者表(authors)
A_id,A_name
01,王纷
02,李尚
03,泰和
部门表(depts)
d_id,d_name
001,编辑一部
002,编辑二部
003,编辑三部
书和作者关联表(bookmap)
book_id,A_id
001,01
002,01
003,02
004,01
005,01
006,03
005,03
006,02
部门和作者关联表(deptmap)
d_id,a_id
001,01
002,02
002,03
找出每个部门的所写的总书量
*/
--7表名:购物信息
/*购物人 商品名称 数量
A 甲 2
B 乙 4
C 丙 1
A 丁 2
B 丙 5
……
(其他用户实验的记录大家可自行插入)
给出所有购入商品为两种或两种以上的购物人记录
*/
--A.求部门平均薪水的等级。
--B.求部门平均的薪水等级
--C.哪些人是经理
-- D.不准用聚合函数,求薪水的最高值(面试题)
--E.平均薪水最高的部门编号
--F.平均薪水最高的部门名称
--G.求平均薪水的等级最低的部门名称
答案:
-- 有一Blog系统使用的数据库是myblog,现在有几个要求:
/*
1、创建数据库myblog。
2、根据实际应用情况在myblog中创建以下两张表:帐号(account)、文章类别(category)。
3、帐号(account),包括:编号(account_id)、用户名(username)、
密码(password)、邮箱号(email)、性别(gender)、博客名(title)
4、通过alter table给此表添加两个字段:昵称(nickname)、头像(headimg)
5、文章类别表(category)编号(category_id)、帐户编号(account_id)、类别名(title)、
描述(description)、文章数(article_num)、可访问性(visible)、创建时间(created_time)
*/
-- 1、创建数据库myblog。
create database myblog;
use myblog;
-- 根据实际应用情况在myblog中创建以下两张表:
-- 帐号(account)、文章类别(category)
-- 帐号(account),包括:
-- 编号(account_id)、用户名(username)、密码(password)、邮箱号(email)、性别(gender)、博客名(title)
create table account
(
account_id int primary key auto_increment,
username varchar(20),
password varchar(30),
email varchar(20),
gender char(1),
title varchar(40)
);
-- 4、通过alter table给此表添加两个字段:昵称(nickname)、头像(headimg)
alter table account
add (
nickname varchar(20),
headimg varchar(15)
);
-- 文章类别表(category)
-- 编号(category_id)、帐户编号(account_id)、类别名(title)、
-- 描述(description)、文章数(article_num)、可访问性(visible)、创建时间(created_time)
create table category
(
category_id int primary key auto_increment,
account_id int,
title varchar(40),
description varchar(30),
article_num int,
visible char(1),
created_time date
);
--1.给出成绩全部合格的学生信息(包含姓名、课程、分数),注:分数在60以上评为合格
/*表名:成绩表
姓名 课程 分数
张三 语文 81
张三 数学 75
李四 语文 56
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 49
*/
select * from score where sname=
(select sname from score group by sname having min(sscore)>60);
--2)表名:商品表
/*名称 产地 进价
苹果 烟台 2.5
苹果 云南 1.9
苹果 四川 3
西瓜 江西 1.5
西瓜 北京 2.4
*/
--给出平均进价在2元以下的商品名称
select sname from goods group by sname having round(avg(sprice),2)<2;
--3)表名:高考信息表
/*准考证号 科目 成绩
2006001 语文 119
2006001 数学 108
2006002 物理 142
2006001 化学 136
2006001 物理 127
2006002 数学 149
2006002 英语 110
2006002 语文 105
2006001 英语 98
2006002 化学 129
*/
--给出高考总分在600以上的学生准考证号
select sid from scoreinfo group by sid having sum(stuscore)>=600;
--4)表名:team
/*
ID(number型) Name(varchar2型)
1 a
2 b
3 b
4 a
5 c
6 c
要求:执行一个删除语句,当Name列上有相同时,只保留ID这列上值小的
例如:删除后的结果应如下:
ID(number型) Name(varchar2型)
1 a
2 b
5 c
请写出SQL语句。
*/
delete from team where id not in(select a.c from
(select min(id) c, name from team group by name) as a);
--5)表名:student
/*name course score
张青 语文 72
王华 数学 72
张华 英语 81
张青 物理 67
李立 化学 98
张燕 物理 70
张青 化学 76
*/
--查询出“张”姓学生中平均成绩大于75分的学生信息
select * from student where name in
(select name from student
where name like '%张%' group by name
having avg(score) >75);
--6书表(books)
/*
book_id,book_name,creatdate,Lastmodifydate,decription
001,三个人的世界,2005-02-02,2005-07-07,NULL
作者表(authors)
A_id,A_name
01,王纷
02,李尚
03,泰和
部门表(depts)
d_id,d_name
001,编辑一部
002,编辑二部
003,编辑三部
书和作者关联表(bookmap)
book_id,A_id
001,01
002,01
003,02
004,01
005,01
006,03
005,03
006,02
部门和作者关联表(deptmap)
d_id,a_id
001,01
002,02
002,03
找出每个部门的所写的总书量
*/
select d_name,count(*)from depts group by d_name;
--7表名:购物信息
/*购物人 商品名称 数量
A 甲 2
B 乙 4
C 丙 1
A 丁 2
B 丙 5
……
(其他用户实验的记录大家可自行插入)
给出所有购入商品为两种或两种以上的购物人记录
*/
select * from goodsinfo where goodname in
(select goodname from goodsinfo group by goodname having count(*) >= 2);
--A.求部门平均薪水的等级。
select e.DEPTNO,e.avgsal,s.grade from salgrade s ,
(select DEPTNO,round(avg(SAL),2) avgsal from emp,salgrade group by DEPTNO) e
where e.avgsal between s.losal and s.hisal;
--B.求部门平均的薪水等级
select deptno, floor(avg(grade)) from
(select e.DEPTNO, s.grade from emp e,salgrade s where e.SAL between s.losal and s.hisal) e1
group by deptno;
--C.哪些人是经理
select * from emp where JOB='MANAGER';
-- D.不准用聚合函数,求薪水的最高值(面试题)
select * from emp order by SAL desc limit 1;
--E.平均薪水最高的部门编号
select DEPTNO from
(select DEPTNO,round(avg(sal),2) avgsal from emp group by DEPTNO) e order by
e.avgsal desc limit 1;
--F.平均薪水最高的部门名称
select DNAME from dept,
(select DEPTNO,round(avg(sal),2) avgsal from emp group by DEPTNO) e order by
e.avgsal desc limit 1;
--G.求平均薪水的等级最低的部门名称
select DNAME from dept d,
(select DEPTNO,round(avg(sal),2) avgsal from emp group by DEPTNO order by
avgsal limit 1) e where d.DEPTNO=e.DEPTNO;