sql統計公司最高薪酬和薪酬分布

2022-07-31 00:57:19 字數 1424 閱讀 2323

統計薪酬最高的和薪酬分布情況,下面包括表的建立和sql語句:

create table `employee` (

`id`

int(4

) default null,

`name` varchar(

12) default null,

`salary` varchar(

11) default null,

`iod`

int(4

) default null

) engine=innodb default charset=utf8

新增相應資料後得到表employee為:

查詢最高薪酬:

select office.`name` as

office,

employee.`name`

asname,

employee.salary

from

office,employee

where employee.iod=office.iod

and employee.salary=(select max(salary) from employee where employee.iod=office.iod);

#沒有分組,避免了多人具有相同最高工資只顯示一人的情況

得到結果為:

檢視薪酬分組和百分比情況:

select

(case when salary<3000 then '

小於3000

'when salary>=3000 and salary<5000 then '

大於等於3000小於5000

'else

'大於等於5000

' end) as

levels,

count(salary)

ascount,

concat(convert(count(salary)*100/(select count(id) from employee),decimal(4,1)),'

%') as

persentage

from

employee group by levels ;

#concat--連線字串

#convert--引數分別可以表示( 要轉換到的型別, 合法的表示式, 格式化型別)

#decimal(a,b) a--代表精度 b--代表小數點位數

結果如圖:

如何優化sql,效率最高,SQL索引優化

索引概念和作用 索引是一種使記錄有序化的技術,它可以指定按某 列 某幾列預先排序,從而大大提高查詢速度 類似 於漢語詞典中按照拼音或者筆畫查詢 索引的主要作用是加快資料查詢速度,提高資料庫 的效能。mysql索引型別 從物理儲存角度上,索引可以分為聚集索引和非聚 集索引。1.聚集索引 cluster...

統計報表 sql統計語句

需要資料統計頁面,肯定需要匯出資料,於是,邊學邊寫,完成了一段sql 最早的版本是這樣的 分三條sql查出三種不同的狀態的記錄數 總記錄,未支付,已支付 select count as recordcount from t record where createtime 2019 01 01 00 ...

SQL 部門薪資最高的員工

employee表 idname salary departmentid 1001 emma 12000 11002 alex 13000 21003 tom9000 31004 star 11000 11005 eric 15000 2department表 idname1it 2sales 3f...