The third MySQL job---single table query

1. Create employee table

CREATE TABLE `worker` (
  `部门号` int NOT NULL,
  `职工号` int NOT NULL,
  `工作时间` date NOT NULL,
  `工资` decimal(8,2) NOT NULL,
  `政治面貌` varchar(10) NOT NULL DEFAULT '群众',
  `姓名` varchar(20) NOT NULL,
  `出生日期` date NOT NULL,
  PRIMARY KEY (`职工号`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

 2. Perform single-table query according to requirements

1. Display the basic information of all employees.
    select * from worker;
2. Query the department numbers of the departments to which all employees belong, and do not display duplicate department numbers.
    select distinct department number from worker;
3. Calculate the number of all employees. 
    select count (employee number) from worker;
4. List the highest and lowest wages. 
    select max(salary) the highest salary, min(salary) the minimum salary from worker;
5. List the average salary and total salary of the workers. 
    select avg(salary) average salary, sum(salary) total salary from worker;
6. Create a new table with only employee number, name and work participation, named work date table.     
    create table work_date(employee number int primary key auto_increment, name varchar(20) not null, join work varchar(30) not null )auto_increment=1001; 7. List the employee number, name
and date of birth of all employees surnamed Liu.
    Select employee number, name, date of birth from worker where name like 'Liu%';
8. List the names and working dates of employees born before 1960.
    select name, working hours from worker where year (date of birth) < 1960;
9. List the names of all employees whose salary is between 1000-2000. 
    select name from worker where salary between 1000 and 2000;
10. List the names of all employees with the surname Chen and the surname Li.
    select name from worker where name like 'Chen%' or name like 'Li%';
11. List all employee numbers, names, and party members whose department numbers are 1 and 2.  
    select department number, group_concat (employee number), group_concat (name), group_concat (political affiliation) from worker group by department number;
12. Sort the employees in the employee table worker according to the order of birth.
    select * from worker order by date of birth;
13. Display the employee number and name of the top 3 workers with the highest salary. 
    mysql8.0.33 [test]>select employee number, name from worker order by salary desc limit 0,3;
14. Calculate the number of party members in each department. 
    select department number, count (political profile) number of party members from worker where political profile = 'party member' group by department number;
15. Statistics of wages and average wages of each department
    mysql8.0.33 [test]>select department number, sum (salary) Salary, avg (salary) average salary from worker group by department number;
16. List the department numbers and the total number of people whose total number is greater than 4.
    select department number, count (employee number) total number from worker group by department number having total number> 4;
 

Guess you like

Origin blog.csdn.net/CQ17743254852/article/details/131628335