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,'财务'),
(102,'销售'),
(103,'IT技术'),
(104,'行政');
insert into emp values
(1789,'张三',35,'1980/1/1',4000,101),
(1674,'李四',32,'1983/4/1',3500,101),
(1776,'王五',24,'1990/7/1',2000,101),
(1568,'赵六',57,'1970/10/11',7500,102),
(1564,'荣七',64,'1963/10/11',8500,102),
(1879,'牛八',55,'1971/10/20',7300,103);
1. Find the name of the oldest employee in the sales department
SELECT name,dept_name,age from dept,emp WHERE dept1=dept2 AND dept_name='销售' and age=(SELECT max(age) from emp);
2. Find the name of the employee with the minimum salary in the financial department
SELECT name,incoming from dept,emp WHERE dept1=dept2 AND dept_name='财务' AND incoming =(SELECT min(incoming) FROM emp );
3. List the names of the departments whose total income of each department is higher than 9000
select dept_name,SUM(incoming) from dept,emp WHERE dept1=dept2 GROUP BY dept_name HAVING sum(incoming)>9000;
4. Ask for the salary between 7500 and 8500 yuan, the name and department of the oldest person
SELECT name,dept_name,age,incoming from dept,emp where dept1=dept2 AND (incoming BETWEEN 7500 AND 8500) AND age =(SELECT max(age) from emp);
5. Find out when the lowest-paid employees in the sales department joined
SELECT name,worktime_start,min(incoming) from dept,emp where dept1=dept2 and dept_name='销售';
6. Names of employees in the financial department whose income exceeds 2,000 yuan
select name,dept_name,incoming from emp,dept where dept1=dept2 and dept_name='财务' and incoming>2000;
7. List the average income of each department and the name of the department
select dept_name,AVG(incoming) from dept,emp WHERE dept1=dept2 GROUP BY dept_name;
8. The employee number of the employee who joined the IT technology department
select sid,dept_name from emp,dept where dept1=dept2 AND dept_name='IT技术';
9. The sum of the income of the financial department;
SELECT dept_name,SUM(incoming) from emp,dept where dept1=dept2 and dept_name='财务';
10. First sort by the size of the department number, and then sort the employee information table from early to late according to the entry time
select * from dept left JOIN emp ON dept1=dept2 ORDER BY dept1 DESC,worktime_start asc;
11. Find out which department has no employees yet;
select dept_name,name from dept LEFT JOIN emp on dept1=dept2 WHERE name is NULL;
12. List the department numbers and department names of department employees whose income is greater than 7000;
SELECT dept1,dept_name,incoming FROM emp,dept WHERE dept1=dept2 and incoming>7000;
13. List the total employee income and department name of each department;
SELECT dept_name,SUM(incoming) from dept,emp WHERE dept1=dept2 GROUP BY dept_name;
14. List the name and department name of the oldest employee in each department;
SELECT d.dept_name, e.name FROM dept d LEFT JOIN emp e ON d.dept1 = e.dept2 WHERE e.age = ( SELECT MAX(age) FROM emp e2 WHERE e2.dept2 = d.dept1 );
15. Find Li Si's income and department name
select name,incoming,dept_name from emp,dept WHERE dept1=dept2 and name='李四';
16. List the names of employees with the highest income in each department, department name, income, and in descending order of income
select e.name,d.dept_name,e.incoming from dept d left join emp e on d.dept1=e.dept2 where e.incoming=(select max(incoming) FROM emp e1 WHERE d.dept1=e1.dept2) ORDER BY e.incoming desc;
17. List the names of departments with more than 1 employee
SELECT dept_name,count(*) from dept d INNER JOIN emp e on d.dept1=e.dept2 GROUP BY dept_name HAVING count(*)>1;
18. Find the name of Zhang San's department
select name,dept_name from emp INNER JOIN dept where dept1=dept2 and name='张三';