MySQL query job

homework topic

Please add a picture description

Answer

Preparation

  • Data table creation
CREATE TABLE worker (
部门号 int(11) NOT NULL,
职工号 int(11) NOT NULL PRIMARY KEY,
工作时间 date NOT NULL,
工资 float(8,2) NOT NULL,
政治面貌 varchar(10) NOT NULL DEFAULT '群众',
姓名 varchar(20) NOT NULL,
出生日期 date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;
  • insert data
INSERT INTO worker (部门号,职工号,工作时间,工资,政治面貌,姓名,出生日期) VALUES (101, 1001, '2015-5-4', 3500.00, '群众', '张三', '1990-7-1'),(101, 1002, '2017-2-6', 3200.00, '团员', '李四', '1997-2-8'),(102, 1003, '2011-1-4', 8500.00, '党员', '王亮', '1983-6-8'),(102, 1004, '2016-10-10', 5500.00, '群众', '赵六', '1994-9-5'),(102, 1005, '2014-4-1', 4800.00, '党员', '钱七', '1992-12-30'),(102, 1006, '2017-5-5', 4500.00, '党员', '孙八', '1996-9-2');
  • view worker table
mysql> select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   | 出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)

Answer

  • Display the basic information of all employees.
mysql> select * from worker;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   | 出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
  • Query the department numbers of the departments to which all employees belong, and do not display duplicate department numbers.
mysql> select 部门号,count(部门号) as 人数  from worker group by 部门号;
+-----------+--------+
| 部门号    | 人数   |
+-----------+--------+
|       101 |      2 |
|       102 |      4 |
+-----------+--------+
2 rows in set (0.00 sec)
  • Find the number of all employees.
mysql> select count(部门号) as 总人数 from worker;
+-----------+
| 总人数    |
+-----------+
|         6 |
+-----------+
1 row in set (0.00 sec)
  • List the highest and lowest wages.
mysql> select max(工资) as 最高工资,min(工资) as 最低工资 from worker;
+--------------+--------------+
| 最高工资     | 最低工资     |
+--------------+--------------+
|      8500.00 |      3200.00 |
+--------------+--------------+
1 row in set (0.00 sec)
  • List the average salary and total salary of the workers.
mysql> select avg(工资) as 平均工资,sum(工资) as 总工资 from worker;
+--------------+-----------+
| 平均工资     | 总工资    |
+--------------+-----------+
|  5000.000000 |  30000.00 |
+--------------+-----------+
1 row in set (0.00 sec)
  • Create a new table with only the employee number, name, and work attendance, called the work date table.
mysql> create table work_time select 职工号,姓名,工作时间 from worker;
Query OK, 6 rows affected (0.01 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> 
mysql> 
mysql> select * from work_time;
+-----------+--------+--------------+
| 职工号    | 姓名   | 工作时间     |
+-----------+--------+--------------+
|      1001 | 张三   | 2015-05-04   |
|      1002 | 李四   | 2017-02-06   |
|      1003 | 王亮   | 2011-01-04   |
|      1004 | 赵六   | 2016-10-10   |
|      1005 | 钱七   | 2014-04-01   |
|      1006 | 孙八   | 2017-05-05   |
+-----------+--------+--------------+
6 rows in set (0.00 sec)
  • List the employee number, name, and date of birth of all employees surnamed Liu.
mysql> select 职工号,姓名,出生日期 from worker where 姓名 like '刘%';
Empty set (0.00 sec)
  • List the names and dates of employment of employees born before 1960.
mysql> select 姓名,工作时间,出生日期 from worker where 出生日期 < '1960-01-01';
Empty set (0.00 sec)
  • List the names of all employees whose salary is between 1000-2000.
mysql> select 姓名,工资 from worker where 工资 between 1000 and 2000;
Empty set (0.00 sec)
  • List the names of all employees with the surnames Chen and Li.
mysql> select 姓名 from worker where 姓名 like '陈%' or 姓名 like '李%';
+--------+
| 姓名   |
+--------+
| 李四   |
+--------+
1 row in set (0.00 sec)
  • List all employee numbers, names, and party members whose department numbers are 2 and 3.
mysql> select 职工号,姓名,政治面貌 from worker where 政治面貌 = '党员' and 职工号 like '%2' or 职工号 like '%3';
+-----------+--------+--------------+
| 职工号    | 姓名   | 政治面貌     |
+-----------+--------+--------------+
|      1003 | 王亮   | 党员         |
+-----------+--------+--------------+
1 row in set (0.00 sec)
  • Sort the employees in the employee table worker in the order of birth.
mysql> select * from worker order by 出生日期;
+-----------+-----------+--------------+---------+--------------+--------+--------------+
| 部门号    | 职工号    | 工作时间     | 工资    | 政治面貌     | 姓名   | 出生日期     |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
|       102 |      1003 | 2011-01-04   | 8500.00 | 党员         | 王亮   | 1983-06-08   |
|       101 |      1001 | 2015-05-04   | 3500.00 | 群众         | 张三   | 1990-07-01   |
|       102 |      1005 | 2014-04-01   | 4800.00 | 党员         | 钱七   | 1992-12-30   |
|       102 |      1004 | 2016-10-10   | 5500.00 | 群众         | 赵六   | 1994-09-05   |
|       102 |      1006 | 2017-05-05   | 4500.00 | 党员         | 孙八   | 1996-09-02   |
|       101 |      1002 | 2017-02-06   | 3200.00 | 团员         | 李四   | 1997-02-08   |
+-----------+-----------+--------------+---------+--------------+--------+--------------+
6 rows in set (0.00 sec)
  • Display the employee numbers and names of the top 3 employees with the highest salary.
mysql> select 职工号,姓名,工资 from worker order by 工资 desc limit 3;
+-----------+--------+---------+
| 职工号    | 姓名   | 工资    |
+-----------+--------+---------+
|      1003 | 王亮   | 8500.00 |
|      1004 | 赵六   | 5500.00 |
|      1005 | 钱七   | 4800.00 |
+-----------+--------+---------+
3 rows in set (0.00 sec)
  • Find the number of party members in each department.
mysql> select count(政治面貌) as 党员总人数 from worker where 政治面貌 ='党员';
+-----------------+
| 党员总人数      |
+-----------------+
|               3 |
+-----------------+
1 row in set (0.00 sec)
  • Statistics of salary and average salary of each department
mysql> select 部门号,avg(工资) as 平均工资 from worker group by 部门号;
+-----------+--------------+
| 部门号    | 平均工资     |
+-----------+--------------+
|       101 |  3350.000000 |
|       102 |  5825.000000 |
+-----------+--------------+
2 rows in set (0.00 sec)
  • List the department numbers and the total number of people whose total number is greater than 4.
mysql> select 部门号,count(部门号)  总人数 from worker group by 部门号 having 总人数 >= 4;
+-----------+-----------+
| 部门号    | 总人数    |
+-----------+-----------+
|       102 |         4 |
+-----------+-----------+
1 row in set (0.00 sec)

Guess you like

Origin blog.csdn.net/bo1029/article/details/131625370