1. 程式人生 > >mysql 數據操作 單表查詢 group by 練習

mysql 數據操作 單表查詢 group by 練習

per style spa 平均薪資 bold 以及 數據 薪資 ros

小練習:

1. 查詢崗位名以及崗位包含的所有員工名字

mysql> select post,group_concat(name) from employee group by post ;
+-----------+-------------------------------------------------+
| post      | group_concat(name)                              |
+-----------+-------------------------------------------------+
|
operation | 程咬鐵,程咬銅,程咬銀,程咬金,張野 | | sale | 格格,星星,丁丁,丫丫,歪歪 | | teacher | 成龍,jinxin,jingliyang,liwenzhou,yuanhao,alex | +-----------+-------------------------------------------------+ 3 rows in set (0.00 sec)


2. 查詢崗位名以及各崗位內包含的員工個數

mysql> select
post,count(id) from employee group by post ; +-----------+-----------+ | post | count(id) | +-----------+-----------+ | operation | 5 | | sale | 5 | | teacher | 6 | +-----------+-----------+ 3 rows in set (0.00 sec)


3. 查詢公司內男員工和女員工的個數

mysql> select
sex,count(id) from employee group by sex ; +--------+-----------+ | sex | count(id) | +--------+-----------+ | male | 8 | | female | 8 | +--------+-----------+ 2 rows in set (0.00 sec)


4. 查詢崗位名以及各崗位的平均薪資

mysql> select post,avg(salary) from employee group by post ;
+-----------+---------------+
| post      | avg(salary)   |
+-----------+---------------+
| operation |  16800.026000 |
| sale      |   2600.294000 |
| teacher   | 175766.718333 |
+-----------+---------------+
3 rows in set (0.00 sec)


5. 查詢崗位名以及各崗位的最高薪資

mysql> select post,max(salary) from employee group by post ;
+-----------+-------------+
| post      | max(salary) |
+-----------+-------------+
| operation |    20000.00 |
| sale      |     4000.33 |
| teacher   |  1000000.31 |
+-----------+-------------+
3 rows in set (0.10 sec)


6. 查詢崗位名以及各崗位的最低薪資

mysql> select post,min(salary) from employee group by post ;
+-----------+-------------+
| post      | min(salary) |
+-----------+-------------+
| operation |    10000.13 |
| sale      |     1000.37 |
| teacher   |     2100.00 |
+-----------+-------------+
3 rows in set (0.00 sec)


7. 查詢男員工與男員工的平均薪資,女員工與女員工的平均薪資

mysql> select sex,avg(salary) from employee group by sex ;
+--------+---------------+
| sex    | avg(salary)   |
+--------+---------------+
| male   | 136700.055000 |
| female |   7250.183750 |
+--------+---------------+
2 rows in set (0.01 sec)

 

mysql 數據操作 單表查詢 group by 練習