日常练习题

日常练习

border by 排序降序desc  排序ase(放在最后)
group by 分组  count
sum(求和)
avg(平均)
count(计数)
max(最大)
min(最小)
having 保存as字段 数值
limit--限定查询

基本语法:

Select  select_list

 From  table_name

 Where  condition_expression

Group by  group_columns   

having condition_expression

Order by sort_columns

Limit skip,pagesize

练习

1)MYSQL中的关于分页语句的用法:

SELECT * FROM table LIMIT skip,pagesize

select 员工编号,姓名 from 员工 limit 20,10

page pagesize

select 员工编号,姓名 from 员工 limit (page-1)*pagesize,pagesize

取员工表第一页的数据(每页5条记录)

page=1,pagesize=5

select 员工编号,姓名 from 员工 limit 0,5

取员工表第二页的数据(每页5条记录)

select 员工编号,姓名 from 员工 limit 5,5

取员工表第三页的数据(每页5条记录)

select 员工编号,姓名 from 员工 limit 9,5

2)从学生表中查询各个专业的人数

select 专业,count(*) as 人数 from 学生 group by 专业

3)从学生表中统计每个专业男女生人数

select 专业,性别,count(*) as 人数 from 学生 group by 专业,性别

4)从学生表中查询超过2个人专业。

select 专业,count(*) as 人数 from 学生 group by 专业 having 人数>2

  1. 统计哪个专业没有女生。

select distinct 专业 from 学生 where 专业 not in ( select 专业 from 学生 group by 专业,性别 having 性别=‘女’)

select distinct 专业 from 学生 where 专业 not in (select distinct 专业 from 学生 where 性别=‘女’)

=接下来是聚合函数的内容====================

COUNT()函数

SUM()函数

AVG()函数

MAX()函数

MIN()函数

6)我想知道公司有多少个员工?

select count(*) from 员工

等价于:

select count(员工编号) from 员工

7)统计员工表中发放员工工资总和

select sum(目前薪资) as 工资总额 from 员工

8)统计本公司员工平均工资是多少。

select avg(目前薪资) as 平均工资 from 员工

9)统计本公司最高薪资是多少?最低薪资是多少

select max(目前薪资) as 最高工资 from 员工

select min(目前薪资) as 最低工资 from 员工

10)统计本公司的基本信息(人数,月工资发放总额,员工平均工资,月最高工资,月最低工资)

select count(*) as 人数,sum(目前薪资) as 工资总额,avg(目前薪资) as 平均工资,max(目前薪资) as 最高工资,min(目前薪资) as 最低工资 from 员工

11)我想知道本公司月薪最高的员工是哪位?

select 员工姓名,部门,目前薪资 from 员工 where 目前薪资 = max(目前薪资)

  1. 我想知道本公司月薪最低的员工是哪位?

select 姓名,部门,目前薪资 from 员工 where 目前薪资=(select min(目前薪资) as 最低工资 from 员工)

13)本公司年龄最大的员工是哪位?

select 姓名,部门,出生日期 from 员工 where 出生日期=(select min(出生日期) from 员工)

另一种实现方式:

select 姓名,部门,year(now())-year(出生日期) as 年龄 from 员工 where

year(now())-year(出生日期)=(select max(year(now())-year(出生日期) ) from 员工)

14)查询出生产制造部员工的最高工资

select max(目前薪资) from 员工 where 部门=‘生产制造部’

15)查询出比生产造部员工最高工资还要高的其他部门员工工资情况

select 姓名,部门,目前薪资 where 目前薪资>(生产制造部员工的最高工资)

select 姓名,部门,目前薪资 from 员工 where 目前薪资>(select max(目前薪资) from 员工 where 部门=‘生产制造部’) and 部门!=‘生产制造部’

16)查询比生产制造部平均工资还要高的其他部门员工的工资情况

select 姓名,部门,目前薪资 from 员工 where 目前薪资>(生产制造部员工的平均工资) and

部门!=‘生产制造部’

生产制造部员工的平均工资:select avg(目前薪资) from 员工 where 部门=‘生产制造部’

select 姓名,部门,目前薪资 from 员工 where 目前薪资>(select avg(目前薪资) from 员工 where 部门=‘生产制造部’) and

部门!=‘生产制造部’

以下是关于分组的训练=============

17)统计各个部门的基本信息(人数,月工资支出,平均工资,月最高工资,月最低工资)

select count(*) as 人数,sum(目前薪资) as 月工资支出,avg(目前薪资) as 平均工资,max(目前薪资) as 最高工资,min(目前薪资) as 最低工资 from 员工

group by 部门

18)在上面查询的基础上进行过滤和排序:

  • 要求过滤出人数大于660人的大部门
  • 同时按人数由高到低进行部门排序
    select 部门,count(*) as 人数,sum(目前薪资) as 月工资支出,avg(目前薪资) as 平均工资,max(目前薪资) as 最高工资,min(目前薪资) as 最低工资 from 员工
    group by 部门 having 人数>660 order by 人数 desc limit 1

19)统计出生产制造部中女性员工的人数

select 部门,性别,count(*) as 人数 from 员工 where 部门=‘生产制造部’ and 性别=-1

19)统计出生产制造部中员工的人数(另一种写法)

以下是OK的:

select 部门,

case 性别

when 0 then ‘男’

when -1 then ‘女’

end as sex ,count(*) as 人数 from 员工 group by 部门,性别 having 部门=‘生产制造部’ and 性别=-1

select 部门,

case 性别

when 0 then ‘男’

when -1 then ‘女’

end as sex ,count(*) as 人数 from 员工 group by 部门,性别 having 部门=‘生产制造部’ and sex=‘女’

但以下是不OK的

select 部门,

case 性别

when 0 then ‘男’

when -1 then ‘女’

end as sex ,count(*) as 人数 from 员工 group by 部门,性别 having 部门=‘生产制造部’ and 性别=‘女’

select 部门,

case 性别

when 0 then ‘男’

when -1 then ‘女’

end as sex ,count(*) as 人数 from 员工 group by 部门,性别 having 部门=‘生产制造部’ and sex=-1

更复杂的示例:

select 部门,

case 性别

when 0 then ‘男’

when -1 then ‘女’

end as sex ,婚姻状况, count(*) as 人数 from 员工 group by 部门,性别,婚姻状况 having 部门=‘生产制造部’ and sex=‘女’ and 婚姻状况=-1

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值