mysql面试题1

MySQL实训_01
1. 创建数据库,名称为cdadb;(如果已有,则省略)
2. 创建数据表customer(客户)、deposite(存款)、bank(银行),表结构如下:
customer的表结构:
属性名称 类型与长度 中文含义 备注
c_id char(6) 客户标识 主键,非空
name varchar(30) 客户姓名 非空
location Varchar(30) 工作地点
salary decimal(8,2) 工资
bank的表结构:
属性名称 类型与长度 中文含义 备注
b_id char(5) 银行标识 主键,非空
bank_name char(30) 银行名次 非空
deposite的表结构:
属性名称 类型与长度 中文含义 备注
d_id int 存款流水号 主键,非空,自增
c_id char(6) 客户标识 外键,关联customer表的c_id
b_id char(5) 银行标识 外键,关联bank表的b_id
dep _date date 存入日期
dep_type char(1) 存款期限 1,3,5分别代表1年期、3年期和5年期
amount decimal(8,2) 存款金额
3. 录入数据如下:
customer的数据如下,注意最后一条记录用你的学号和你的姓名代替。
c_id name location salary
101001 孙杨 广州 1234
101002 郭海 南京 3526
101003 卢江 苏州 6892
101004 郭惠 济南 3492
你的学号 你的姓名 北京 6324

bank的数据如下:
b_id bank_name
B0001 工商银行
B0002 建设银行
B0003 中国银行
B0004 农业银行
deposite的数据如下:
d_id c_id b_id dep_date dep_type amount
1 101001 B0001 2011-04-05 3 42526
2 101002 B0003 2012-07-15 5 66500
3 101003 B0002 2010-11-24 1 42366
4 101004 B0004 2008-03-31 1 62362
5 101001 B0003 2002-02-07 3 56346
6 101002 B0001 2004-09-23 3 353626
7 101003 B0004 2003-12-14 5 36236
8 101004 B0002 2007-04-21 5 26267
9 101001 B0002 2011-02-11 1 435456
10 101002 B0004 2012-05-13 1 234626
11 101003 B0003 2001-01-24 5 26243
12 101004 B0001 2009-08-23 3 45671
4. 更新customer表的salary属性,将salary低于5000的客户的salary变为原来的2倍.
5. 对deposite表进行统计,按银行统计存款总数,显示为b_id,total.
6. 对deposite、customer、bank进行查询,查询条件为location在广州、苏州、济南的客户,存款在300000至500000之间的存款记录,显示客户姓名name、银行名称bank_name、存款金额amount.

关系型数据库和MySQL作业

作业:使用下面的SQL在MySQL中创建数据库、二维表并插入数据,然后完成下面的查询操作。
create database hrs default charset utf8mb4;

use hrs;

create table tb_dept
(
dno int not null comment '编号',
dname varchar(10) not null comment '名称',
dloc varchar(20) not null comment '所在地',
primary key (dno)
);

insert into tb_dept values 
	(10, '会计部', '北京'),
	(20, '研发部', '成都'),
	(30, '销售部', '重庆'),
	(40, '运维部', '深圳');

create table tb_emp
(
eno int not null comment '员工编号',
ename varchar(20) not null comment '员工姓名',
job varchar(20) not null comment '员工职位',
mgr int comment '主管编号',
sal int not null comment '员工月薪',
comm int comment '每月补贴',
dno int comment '所在部门编号',
primary key (eno),
foreign key (dno) references tb_dept(dno),
foreign key (mgr) references tb_emp(eno)
);

insert into tb_emp values 
	(7800, '张三丰', '总裁', null, 9000, 1200, 20),
	(2056, '乔峰', '分析师', 7800, 5000, 1500, 20),
	(3088, '李莫愁', '设计师', 2056, 3500, 800, 20),
	(3211, '张无忌', '程序员', 2056, 3200, null, 20),
	(3233, '丘处机', '程序员', 2056, 3400, null, 20),
	(3251, '张翠山', '程序员', 2056, 4000, null, 20),
	(5566, '宋远桥', '会计师', 7800, 4000, 1000, 10),
	(5234, '郭靖', '出纳', 5566, 2000, null, 10),
	(3344, '黄蓉', '销售主管', 7800, 3000, 800, 30),
	(1359, '胡一刀', '销售员', 3344, 1800, 200, 30),
	(4466, '苗人凤', '销售员', 3344, 2500, null, 30),
	(3244, '欧阳锋', '程序员', 3088, 3200, null, 20),
	(3577, '杨过', '会计', 5566, 2200, null, 10),
	(3588, '朱九真', '会计', 5566, 2500, null, 10);
  1. 查询月薪最高的员工姓名和月薪

    mysql> select ename as 姓名,sal as 月薪 from tb_emp where sal = (select max(sal) f
    rom tb_emp);
    
  2. 查询员工的姓名和年薪((月薪+补贴)*13)

    mysql> select ename as 姓名,(sal+ifnull(comm,0))*13 as 年薪 from tb_emp;
    
  3. 查询所有部门的名称和人数

    mysql> select dname as 部门,total as 人数 from tb_dept d left join (select dno,count(dno) as total from tb_emp group by dno) e on d.dno = e.dno;
    
  4. 查询月薪最高的员工(Boss除外)的姓名和月薪

    mysql> select ename as 姓名,sal as 月薪 from tb_emp where sal = (select max(sal) from tb_emp where job !='总裁');
    
  5. 查询月薪超过平均月薪的员工的姓名和月薪

    mysql> select ename as 姓名,sal as 月薪 from tb_emp where sal > (select avg(sal) from tb_emp);
    
  6. 查询月薪超过其所在部门平均月薪的员工的姓名、部门编号和月薪

    mysql> select e.ename as 姓名,d.dno as 部门编号,e.sal as 月薪 from tb_emp e inner
    join (select dno,avg(sal) as avgsal from tb_emp group by dno) as d on d.dno =e.dno where e.sal > d.avgsal;
    
  7. 查询部门中月薪最高的人姓名、月薪和所在部门名称

    # 1 部门中月薪最高的拿出来  
    SELECT dno,max(sal) as maxsal from tb_emp GROUP BY dno;
    
    # 2  部门中月薪最高的人 部门编号 拿出来
    SELECT ename,sal,t1.dno from tb_emp t1 INNER JOIN (SELECT dno,max(sal) as maxsal from tb_emp GROUP BY dno) t2 on t1.dno=t2.dno where sal=maxsal;
    # 3 根据上表 拿出 部门名称  
    mysql> select e.ename as 姓名,p.dname as 部门名称,e.sal as 月薪 from tb_emp e inner join (select dno,max(sal) as maxsal from tb_emp group by dno) d on d.dno = e.dno inner join tb_dept p on d.dno =p.dno where e.sal = d.maxsal;
    
  8. 查询主管的姓名和职位

    SELECT eno,ename,job from tb_emp where eno in (SELECT DISTINCT mgr from tb_emp where mgr is not null);
    
  9. 查询月薪排名4~6名的员工排名、姓名和月薪

SELECT @n:=@n+1 as rank, ename,sal from tb_emp,(SELECT @n:=0) t1 ORDER BY sal desc;
SELECT ename,rank,sal from (SELECT @n:=@n+1 as rank, ename,sal from tb_emp,(SELECT @n:=0) t1 ORDER BY sal desc) t2 LIMIT 3,3;


SELECT (select count(eno) from tb_emp t2 where t1.sal<t2.sal) as '排名',t1.ename as '姓名',t1.sal as '月薪' from tb_emp t1 where t1.sal in (SELECT sal from tb_sal) ORDER BY t1.sal desc;

create table tb_sal(SELECT DISTINCT sal from tb_emp ORDER BY sal desc limit 3,3);
  1. 有员工的部门编号和人数

    SELECT dno,COUNT(dno) as 人数 from tb_emp GROUP BY dno;
    
    
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值