MySQL经典18道sql试题

Employees MySQL经典18道sql试题
为了培养新人,本人特别准备了18道MySQL sql试题,题不在于多,而在于精,意在锻炼他们对表结构的认识,数据库函数的调用,业务的理解上。废话不说直接上题,答案暂时不公布,先让他们动动脑筋。

本套试题所涉及到的库为MySQL经典的Employees库,关联图如下:
MySQL经典18道sql试题_第1张图片

    mysql> use employees;
    Database changed
    mysql> select table_name,table_schema,table_rows from information_schema.tables where table_schema='employees';
    +----------------+--------------+------------+
    | table_name     | table_schema | table_rows |
    +----------------+--------------+------------+
    | departments    | employees    |          9 |
    | dept_emp       | employees    |     331143 |
    | dept_manager   | employees    |         24 |
    | employees      | employees    |     299556 |
    | salaries       | employees    |    2838426 |
    | temp_employees | employees    |         10 |
    | titles         | employees    |     442248 |
    +----------------+--------------+------------+
    7 rows in set (0.00 sec)

1、统计人力资源部(Human Resources)一共有多少人任职(包括在职和离职),字段包括员工数量(count)。
输出结果如下。

    +-------+
    | count |
    +-------+
    | 17786 |
    +-------+

2、统计开发部(Development)在1990年,一年里入职又离职的员工信息,字段包括员工编号(emp_no),员工编号(名称),入职日期(from_date),离职日期(to_date)。
输出结果如下。

    +--------+--------------+------------+------------+
    | emp_no | last_name    | from_date  | to_date    |
    +--------+--------------+------------+------------+
    |  10902 | Perly        | 1990-06-11 | 1990-09-25 |
    |  15194 | Rosar        | 1990-08-21 | 1990-08-24 |
    |  25495 | Lakshmanan   | 1990-08-20 | 1990-10-04 |
    |  32890 | Horswill     | 1990-08-13 | 1990-08-14 |
    |  33090 | Syrzycki     | 1990-08-13 | 1990-11-08 |
    |  38357 | Turnbull     | 1990-09-04 | 1990-10-04 |
    |  38691 | Denos        | 1990-04-18 | 1990-05-04 |
    |  39802 | Rodier       | 1990-09-15 | 1990-11-01 |
    |  42026 | Impagliazzo  | 1990-01-06 | 1990-09-07 |
    |  59088 | Lowrie       | 1990-02-16 | 1990-03-31 |
    |  65478 | Baja         | 1990-06-22 | 1990-07-04 |
    |  73856 | Rathonyi     | 1990-05-05 | 1990-08-11 |
    |  75543 | Valiente     | 1990-02-08 | 1990-11-22 |
    |  80993 | Kavraki      | 1990-02-26 | 1990-04-11 |
    |  86446 | Awdeh        | 1990-04-04 | 1990-05-03 |
    |  87548 | Doering      | 1990-06-16 | 1990-06-20 |
    |  96872 | Munke        | 1990-01-06 | 1990-01-14 |
    |  99437 | Thiran       | 1990-01-12 | 1990-08-08 |
    | 104794 | Hofman       | 1990-02-20 | 1990-12-17 |
    | 107127 | Madeira      | 1990-03-20 | 1990-04-13 |
    | 201815 | Cooke        | 1990-06-27 | 1990-11-10 |
    | 203700 | Seuren       | 1990-07-22 | 1990-09-22 |
    | 204773 | Vesna        | 1990-07-07 | 1990-10-09 |
    | 209747 | Millington   | 1990-04-08 | 1990-12-22 |
    | 212020 | Liedekerke   | 1990-04-11 | 1990-08-23 |
    | 212119 | Randi        | 1990-01-30 | 1990-08-23 |
    | 217590 | Lamba        | 1990-05-21 | 1990-12-16 |
    | 219460 | Shimshoni    | 1990-01-11 | 1990-11-25 |
    | 222006 | Gimbel       | 1990-10-13 | 1990-10-13 |
    | 233261 | Lally        | 1990-02-01 | 1990-06-01 |
    | 236998 | Cummings     | 1990-04-28 | 1990-08-19 |
    | 238135 | Heijenga     | 1990-04-07 | 1990-11-02 |
    | 239469 | Merli        | 1990-01-24 | 1990-08-16 |
    | 241112 | Bharadwaj    | 1990-06-10 | 1990-07-17 |
    | 245024 | Zhiwei       | 1990-05-18 | 1990-09-17 |
    | 247263 | Schach       | 1990-03-24 | 1990-12-07 |
    | 247661 | Sabnani      | 1990-04-19 | 1990-07-05 |
    | 253013 | Brandsma     | 1990-07-07 | 1990-11-24 |
    | 256418 | Ulupinar     | 1990-06-23 | 1990-07-15 |
    | 257472 | Pashtan      | 1990-08-16 | 1990-12-22 |
    | 259776 | Baez         | 1990-01-04 | 1990-12-20 |
    | 260699 | Staudhammer  | 1990-06-09 | 1990-08-02 |
    | 261227 | Munke        | 1990-07-14 | 1990-09-17 |
    | 262481 | Swiss        | 1990-04-22 | 1990-11-19 |
    | 271302 | Herath       | 1990-01-05 | 1990-05-22 |
    | 275235 | Covnot       | 1990-09-01 | 1990-09-25 |
    | 276038 | Bellmore     | 1990-08-03 | 1990-12-05 |
    | 276253 | Luef         | 1990-07-09 | 1990-12-22 |
    | 277339 | Kamble       | 1990-08-28 | 1990-10-23 |
    | 279822 | Kalsbeek     | 1990-03-05 | 1990-04-13 |
    | 285432 | Karcich      | 1990-07-31 | 1990-08-16 |
    | 285586 | Silva        | 1990-09-01 | 1990-11-22 |
    | 288724 | Ouhyoung     | 1990-01-16 | 1990-10-24 |
    | 289271 | Macedo       | 1990-01-27 | 1990-03-05 |
    | 295205 | Schwabacher  | 1990-01-03 | 1990-06-07 |
    | 296608 | Ravishankar  | 1990-07-05 | 1990-07-12 |
    | 400181 | Gurbaxani    | 1990-01-22 | 1990-04-11 |
    | 401024 | Rotem        | 1990-06-15 | 1990-11-20 |
    | 402369 | Emmart       | 1990-08-17 | 1990-12-14 |
    | 404442 | Bahk         | 1990-10-26 | 1990-12-03 |
    | 412045 | Bolotov      | 1990-07-29 | 1990-08-07 |
    | 416310 | Schapire     | 1990-03-16 | 1990-12-29 |
    | 418567 | Nollmann     | 1990-08-19 | 1990-09-16 |
    | 421381 | Heusch       | 1990-08-20 | 1990-09-03 |
    | 424000 | Zlotek       | 1990-08-30 | 1990-11-06 |
    | 424227 | Birke        | 1990-04-21 | 1990-12-25 |
    | 432473 | Plesums      | 1990-02-03 | 1990-08-20 |
    | 436610 | Garnham      | 1990-04-16 | 1990-11-28 |
    | 441212 | Picaronny    | 1990-05-27 | 1990-09-18 |
    | 441423 | Irland       | 1990-01-15 | 1990-05-20 |
    | 442169 | Vidya        | 1990-11-09 | 1990-11-24 |
    | 444955 | Provine      | 1990-06-07 | 1990-09-23 |
    | 447339 | Demos        | 1990-10-31 | 1990-11-17 |
    | 451512 | Roisin       | 1990-11-05 | 1990-11-29 |
    | 456501 | Bierman      | 1990-04-12 | 1990-07-19 |
    | 457109 | Baaz         | 1990-04-27 | 1990-06-12 |
    | 465347 | Peroz        | 1990-02-10 | 1990-06-01 |
    | 465879 | Mullainathan | 1990-06-23 | 1990-12-29 |
    | 470097 | Constantine  | 1990-06-03 | 1990-10-19 |
    | 470701 | Cyre         | 1990-08-15 | 1990-11-26 |
    | 480924 | Busillo      | 1990-02-26 | 1990-03-03 |
    | 481369 | Coombs       | 1990-03-21 | 1990-09-21 |
    | 483274 | Lichtner     | 1990-01-25 | 1990-11-28 |
    | 487291 | Thorensen    | 1990-09-20 | 1990-11-12 |
    | 489277 | Yamaashi     | 1990-11-17 | 1990-12-28 |
    | 490851 | Plotkin      | 1990-04-11 | 1990-05-27 |
    | 492491 | Peron        | 1990-04-27 | 1990-08-29 |
    | 496147 | Neiman       | 1990-04-17 | 1990-04-17 |
    +--------+--------------+------------+------------+

3、统计开发部(Development)在2000年度里,每月员工人事调动或离职的情况,并按照月份排序,字段包括月份名称(month),员工数量(count)。
输出结果如下。

    +-----------+-------+
    | month     | count |
    +-----------+-------+
    | January   |   236 |
    | February  |   251 |
    | March     |   258 |
    | April     |   255 |
    | May       |   260 |
    | June      |   220 |
    | July      |   243 |
    | August    |   246 |
    | September |   247 |
    | October   |   230 |
    | November  |   233 |
    | December  |   227 |
    +-----------+-------+

4、统计开发部(Development)在2000年1月份,每天员工离职的情况,并按照日期排序,字段包括日期(day),员工数量(count)。
输出结果如下。

    +------+-------+
    | day  | count |
    +------+-------+
    |    1 |     6 |
    |    2 |    10 |
    |    3 |     6 |
    |    4 |     3 |
    |    5 |    10 |
    |    6 |     8 |
    |    7 |     6 |
    |    8 |     6 |
    |    9 |    11 |
    |   10 |     5 |
    |   11 |     6 |
    |   12 |     7 |
    |   13 |     3 |
    |   14 |     7 |
    |   15 |     7 |
    |   16 |    10 |
    |   17 |     7 |
    |   18 |     4 |
    |   19 |    13 |
    |   20 |     6 |
    |   21 |    14 |
    |   22 |    10 |
    |   23 |     6 |
    |   24 |     7 |
    |   25 |     5 |
    |   26 |    11 |
    |   27 |    13 |
    |   28 |     7 |
    |   29 |     9 |
    |   30 |     8 |
    |   31 |     5 |
    +------+-------+

5、统计开发部(Development)在2000年1月份,每天员工离职的情况,并按照日期排序,字段包括日期(day),星星数量(stars)。
输出结果如下。

    +------+----------------+
    | day  | starts         |
    +------+----------------+
    |    1 | ******         |
    |    2 | **********     |
    |    3 | ******         |
    |    4 | ***            |
    |    5 | **********     |
    |    6 | ********       |
    |    7 | ******         |
    |    8 | ******         |
    |    9 | ***********    |
    |   10 | *****          |
    |   11 | ******         |
    |   12 | *******        |
    |   13 | ***            |
    |   14 | *******        |
    |   15 | *******        |
    |   16 | **********     |
    |   17 | *******        |
    |   18 | ****           |
    |   19 | *************  |
    |   20 | ******         |
    |   21 | ************** |
    |   22 | **********     |
    |   23 | ******         |
    |   24 | *******        |
    |   25 | *****          |
    |   26 | ***********    |
    |   27 | *************  |
    |   28 | *******        |
    |   29 | *********      |
    |   30 | ********       |
    |   31 | *****          |
    +------+----------------+

6、统计开发部(Development)在2000年1月份,不是工作日(周一到周五)员工离职的情况,并按照周一到周五排序,字段包括日期(dayname),星星数量(count)。
输出结果如下。

	        +-----------+-------+
	        | dayname   | count |
	        +-----------+-------+
	        | Monday    |    30 |
	        | Tuesday   |    18 |
	        | Wednesday |    41 |
	        | Thursday  |    30 |
	        | Friday    |    34 |
	        +-----------+-------+

7、对各部门的在职员工数进行排序,希望有如下所示的结果集,字段包括名次(rank),员工数量(count)。

        +--------------------+-------+
        | dept_name          | count |
        +--------------------+-------+
        | Development        | 61386 |
        | Production         | 53304 |
        | Sales              | 37701 |
        | Customer Service   | 17569 |
        | Research           | 15441 |
        | Marketing          | 14842 |
        | Quality Management | 14546 |
        | Human Resources    | 12898 |
        | Finance            | 12437 |
        +--------------------+-------+

输出结果如下。

    	+------+-------+
    	| rank | count |
    	+------+-------+
    	|    1 | 61386 |
    	|    2 | 53304 |
    	|    3 | 37701 |
    	|    4 | 17569 |
    	|    5 | 15441 |
    	|    6 | 14842 |
    	|    7 | 14546 |
    	|    8 | 12898 |
    	|    9 | 12437 |
    	+------+-------+

8、对各部门的在职员工数进行横向排序,希望有如下所示的结果集,字段包括名次(rank1-rank9)。

    	+------+-------+
    	| rank | count |
    	+------+-------+
    	|    1 | 61386 |
    	|    2 | 53304 |
    	|    3 | 37701 |
    	|    4 | 17569 |
    	|    5 | 15441 |
    	|    6 | 14842 |
    	|    7 | 14546 |
    	|    8 | 12898 |
    	|    9 | 12437 |
    	+------+-------+

输出结果如下。

    +-------+-------+-------+-------+-------+-------+-------+-------+-------+
    | rank1 | rank2 | rank3 | rank4 | rank5 | rank6 | rank7 | rank8 | rank9 |
    +-------+-------+-------+-------+-------+-------+-------+-------+-------+
    | 61386 | 53304 | 37701 | 17569 | 15441 | 14842 | 14546 | 12898 | 12437 |
    +-------+-------+-------+-------+-------+-------+-------+-------+-------+

9、返回薪水在150000以上的在职(to_date=‘9999-01-01’)员工信息,字段包括员工编号(emp_no),员工全名(name),性别(gender),生日(birth_date),入职日期(hire_date)。
输出结果如下。

    +--------+-------------------+--------+------------+------------+
    | emp_no | name              | gender | birth_date | hire_date  |
    +--------+-------------------+--------+------------+------------+
    |  43624 | Tokuyasu Pesch    | M      | 1953-11-14 | 1985-03-26 |
    |  46439 | Ibibia Junet      | M      | 1953-01-31 | 1985-05-20 |
    |  47978 | Xiahua Whitcomb   | M      | 1956-03-24 | 1985-07-18 |
    |  66793 | Lansing Kambil    | M      | 1964-05-15 | 1985-06-20 |
    |  80823 | Willard Baca      | M      | 1963-01-21 | 1985-02-26 |
    | 109334 | Tsutomu Alameldin | M      | 1955-08-02 | 1985-02-15 |
    | 205000 | Charmane Griswold | M      | 1956-01-14 | 1990-06-23 |
    | 237542 | Weicheng Hatcliff | F      | 1954-10-05 | 1985-04-12 |
    | 238117 | Mitsuyuki Stanfel | M      | 1959-06-21 | 1988-01-03 |
    | 253939 | Sanjai Luders     | M      | 1957-12-03 | 1987-04-15 |
    | 254466 | Honesty Mukaidono | M      | 1963-05-27 | 1986-08-08 |
    | 266526 | Weijing Chenoweth | F      | 1957-02-14 | 1986-10-08 |
    | 276633 | Shin Birdsall     | M      | 1954-01-27 | 1987-10-08 |
    | 279776 | Mohammed Moehrke  | M      | 1955-05-06 | 1986-06-10 |
    | 493158 | Lidong Meriste    | M      | 1961-05-20 | 1987-05-09 |
    +--------+-------------------+--------+------------+------------+

10、查找各个部门当前(to_date=‘9999-01-01’)领导当前薪水详情,字段包括部门编号(dept_no),员工编号(emp_no),员工名称(last_name),薪水(salary)。
输出结果如下。

    +---------+--------+------------+--------+
    | dept_no | emp_no | last_name  | salary |
    +---------+--------+------------+--------+
    | d001    | 110039 | Minakawa   | 106491 |
    | d002    | 110114 | Legleitner |  83457 |
    | d003    | 110228 | Sigstam    |  65400 |
    | d004    | 110420 | Ghazalie   |  56654 |
    | d005    | 110567 | DasSarma   |  74510 |
    | d006    | 110854 | Pesch      |  72876 |
    | d007    | 111133 | Zhang      | 101987 |
    | d008    | 111534 | Kambil     |  79393 |
    | d009    | 111939 | Weedman    |  58745 |
    +---------+--------+------------+--------+

11、返回员工入职年龄在25(包含25)岁以下的1990年以后入职的在职员工信息,字段包括员工编号(emp_no),员工名称(last_name),员工生日(birth_date),员工入职日期(hire_date)。
输出结果如下。

    +--------+-----------+------------+------------+
    | emp_no | last_name | birth_date | hire_date  |
    +--------+-----------+------------+------------+
    |  60875 | Masaki    | 1965-01-21 | 1990-01-14 |
    |  74200 | Weiland   | 1965-01-16 | 1990-01-02 |
    |  79171 | Ellozy    | 1965-01-19 | 1990-01-14 |
    |  82098 | Wilharm   | 1965-01-25 | 1990-01-18 |
    | 218100 | Garnham   | 1965-01-24 | 1990-01-02 |
    | 295278 | Barvinok  | 1965-01-04 | 1990-01-02 |
    +--------+-----------+------------+------------+

12、返回first_name,last_name首尾字母都相同的在职员工 ,字段包括员工全名name。
输出结果如下。

    +--------------------------+
    | name                     |
    +--------------------------+
    | Kirk Kalsbeek            |
    | Hinrich Herath           |
    | Georg Guting             |
    | Snehasis Swiss           |
    | Snehasis Servieres       |
    | Eldridge Eldridge        |
    | Szabolcs Salinas         |
    | Snehasis Smeets          |
    | Nidapan Nicolson         |
    | Aria Ariola              |
    | Stabislas Stassinopoulos |
    | Szabolcs Salinas         |
    | Alenka Aloia             |
    | Almudena Aloia           |
    | Eldridge Erde            |
    | Abdulla Akiyama          |
    | Rutger Ratnakar          |
    | Neven Naudin             |
    | Snehasis Swiss           |
    | Ebbe Etalle              |
    | Sreenivas Slaats         |
    | Arra Akiyama             |
    | Snehasis Stiles          |
    | Surveyors Staylopatis    |
    | Guoxiang Guting          |
    | Sreenivas Sullins        |
    | Nitsan Nollmann          |
    | Nitsan Naudin            |
    | Elrique Erbe             |
    | Snehasis Sifakis         |
    | Nakhoon Nilsson          |
    | Surveyors Salinas        |
    | Stabislas Stranks        |
    | Hugh Harbusch            |
    | Ravishankar Ravishankar  |
    | Alenka Azuma             |
    | Sreenivas Sullins        |
    | Kirk Koblick             |
    | Snehasis Swiss           |
    | Sreenivas Stranks        |
    | Navin Neiman             |
    +--------------------------+

13、返回头衔为Engineer的1990年以后入职薪水在40000(包括40000)元以下的在职员工 ,字段包括员工编号(emp_no),员工名称(last_name),员工头衔(title)。
输出结果如下。

    +--------+----------------+--------------------+
    | emp_no | last_name      | title              |
    +--------+----------------+--------------------+
    |  23987 | Gladwell       | Engineer           |
    |  34145 | Rothe          | Assistant Engineer |
    |  39498 | Borstler       | Engineer           |
    |  44230 | Angel          | Engineer           |
    |  45931 | Swick          | Senior Engineer    |
    |  47097 | Ambroszkiewicz | Engineer           |
    |  63966 | Kolinko        | Senior Engineer    |
    |  65936 | Barbanera      | Senior Engineer    |
    |  67332 | Honglei        | Engineer           |
    |  73372 | Miculan        | Engineer           |
    |  80271 | Zobel          | Engineer           |
    |  85682 | Turnbull       | Senior Engineer    |
    |  98626 | Emmerich       | Engineer           |
    | 105604 | Esposito       | Engineer           |
    | 108687 | Rassart        | Engineer           |
    | 108824 | Pileggi        | Engineer           |
    | 109200 | Budinsky       | Engineer           |
    | 201690 | Valtorta       | Engineer           |
    | 201768 | Muhling        | Assistant Engineer |
    | 209653 | Kohling        | Engineer           |
    | 212818 | Greenaway      | Engineer           |
    | 228121 | Reutenauer     | Engineer           |
    | 230890 | Schmiedel      | Engineer           |
    | 240474 | Malabarba      | Engineer           |
    | 244413 | Jeansoulin     | Engineer           |
    | 247851 | Vidya          | Assistant Engineer |
    | 250114 | Wendorf        | Engineer           |
    | 250516 | Baig           | Engineer           |
    | 251123 | Argence        | Senior Engineer    |
    | 276851 | McFarlin       | Engineer           |
    | 291280 | Yoshimura      | Engineer           |
    | 292755 | Perelgut       | Engineer           |
    | 293851 | Callaway       | Assistant Engineer |
    | 297603 | Baak           | Assistant Engineer |
    | 401786 | Nicolson       | Engineer           |
    | 408129 | Engelmann      | Engineer           |
    | 408667 | Hiroyama       | Engineer           |
    | 420978 | Wolniewicz     | Senior Engineer    |
    | 433565 | Kranzdorf      | Senior Engineer    |
    | 444557 | Tsukuda        | Senior Engineer    |
    | 456636 | Pettey         | Engineer           |
    | 459496 | Gaughan        | Senior Engineer    |
    | 463145 | Zweizig        | Senior Engineer    |
    | 463548 | Huhdanpaa      | Engineer           |
    | 464323 | Curless        | Engineer           |
    | 466226 | Talmor         | Engineer           |
    | 467021 | Kruskal        | Engineer           |
    | 473398 | Ouhyoung       | Engineer           |
    | 474751 | Stassinopoulos | Engineer           |
    | 475590 | Jiafu          | Engineer           |
    | 479802 | Kalsbeek       | Engineer           |
    | 499891 | Weedman        | Senior Engineer    |
    +--------+----------------+--------------------+

14、返回人力资源部(Human Resources)最年轻的男(M)性在职员工,字段包括部门名称员工编号(emp_no),员工名称(last_name),部门名称(dept_name),性别(gender),生日(birth_date)。
输出结果如下。

    +--------+-----------+-----------------+--------+------------+
    | emp_no | last_name | dept_name       | gender | birth_date |
    +--------+-----------+-----------------+--------+------------+
    |  65308 | Pocchiola | Human Resources | M      | 1952-02-01 |
    +--------+-----------+-----------------+--------+------------+

15、将员工分成若干个组,一个组最多8人,结果显示如下,字段包括组号(grp),员工编号(emp_no),员工名称(last_name)。

注意:解答此题前先执行以下语句:
DROP TABLE if exists employees.temp_employees ;
CREATE TABLE if not exists employees.temp_employees AS SELECT e.emp_no, e.last_name FROM
employees e
ORDER BY e.emp_no
LIMIT 32;

    表:temp_employees
    +--------+-------------+
    | emp_no | last_name   |
    +--------+-------------+
    |  10001 | Facello     |
    |  10002 | Simmel      |
    |  10003 | Bamford     |
    |  10004 | Koblick     |
    |  10005 | Maliniak    |
    |  10006 | Preusig     |
    |  10007 | Zielinski   |
    |  10008 | Kalloufi    |
    |  10009 | Peac        |
    |  10010 | Piveteau    |
    |  10011 | Sluis       |
    |  10012 | Bridgland   |
    |  10013 | Terkki      |
    |  10014 | Genin       |
    |  10015 | Nooteboom   |
    |  10016 | Cappelletti |
    |  10017 | Bouloucos   |
    |  10018 | Peha        |
    |  10019 | Haddadi     |
    |  10020 | Warwick     |
    |  10021 | Erde        |
    |  10022 | Famili      |
    |  10023 | Montemayor  |
    |  10024 | Pettey      |
    |  10025 | Heyers      |
    |  10026 | Berztiss    |
    |  10027 | Reistad     |
    |  10028 | Tempesti    |
    |  10029 | Herbst      |
    |  10030 | Demeyer     |
    |  10031 | Joslin      |
    |  10032 | Reistad     |
    +--------+-------------+

输出结果如下。

    +------+--------+-------------+
    | grp  | emp_no | last_name   |
    +------+--------+-------------+
    |    1 |  10028 | Tempesti    |
    |    1 |  10032 | Reistad     |
    |    1 |  10025 | Heyers      |
    |    1 |  10029 | Herbst      |
    |    1 |  10026 | Berztiss    |
    |    1 |  10030 | Demeyer     |
    |    1 |  10027 | Reistad     |
    |    1 |  10031 | Joslin      |
    |    2 |  10020 | Warwick     |
    |    2 |  10024 | Pettey      |
    |    2 |  10017 | Bouloucos   |
    |    2 |  10021 | Erde        |
    |    2 |  10018 | Peha        |
    |    2 |  10022 | Famili      |
    |    2 |  10019 | Haddadi     |
    |    2 |  10023 | Montemayor  |
    |    3 |  10012 | Bridgland   |
    |    3 |  10016 | Cappelletti |
    |    3 |  10009 | Peac        |
    |    3 |  10013 | Terkki      |
    |    3 |  10010 | Piveteau    |
    |    3 |  10014 | Genin       |
    |    3 |  10011 | Sluis       |
    |    3 |  10015 | Nooteboom   |
    |    4 |  10004 | Koblick     |
    |    4 |  10008 | Kalloufi    |
    |    4 |  10001 | Facello     |
    |    4 |  10005 | Maliniak    |
    |    4 |  10002 | Simmel      |
    |    4 |  10006 | Preusig     |
    |    4 |  10003 | Bamford     |
    |    4 |  10007 | Zielinski   |
    +------+--------+-------------+

16、请以当前日期为基准(2020-07-04)分别求出员工编号为10134,10200,10425,10434,10449,10459,10466,10532的任职情况,字段包括员工编号(emp_no),入职日期(from_date),离职日期(to_date),工作年限(years),状态(status)。
输出结果如下。

    +--------+---------+------------+------------+-------+--------+
    | emp_no | dept_no | from_date  | to_date    | years | status |
    +--------+---------+------------+------------+-------+--------+
    |  10134 | d004    | 1993-12-02 | 9999-01-01 |    26 | on     |
    |  10134 | d005    | 1987-12-12 | 1993-12-02 |     5 | off    |
    |  10425 | d004    | 1995-05-07 | 2002-05-06 |     6 | off    |
    |  10434 | d004    | 1995-06-27 | 2002-06-22 |     6 | off    |
    |  10449 | d001    | 1996-03-12 | 9999-01-01 |    24 | on     |
    |  10449 | d007    | 1987-03-28 | 1996-03-12 |     8 | off    |
    |  10459 | d002    | 1998-06-14 | 9999-01-01 |    22 | on     |
    |  10459 | d003    | 1997-06-28 | 1998-06-14 |     0 | off    |
    |  10466 | d009    | 1997-08-22 | 2000-08-15 |     2 | off    |
    +--------+---------+------------+------------+-------+--------+

17、公司规定,新员工必须要进行1次岗前培训,培训是1个月2次,分别为每个月的第1个星期五和最后1个星期五,
请给以下员工编号为10019,10298,10684,11325,11697,11754,11829,12015,12299,12389,15368,16166的员工安排培训日期,字段包括员工编号(emp_no),入职日期(from_date),培训日期(edu_date)。
希望得到以下结果,你安排对了吗?
输出结果如下。

    +--------+------------+------------+
    | emp_no | hire_date  | edu_date   |
    +--------+------------+------------+
    |  10019 | 1999-04-30 | 1999-04-30 |
    |  10298 | 1999-03-30 | 1999-04-02 |
    |  10684 | 1999-10-28 | 1999-10-29 |
    |  11325 | 1999-01-10 | 1999-01-29 |
    |  11697 | 1999-11-06 | 1999-11-26 |
    |  11754 | 1999-08-23 | 1999-08-27 |
    |  11829 | 1999-02-17 | 1999-02-26 |
    |  12015 | 1999-01-04 | 1999-01-29 |
    |  12299 | 1999-01-24 | 1999-01-29 |
    |  12389 | 1999-10-08 | 1999-10-29 |
    |  15368 | 1999-04-01 | 1999-04-02 |
    |  16166 | 1999-03-04 | 1999-03-05 |
    +--------+------------+------------+

18、请对以下离职员(10008,10011,10021,10025,10028)工薪资变动情况做一下统计,希望得到如下结果,字段包括员工编号(emp_no),开始日期(from_date),结束日期(to_date)。

    +--------+--------+------------+------------+
    | emp_no | salary | from_date  | to_date    |
    +--------+--------+------------+------------+
    |  10008 |  46671 | 1998-03-11 | 1999-03-11 |
    |  10008 |  48584 | 1999-03-11 | 2000-03-10 |
    |  10008 |  52668 | 2000-03-10 | 2000-07-31 |
    |  10011 |  42365 | 1990-01-22 | 1991-01-22 |
    |  10011 |  44200 | 1991-01-22 | 1992-01-22 |
    |  10011 |  48214 | 1992-01-22 | 1993-01-21 |
    |  10011 |  50927 | 1993-01-21 | 1994-01-21 |
    |  10011 |  51470 | 1994-01-21 | 1995-01-21 |
    |  10011 |  54545 | 1995-01-21 | 1996-01-21 |
    |  10011 |  56753 | 1996-01-21 | 1996-11-09 |
    |  10021 |  55025 | 1988-02-10 | 1989-02-09 |
    |  10021 |  56399 | 1989-02-09 | 1990-02-09 |
    |  10021 |  59700 | 1990-02-09 | 1991-02-09 |
    |  10021 |  60851 | 1991-02-09 | 1992-02-09 |
    |  10021 |  61117 | 1992-02-09 | 1993-02-08 |
    |  10021 |  60708 | 1993-02-08 | 1994-02-08 |
    |  10021 |  63514 | 1994-02-08 | 1995-02-08 |
    |  10021 |  66249 | 1995-02-08 | 1996-02-08 |
    |  10021 |  70570 | 1996-02-08 | 1997-02-07 |
    |  10021 |  74759 | 1997-02-07 | 1998-02-07 |
    |  10021 |  77519 | 1998-02-07 | 1999-02-07 |
    |  10021 |  77237 | 1999-02-07 | 2000-02-07 |
    |  10021 |  79631 | 2000-02-07 | 2001-02-06 |
    |  10021 |  82295 | 2001-02-06 | 2002-02-06 |
    |  10021 |  84169 | 2002-02-06 | 2002-07-15 |
    |  10025 |  40000 | 1987-08-17 | 1988-08-16 |
    |  10025 |  44416 | 1988-08-16 | 1989-08-16 |
    |  10025 |  48680 | 1989-08-16 | 1990-08-16 |
    |  10025 |  50120 | 1990-08-16 | 1991-08-16 |
    |  10025 |  50980 | 1991-08-16 | 1992-08-15 |
    |  10025 |  54459 | 1992-08-15 | 1993-08-15 |
    |  10025 |  54395 | 1993-08-15 | 1994-08-15 |
    |  10025 |  56643 | 1994-08-15 | 1995-08-15 |
    |  10025 |  57585 | 1995-08-15 | 1996-08-14 |
    |  10025 |  57110 | 1996-08-14 | 1997-08-14 |
    |  10025 |  57157 | 1997-08-14 | 1997-10-15 |
    |  10028 |  48859 | 1991-10-22 | 1992-10-21 |
    |  10028 |  50805 | 1992-10-21 | 1993-10-21 |
    |  10028 |  52082 | 1993-10-21 | 1994-10-21 |
    |  10028 |  54949 | 1994-10-21 | 1995-10-21 |
    |  10028 |  55963 | 1995-10-21 | 1996-10-20 |
    |  10028 |  57831 | 1996-10-20 | 1997-10-20 |
    |  10028 |  58502 | 1997-10-20 | 1998-04-06 |
    +--------+--------+------------+------------+

输出结果如下。

    +--------+------------+------------+
    | emp_no | from_date  | to_date    |
    +--------+------------+------------+
    |  10008 | 1998-03-11 | 2000-07-31 |
    |  10011 | 1990-01-22 | 1996-11-09 |
    |  10021 | 1988-02-10 | 2002-07-15 |
    |  10025 | 1987-08-17 | 1997-10-15 |
    |  10028 | 1991-10-22 | 1998-04-06 |
    +--------+------------+------------+

你可能感兴趣的:(MySQL)