MYSQL_练习

173 阅读6分钟

一、根据job表,完成如下查询:

1.查询本公司都招聘哪些职位

 select DISTINCT position 
 from job

2.查询本公司的简历都通过哪些途径获取

 select distinct source 
 from job

3.在求职市场专员职位的男性求职者中,显示出最年轻的前10位求职者信息

 select * from job
 where sex='男' and position='市场专员'
 ORDER BY age 
 LIMIT 10 OFFSET 1

4.查询本公司一共收到多少份简历

 select count(id)as num
 from job

5.查询每个招聘途径收到多少份简历

 select source,count(*)as num
 from job
 group by source

6.查询求职“市场专员”职位的男女求职者分别多少人

 select sex,count(*)as num
 from job
 where position='市场专员'
 group by sex

7.查询哪种招聘途径收到的咨询顾问简历最多

 select source,count(*)as num
 from job
 WHERE position='咨询顾问'
 GROUP BY source
 ORDER BY num desc
 limit 1

8.查询年龄在22-28岁之间的求职者中,每个职位有多少人感兴趣,将结果按照人数由多到少显示

 select position,count(*)as num
 from job
 where age between 22 and 28
 group by position
 order by num desc

9.收到简历在20份以上的招聘途径分别是哪些

 select source,count(*)as num
 from job
 GROUP BY source
 having num>20
 ​
 ​

10.查询本公司招聘多少个职位

 select count(distinct source) as num
 from job

11.显示哪个两个职位最受欢迎

 select position,count(*) as num
 from job
 group by position
 order by num desc
 limit 2

12.显示哪个月份收到的简历数最多

 select SUBSTRING(recDate,6,2)as month,count(*)as num
 from job
 GROUP BY month
 ORDER BY num desc
 limit 1

13.显示女性求职者中,每个职位的求职者平均年龄是多少岁

 select ROUND(avg(age),0)as avg_age 
 from job
 where sex='女'

14.显示投递市场专员职位的求职者中,男女求职者的最大年龄分别是多少岁

 select max(age) as max_age
 from job
 group by sex

二、使用employees表和department表做查询操作

1.查询工资总额最高的员工信息

 select * ,(emp_wage+emp_salary)as sum_wage
 from employees
 order by sum_wage desc
 limit 1

2.查询c02部门获得奖金最高的员工是谁

 select * 
 from employees
 where com_id='c02'
 order by emp_salary desc
 limit 1

3.查询所有员工的信息,将信息按照基本工资从高到低显示,当基本工资相同时,按照员工的入职日期由晚到早来显示

 select * from employees
 ORDER BY emp_wage desc,emp_date desc

4.查询显示出所有员工的基本工资总和,基本工资平均值,最高工资和最低工资,给每个结果列都要起一个别名

 select
 sum(emp_wage)as sum_wage,
 avg(emp_wage)as avg_wage,
 max(emp_wage)as max_wage,
 min(emp_wage)as min_wage
 from employees

5.查询销售额最高的部门编号

 select com_id 
 from department
 order by com_total desc
 limit 1

6.根据employee表查询出本公司有几个部门

 select count(distinct(com_id))
 from employees

7.查询出本公司的员工来自几个城市

 SELECT count(distinct (emp_city)) as city_num
 from employees

8.查询哪个部门的平均工资最高

 select com_id,avg(emp_wage) as avg
 from employees
 group by com_id
 ORDER BY avg desc
 limit 1

9.查询来自每个城市的员工分别有多少人

 select emp_city as city,
 count(*) as num
 from employees
 GROUP BY city

10.查询显示该公司每个城市的员工平均工资

 select emp_city as city,
 avg(emp_wage) as avg
 from employees
 GROUP BY city

11.在基本工资高于2000元的员工中,查询每个部门的员工平均工资,并显示出部门平均工资高于4000元的记录,按照平均工资由低到高显示

 select com_id,
 avg(emp_wage) as avg
 from employees
 where emp_wage>2000
 group by com_id
 having avg>4000
 order by avg asc

12.查询部门人数超过6个人的部门编号

 select com_id, count(*)as num
 from employees
 group by com_id
 having num>6

13.查询每个部门的最高奖金为多少钱

 select com_id,max(emp_salary)
 from employees
 group by com_id

14.查询每一年入职的员工有多少人,显示效果如下图所示(提示:函数concat(值1,值2)用于将两个值连接到一起) concat(year(joinDate),’year’)

 select CONCAT(SUBSTRING(emp_date,1,4),'年')as 入职年份,
 count(*) as '人数'
 from employees
 GROUP BY 入职年份

三、使用titles表、sales表做查询

titles(图书标题)表字段:title_id(图书编号),title(标题名),type(类型),price(价格),pubdate(出版日期) sales(销售)表字段:ord_num(订单编号),ord_date(订购日期),qty(订购数量),payterms(付款方式) 1.查询每种类型图书的平均价格

 select type,avg(price) as avg from titles
 group by type

2.查询出版日期在1991年6月份的所有图书中,每种类型图书的平均价格,并将价格由高到低显示

 select type,avg(price) as avg from titles
 where pubdate between '1991-06-01' and '1991-06-30'
 GROUP BY type
 order by avg desc

3.查询每种付款方式订购的图书数量

 SELECT payterms as pay,sum(qty) as sum
     from sales
     group by pay

4.查询每天订购的图书数量

 select ord_date, sum(qty)sum
 from sales
 GROUP BY ord_date

四、分组统计查询

1、根据如下表结构使用SQL命令创建表:ordertb,字段说明如下: 1)orderID:订单号 2)webName:订购商平台 3)productName:商品名称 4)productType:商品类别 5)orderCount:订单数量 6)oderDate:订单日期 7)agent:代理商

 create table ordertb(
 orderID int(11) not null  primary key,
 productName varchar(50) not null,
 productType varchar(30),
 orderCount int(11) not null,
 orderDate datetime  not null,
 agent varchar(30) not null
 );

2、将order.xls文件中的数据导入到数据库的ordertb表中 略 3、完成如下查询 1) 查询订单总数超过5000的有哪些商品

 select productType 
 from ordertb
 group by productType
 having sum(orderCount)>5000

2) 查询每个订购商品都订购了多少次商品

 select productName,count(*)
 from ordertb
 group by productName

3) 查询每个代理商订购的商品总额

 select agent,sum(orderCount)as sum
 from ordertb
 group by agent

4) 将每个订购商订购的商品数量由多到少显示出来

 select agent,sum(orderCount)as sum
 from ordertb
 group by agent
 ORDER BY sum desc

5) 显示每天每件商品的订购数量

 select productName,orderDate,sum(orderCount)
 from ordertb
 group by productName,orderDate

6) 显示每个订购商品订购的每件商品的总数

 select productType,productName,sum(orderCount)
 from ordertb
 group by productType,productName 

7) 显示订购数量最多的商品

 select productName as p,sum(orderCount)
 from ordertb
 group by productName
 order by p desc
 limit 1

8) 显示订货最多的订购商

 select agent as p,sum(orderCount)
 from ordertb
 group by agent
 order by p desc
 limit 1

9) 显示订购数量最多3天???======== 》不会!!!!!!!!!!!!

 select productName,sum(orderCount) as sum
  from ordertb
  group by productName

10) 查询都有哪些订购商品订购了电子产品

 select distinct productName
 from ordertb
 ​
 where productType='电子产品'

11) 查询牙膏的每天订货量,按照订货量由高到低显示

 select orderCount from ordertb
 group by productType,orderDate
 HAVING productType='牙膏'

12) 查询哪个月份的订货量最多

 select substring(orderDate,1,7)as month,
 sum(orderCount) as sum
 from ordertb
 GROUP BY month
 ORDER BY sum desc
 limit 1