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;
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.
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)