数据库查询练习

数据库查询练习

一、示例表格

在这里插入图片描述

二、练习

1.显示所有职工的基本信息

mysql> select *from worker ;

在这里插入图片描述
2. 查询所有职工所属部门的部门号,不显示重复的部门号

mysql> select distinct `部门号` from worker;

在这里插入图片描述
3. 求出所有职工的人数

mysql> select count(1) from worker;

在这里插入图片描述
4. 列出最高工资和最低工资

mysql> select max(`工资`) as `最高工资`,min(`工资`) as `最低工资` from worker;

在这里插入图片描述
5. 列出职工的平均工资和总工资

mysql> select avg(`工资`) as `平均工资`,sum(`工资`) as `总工资` from worker;

在这里插入图片描述
6. 创建一个只有职工号、姓名和参加工作时间的新表,名为工作日期表

mysql> create table work_date select `职工号`,`姓名`,`工作时间` from worker;

在这里插入图片描述

7. 列出所有姓赵的职工的职工号、姓名和出生日期。

mysql> select `职工号`,`姓名`,`出生日期` from worker where `姓名` like '赵%';

在这里插入图片描述
8. 列出1991年以前出生的职工的姓名、参加工作日期

mysql> select `姓名`,`工作时间` from worker where `出生日期`<'1991-1-1';

在这里插入图片描述
9. 列出工资在3000-5000之间的所有职工姓名

mysql> select `姓名` from worker where `工资`>3000 and `工资`<5000;

在这里插入图片描述
10. 列出所有姓赵和李的职工姓名

mysql> select `姓名` from worker where `姓名` like '李%' or `姓名` like '赵%';

在这里插入图片描述
11. 列出所有部门号为2和3的职工号、姓名、党员否

mysql> select `职工号`,`姓名`,`政治面貌` from worker where `部门号` in (102,103) and `政治面貌` = '党员';

在这里插入图片描述

12. 将职工表worker中的职工按出生的先后顺序排序

mysql> select *from worker order by `出生日期`;

在这里插入图片描述

13. 显示工资最高的前3名职工的职工号和姓名

mysql> select `职工号`,`姓名` from worker order by `工资` desc limit 0,3;

在这里插入图片描述

14. 求出各部门党员的人数

mysql> select `部门号`,count(*) from worker where `政治面貌`= '党员' group by `部门号` having count(*)>1;

在这里插入图片描述
15. 统计各部门的工资和平均工资

mysql> select `部门号`,sum(`工资`),avg(`工资`) from worker group by `部门号`;

在这里插入图片描述
16. 列出总人数大于4的部门号和总人数

mysql> select `部门号`,count(*) from worker group by `部门号` having count(*)>=4;

在这里插入图片描述

create table sailors( sid char(10) primary key, sname char(20), rating int, age int); create table boats( bid char(10) primary key, bname char(20), color char(10)); create table reserves( sid char(10) , bid char(10) , rdate date, primary key(sid,bid,rdate), foreign key (sid) references sailors(sid) on delete cascade, foreign key (bid) references boats(bid) on delete cascade); insert into sailors(sid,sname,rating,age) values("22","dustin",7,45) ("29","brustus",1,33), ("31","lubber",8,56), ("32","andy",8,26), ("58","rusty",10,35), ("64","horatio",7,35), ("71","zorba",10,35), ("74","horatio",9,35), ("85","art",3,26), ("86","john",1,17), ("95","bob",3,64), ("96","frodo",3,26), ("98","tom",3,17); insert into boats(bid,bname,color) values("101","A","red"), ("102","B","green"), ("103","C","blue"), ("104","D","white") ("105","E","red"), ("106","F","blue"), ("107","G","green"); insert into reserves(sid,bid,rdata) values("22","101","2010-01-08"), ("22","102","2010-01-09"), ("29","103","2010-01-09"), ("31","102","2010-02-11"), ("22","104","2010-03-08"), ("22","103","2010-03-10"), ("32","105","2010-03-11"), ("32","106","2010-03-18"), ("32","102","2010-03-19"), ("58","104","2010-03-20"), ("64","105","2010-03-20"), ("95","101","2010-04-02"), ("85","102","2010-04-05"), ("22","101","2010-04-07"), ("22","105","2010-05-01"), ("22","106","2010-06-18"), ("22","107","2010-07-09"), ("31","106","2010-08-06"), ("32","105","2010-08-06"), ("29","104","2010-08-07"), ("64","103","2010-09-05"), ("58","102","2010-09-09"), ("64","104","2010-11-03"), ("64","105","2010-11-04"), ("31","106","2010-12-0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值