mysql语句 查询前5个_MySQL 查询语句--------------进阶5:分组查询
#進(jìn)階5:分組查詢(xún)
/*
select 分組函數(shù),列(要求出現(xiàn)在group by的后面)
from 表 【where 篩選條件】 group by 分組的列表 【order by 子句】
注意:
查詢(xún)列表必須特殊,要求是分組函數(shù)和group by后出現(xiàn)的字段
特點(diǎn):
1.分組查詢(xún)中的篩選條件分為兩類(lèi)
數(shù)據(jù)源位置關(guān)鍵字
分組前篩選 原始表 group by 前where
分組后篩選 分組后的結(jié)果集 group by 后having
分組函數(shù)做條件,一定是放在having子句之中
group by子句支持單個(gè)字段分組,多個(gè)字段分組(多個(gè)字段之間用逗號(hào)隔開(kāi)沒(méi)有順序要求),表達(dá)式或者函數(shù)(用的較少)
也可以添加排序(排序放在分組查詢(xún)的最后)
*/
# 引入:查詢(xún)每個(gè)部門(mén)的平均工資
select avg(salary) from employees; #這個(gè)是整個(gè)表格的基本工資
#需要拆分小組
select avg(salary),department_id from employees group by department_id; #這個(gè)結(jié)果不太對(duì)
select distinct department_id from employees;
#案例1:查詢(xún)每個(gè)工種的最高工資
select max(salary),job_id from employees group by job_id;
#案例2:查詢(xún)每個(gè)位置上的部門(mén)個(gè)數(shù)
select count(department_id),location_id from departments group by location_id;
#添加 分組前 篩選條件
#案例1:查詢(xún)郵箱中包含a字符的,每個(gè)部門(mén)的平均工資
select avg(salary),department_id from employees where email like "%a%" group by department_id;
#案例2:查詢(xún)有獎(jiǎng)金的每個(gè)領(lǐng)導(dǎo)手下員工的最高工資
select max(salary),manager_id from employees where commission_pct is not null group by manager_id;
#添加 分組后 的復(fù)雜的篩選條件
#案例1:查詢(xún)哪個(gè)部門(mén)的員工個(gè)數(shù)>2
select count(*),department_id from employees where count(*)>2 group by department_id; #這個(gè)是錯(cuò)誤的,因?yàn)閑mployees中沒(méi)有 count(*)>2
這里使用having
select count(*),department_id from employees group by department_id having count(*)>2 ;
#案例2:查詢(xún)每個(gè)工種有獎(jiǎng)金的員工,他們的最高工資>12000的工種編號(hào)和最高工資
select max(salary),job_id from employees where commission_pct is not null group by job_id; #獲得有獎(jiǎng)金條件下的,每個(gè)工種的最高工工資
# 加上:他們的最高工資>12000的工種編號(hào)和最高工資
select max(salary),job_id from employees where commission_pct is not null group by job_id having max(salary)>12000;
#案例3:查詢(xún)領(lǐng)導(dǎo)編號(hào)>102的每個(gè)領(lǐng)導(dǎo)手下的最低工資>5000的領(lǐng)導(dǎo)編號(hào)是哪個(gè),以及其最低的工資
篩選條件:
where manager_id>102
having min(salary)>5000
完整:
select min(salary),manager_id from employees where manager_id>102 group by manager_id having min(salary)>5000;
# group by 后跟 表達(dá)式或者分組函數(shù)(可以不是簡(jiǎn)單的字段)
#案例:按照員工姓名的長(zhǎng)度分組,查詢(xún)每一組的員工個(gè)數(shù),篩選員工個(gè)數(shù)>5的有哪些
select count(employee_id),length(last_name) from employees group by length(last_name) having count(employee_id)>5;
# 按照多個(gè)字段分組
#案例:查詢(xún)每個(gè)部門(mén)每個(gè)工種的員工的平均工資
select avg(salary),department_id,job_id from employees group by department_id,job_id;
# 添加排序
#案例:查詢(xún)每個(gè)部門(mén)每個(gè)工種的員工的平均工資,并且按照平均工資的高低顯示
select avg(salary),department_id,job_id from employees group by department_id,job_id order by avg(salary) desc;
order by 后面可以跟函數(shù)語(yǔ)句。
#題目1:查詢(xún)各個(gè)job_id的員工工資的最大值、最小值、平均值、總和,并按照job_id升序
select max(salary),min(salary),avg(salary),sum(salary),job_id from employees group by job_id order by job_id asc;
#題目2:查詢(xún)員工最高工資和最低工資的差距(difference)
select max(salary)-min(salary) difference from employees;
#題目3:查詢(xún)各個(gè)管理者手下員工的最低工資,其中最低工資不能低于6000,沒(méi)有管理者的員工不計(jì)算在內(nèi)。
篩選條件:
where manager_id is not null
having min(salary)>=6000
select min(salary),manager_id from employees where manager_id is not null group by manager_id having min(salary)>=6000;
#題目4:查詢(xún)所有部門(mén)的編號(hào),員工數(shù)量和工資的平均值,并按照平均工資降序
select count(employee_id),avg(salary),department_id from employees group by department_id order by avg(salary) desc;
#題目5:選擇具有各個(gè)job_id的員工人數(shù)
select count(*),job_id from employees group by job_id;
總結(jié)
以上是生活随笔為你收集整理的mysql语句 查询前5个_MySQL 查询语句--------------进阶5:分组查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 线程组之间的JMeter传递变量
- 下一篇: 【转载文章】记录一次MySQL两千万数据