Single table and multi-table join query practice

1. Single table query practice
/* material
CREATE TABLE emp (
empno int(4) NOT NULL,
ename varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
job varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
mgr int(4) NULL DEFAULT NULL,
hiredate date NOT NULL,
sai int(255) NOT NULL,
comm int(255) NULL DEFAULT NULL,
deptno int(2) NOT NULL,
PRIMARY KEY ( empno ) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO emp VALUES (1001, ' Ganning ', ' clerk ', 1013, '2000-12-17', 8000, NULL, 20);
INSERT INTO emp VALUES (1002, ' Daikis ', ' Salesperson ', 1006, '2001-02-20', 16000, 3000, 30);
INSERT INTO emp VALUES (1003, ' Yin Tianzheng ', ' Salesperson ', 1006, '2001-02-22', 12500, 5000, 30);
INSERT INTO emp VALUES (1004, ' Liu Bei ', ' Manager ', 1009, '2001-04-02', 29750, NULL, 20);
INSERT INTO emp VALUES (1005, ' Xie Xun ', ' Salesman ', 1006, '2001-09-28', 12500, 14000, 30);
INSERT INTO emp VALUES (1006, ' Guan Yu ', ' Manager ', 1009, '2001-05-01', 28500, NULL, 30);
INSERT INTO emp VALUES (1007, ' Zhang Fei ', ' Manager ', 1009, '2001-09-01', 24500, NULL, 10);
INSERT INTO emp VALUES (1008, ' Zhuge Liang ', ' Analyst ', 1004, '2007-04-19', 30000, NULL, 20);
INSERT INTO emp VALUES (1009, ' Zeng A Niu ', ' Chairman ', NULL, '2001-11-17', 50000, NULL, 10);
INSERT INTO emp VALUES (1010, ' Wei Yixiao ', ' Salesman ', 1006, '2001-09-08', 15000, 0, 30);
INSERT INTO emp VALUES (1011, ' Zhou Tai ', ' clerk ', 1006, '2007-05-23', 11000, NULL, 20);
INSERT INTO emp VALUES (1012, ' Cheng Pu ', ' clerk ', 1006, '2001-12-03', 9500, NULL, 30);
INSERT INTO emp VALUES (1013, ' Pang Tong ', ' Analyst ', 1004, '2001-12-03', 30000, NULL, 20);
INSERT INTO emp VALUES (1014, ' yellow cover ', ' clerk ', 1007, '2002-01-23', 13000, NULL, 10);
INSERT INTO emp VALUES (1015, ' Zhang San ', ' cleaner ', 1001, '2013-05-01', 80000, 50000, 50);
1. Query all employees whose department number is 30
select * from emp where deptno = 30;
2. Names, numbers and department numbers of all salespersons.
 select ename,empno,mgr from emp;
3. Find employees whose bonus is higher than salary.
 select * from emp where sai < comm;
4. Find employees whose bonus is 60% higher than their salary.
 select * from emp where sai < (comm * 0.6);
5. Find out the details of all managers in department number 10 and all salesmen in department number 20.
 select * from emp where deptno in (10,20) and job in ('manager','salesperson');
6. Find all managers in department number 10, all salespersons in department number 20, and Details of all employees who are neither managers nor salespersons but whose salary is greater than or equal to 20000.
 select * from emp where (deptno = 10 and job = 'Manager') or (deptno = 20 and job = 'Sales') or (sai >= 20000 and job != 'Manager' and job !='Sales' );
7. Employees with no bonus or bonus less than 1000.
 select * from emp where comm is null or comm < 1000;
8. Query employees whose names consist of three characters.
select * from emp where length(ename) = 9;  
9. Query the employees who joined in 2000.
select * from emp where year(hiredate)=2000;
10. Query the detailed information of all employees and sort them in ascending order by number
 select * from emp order by empno;
11. Query the detailed information of all employees and sort them by salary in descending order. If the salary is the same, use entry Date ascending order
 select * from emp order by sai desc,hiredate;
12. Query the average salary of each department
select deptno,avg(sai) as total from emp group by deptno;
13. Query the number of employees in each department
 select deptno, count(job) as total from emp group by deptno;
14. Query the maximum wage, minimum wage and number of people for each job
select job,max(sai),min(sai),count(job) from emp group by job;
2. Multi-table join query
use mydb3;
--Create department table
create table if not exists dept3(
deptno varchar(20) primary key , -- department number
name varchar(20) -- department name
);
--Create employee table
create table if not exists emp3(
eid varchar(20) primary key , --employee number
ename varchar(20), --employee name
age int, --employee age
dept_id varchar(20) --Employee 's department
);
--Add data to dept3 table
insert into dept3 values('1001',' R&D department ');
insert into dept3 values('1002',' Sales Department ');
insert into dept3 values('1003',' Finance Department ');
insert into dept3 values('1004',' HR ');
--Add data to emp3 table
insert into emp3 values('1',' Qiao Feng ',20, '1001');
insert into emp3 values('2',' Duanyu ',21,'1001');
insert into emp3 values('3',' Xuzhu ',23,'1001');
insert into emp3 values('4',' Azi ',18,'1001');
insert into emp3 values('5',' sweeping monk ',85, '1002');
insert into emp3 values('6',' Li Qiushui ',33,'1002');
insert into emp3 values('7',' Jumozhi ',50, '1002');
insert into emp3 values('8',' Tianshan Child Elder ',60, '1003');
insert into emp3 values('9',' Murongbo ',58,'1003');
insert into emp3 values('10',' Ding Chunqiu ',71, '1005');
1 Query the employees of each department
 select name,GROUP_CONCAT(ename) from emp3 e RIGHT JOIN dept3 d on e.dept_id = d.deptno GROUP BY name ;
2 Query the employees of the R&D department
select name,GROUP_CONCAT(ename) from emp3 e RIGHT JOIN dept3 d on e.dept_id = d.deptno and name='R&D department';
3. Query the employees of R&D department and sales department
select name, GROUP_CONCAT(ename) from emp3 e RIGHT JOIN dept3 d on e.dept_id = d.deptno group by name and d.name='R&D Department' or d.name='Sales Department' ;
4. Query the number of employees in each department and sort them in ascending order
select name,count(deptno) as total from emp3 e left JOIN dept3 d on e.dept_id = d.deptno GROUP BY d.name HAVING NAME is not null ORDER BY total;
5. Query departments with a number greater than or equal to 3, and sort them in descending order according to the number of people
select name,count(deptno) as total from emp3 e left JOIN dept3 d on  e.dept_id = d.deptno GROUP BY d.name  HAVING NAME is not null and total > 1 ORDER BY total desc 

Add new employee table emp and department table dept
 create table dept (dept1 int ,dept_name varchar(11));
 create table emp (sid int ,name varchar(11),age int,worktime_start date,incoming int,dept2 int);

     insert into dept values
    ​​(101,'finance'),
    (102,'sales'),
    (103,'IT technology'),
    (104,'administration');

     insert into emp values
    ​​(1789,'Zhang San',35,'1980/1/1',4000,101),
    (1674,'Li Si',32,'1983/4/1',3500,101),
    (1776,'Wang Wu',24,'1990/7/1',2000,101),
    (1568,'Zhao Liu',57,'1970/10/11',7500,102),
    (1564,' Rongqi',64,'1963/10/11',8500,102),
    (1879,'Niuba',55,'1971/10/20',7300,103);

1. Find out the name of the oldest employee in the sales department
select name from emp e ,dept d where e.dept2=d.dept1 and dept_name='sales' and age=(select max(age) from emp where dept2=( select dept1 from dept where dept_name='sales'));
 2. Find the name of the employee with the minimum salary in the financial department
select name from emp e ,dept d where e.dept2=d.dept1 and dept_name='finance' and incoming=(
select min(incoming) from emp where dept2=(select dept1 from dept where dept_name='service'));

 3. List the names of departments whose total income of each department is higher than 9000
select dept_name from dept d,(select dept2 from emp group by dept2 having sum(incoming)> 9000 ) e where d.dept1=e.dept2;
 4. Ask for the salary between 7500 and 8500 yuan, the name and department of the oldest person
select dept_name, `name` from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE incoming >= 7500 AND incoming <= 8500 and d.dept_name = 'sales' ORDER BY age desc LIMIT 1;
5. Find the entry time of the employee with the lowest income in the sales department 
SELECT worktime_start FROM emp e RIGHT JOIN dept d ON e.dept2 = d.dept1 where d.dept_name = 'Sales' ORDER BY incoming ASC LIMIT 1;
6. The income of the financial department exceeds Select name
from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where d.dept_name = 'finance' and incoming > 2000;
7. List the average income and department name of each department
select dept_name ,AVg(incoming) from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name;
8.
Select sid from emp e RIGHT JOIN dept d on e.dept2 = d. dept1 where d.dept_name = 'IT technology'
9. The sum of the financial department's income;
select dept_name, sum(incoming) from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE d.dept_name = 'finance' ;
10 .Sort by department number first, and then sort the employee information table from early to late according to the entry time
select * from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 ORDER BY dept2,worktime_start;
11. Find out which department has no employees yet;
select dept_name from emp e RIGHT JOIN dept d on e.dept2 = d. dept1 WHERE worktime_start is NULL;
12. List the department number and department name of department employees whose income is greater than 7000;
select dept1, dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 WHERE incoming > 7000;
13. List Total employee income and department name of each department;
select dept_name,sum(incoming) from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name

14. List the name and department name of the oldest employee in each department;
select MAX(age), dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name
15. Seek Li Si's income and Department name
select `name`,incoming,dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where name = 'Li Si'
16. List the names of the employees with the highest income in each department, department name, income , and select name,MAX(incoming),dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name in descending order of income ;

17. List the names of departments with more than 1 employees
select dept_name, dept2 from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 group by dept_name HAVING COUNT(dept2) > 1; 19.
Find where Zhang San is located Department name
select dept_name from emp e RIGHT JOIN dept d on e.dept2 = d.dept1 where name = 'Zhang San'

Guess you like

Origin blog.csdn.net/m0_70940822/article/details/131651369