------mysql database practice questions-------
While learning the basics, it's impossible to have no practice questions. The
blogger has organized 34 database practice questions for everyone here,
hoping to help students open their minds.
Note: The statements used have been marked in the form of code blocks
Article directory
- 34 database practice questions to open your mind
-
- 1. The name of the person who obtained the highest salary in each department
- 2. Who's salary is above the department's average salary?
- 3. Obtain the average salary grade (of all people) in the department
- 5. The department number of the department with the highest average salary
- 6. The department name of the department with the highest average salary
- 7. Find the department name of the department with the lowest average salary
- 8. Get the name of the leader who is higher than the highest salary of ordinary employees (the employee code does not appear on the mgr field)
- 9. Top 5 employees with the highest salary
- 10. The sixth to tenth employees with the highest salary
- 11. Get the last 5 employees
- 12. Get how many employees in each salary grade
- 13. Interview questions:
- 14. List the names of all employees and leaders
- 15. List the numbers, names, and department names of all employees whose employment dates are earlier than their immediate superiors
- 16. List the department names and employee information of these departments, and list those departments that do not have employees
- 17. List all departments with at least 5 employees
- 18. List all employees whose salary is more than "SMITH"
- 19. List the names of all "CLERK" (clerks) and their department names, the number of people in the department
- 20. List all kinds of jobs with a minimum salary of more than 1500 and the total number of employees engaged in this job to find the minimum value by grouping of jobs.
- 21. List the names of employees who work in the department "SALES" <sales>, assuming the department number of the sales department is unknown.
- 22. List all employees whose salary is higher than the average salary of the company, their department, their superiors, and their salary grades.
- 23. List the names of all employees and departments who do the same job as "SCOTT"
- 24. List the names and salaries of other employees whose salaries are equal to those of employees in department 30.
- 25. List the names and salaries of employees whose salaries are higher than the salaries of all employees working in department 30. Department Name
- 26. List the number of employees working in each department, the average salary and the average length of service
- 27. List the names, departments and salaries of all employees.
- 28. List details and headcount of all departments
- 29. List the minimum wage for various jobs and the name of the employee doing the job
- 30. List the minimum salary of MANAGER (leader) in each department
- 31. List the annual salary of all employees, sorted by annual salary from low to high
- 32. Find out the names and leaders of employees whose salaries exceed 3,000
- 33. Find the total salary and number of employees in the department with the 'S' character in the department name
- 34. Give a 10% salary increase to employees who have served for more than 30 years.
34 database practice questions to open your mind
1. The name of the person who obtained the highest salary in each department
Step 1: Get the highest salary in each department (group by department number and find the maximum value for each group)
mysql> select deptno,max(sal) as maxsal from emp group by deptno;
±-------±--------+
| deptno | maxsal |
±-------±--------+
| 10 | 5000.00 |
| 20 | 3000.00 |
| 30 | 2850.00 |
±-------±--------+
Step 2: Treat the above query result as a temporary table t,
join t and emp table, condition: t. deptno = e.deptno and t.maxsal = e.sal
select
e.ename, t.*
from
emp e
join
(select deptno,max(sal) as maxsal from emp group by deptno) t
on
t.deptno = e.deptno and t.maxsal = e.sal;
±------±-------±--------+
| ename | deptno | maxsal |
±------±-------±--------+
| BLAKE | 30 | 2850.00 |
| SCOTT | 20 | 3000.00 |
| KING | 10 | 5000.00 |
| FORD | 20 | 3000.00 |
±------±-------±--------+
2. Who's salary is above the department's average salary?
Step 1: Find the Average Salary in Each Department
select deptno,avg(sal) as avgsal from emp group by deptno;
±-------±------------+
| deptno | avgsal |
±-------±------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
±-------±------------+
Step 2: Treat the above query results as t table, t and emp table connection
condition: the department numbers are the same, and the sal of emp is greater than the avgsal of t table
select
t.*, e.ename, e.sal
from
emp e
join
(select deptno,avg(sal) as avgsal from emp group by deptno) t
on
e.deptno = t.deptno and e.sal > t.avgsal;
±-------±------------±------±--------+
| deptno | avgsal | ename | sal |
±-------±------------±------±--------+
| 30 | 1566.666667 | ALLEN | 1600.00 |
| 20 | 2175.000000 | JONES | 2975.00 |
| 30 | 1566.666667 | BLAKE | 2850.00 |
| 20 | 2175.000000 | SCOTT | 3000.00 |
| 10 | 2916.666667 | KING | 5000.00 |
| 20 | 2175.000000 | FORD | 3000.00 |
±-------±------------±------±--------+
3. Obtain the average salary grade (of all people) in the department
Average pay grades: Calculate each pay grade first, then find the average of the pay grades.
Grades for Average Salary: Calculate the average salary first, then find the grade value for each average salary.
Step 1: Find out each person's salary grade
emp e and salgrade s table join.
Join condition: e.sal between s.losal and s.hisal
select
e.ename,e.sal,e.deptno,s.grade
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal;
±-------±--------±-------±------+
| ename | sal | deptno | grade |
±-------±--------±-------±------+
| CLARK | 2450.00 | 10 | 4 |
| KING | 5000.00 | 10 | 5 |
| MILLER | 1300.00 | 10 | 2 |
| SMITH | 800.00 | 20 | 1 |
| ADAMS | 1100.00 | 20 | 1 |
| SCOTT | 3000.00 | 20 | 4 |
| FORD | 3000.00 | 20 | 4 |
| JONES | 2975.00 | 20 | 4 |
| MARTIN | 1250.00 | 30 | 2 |
| TURNER | 1500.00 | 30 | 3 |
| BLAKE | 2850.00 | 30 | 4 |
| ALLEN | 1600.00 | 30 | 3 |
| JAMES | 950.00 | 30 | 1 |
| WARD | 1250.00 | 30 | 2 |
±-------±--------±-------±------+
Step 2: Based on the above results, continue to group by deptno and find the average of grades.
select
e.deptno,avg(s.grade)
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal
group by
e.deptno;
±-------±-------------+
| deptno | avg(s.grade) |
±-------±-------------+
| 10 | 3.6667 |
| 20 | 2.8000 |
| 30 | 2.5000 |
±-------±-------------+
4. Do not use the group function (Max) to get the highest salary
The first: sal descending, limit 1
select ename,sal from emp order by sal desc limit 1;
±------±--------+
| ename | sal |
±------±--------+
| KING | 5000.00 |
±------±--------+
Second option:
select max(sal) from emp;
The third scheme: self-joining of tables
select sal from emp where sal not in(select distinct a.sal from emp a join emp b on a.sal < b.sal);
±--------+
| sal |
±--------+
| 5000.00 |
±--------+
select
distinct a.sal
from
emp a
join
emp b
on
a.sal < b.sal
a table
±--------+
| sal |
±--------+
| 800.00 |
| 1600.00 | | 1250.00
|
| 2975.00 |
| 1250.00 |
|
2850.00
|
| 5000.00 |
| 1500.00 |
| 1100.00 |
| 950.00 |
| 3000.00 |
| 1300.00 |
±--------+
±--------+
| sal |
±--------+
| 800.00 |
| 1600.00 | | 1250.00
|
| 2975.00 |
| 1250.00 |
|
2850.00
|
| 5000.00 |
| 1500.00 |
| 1100.00 |
| 950.00 |
| 3000.00 |
| 1300.00 |
±--------+
5. The department number of the department with the highest average salary
The first solution: take the first one in descending order.
Step 1: Find the Average Salary in Each Department
select deptno,avg(sal) as avgsal from emp group by deptno;
±-------±------------+
| deptno | avgsal |
±-------±------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
±-------±------------+
Step 2: Select the first one in descending order.
select deptno,avg(sal) as avgsal from emp group by deptno order by avgsal desc limit 1;
±-------±------------+
| deptno | avgsal |
±-------±------------+
| 10 | 2916.666667 |
±-------±------------+
The second option: max
Step 1: Find the Average Salary in Each Department
select deptno,avg(sal) as avgsal from emp group by deptno;
±-------±------------+
| deptno | avgsal |
±-------±------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
±-------±------------+
Step 2: Find the largest value of avgsal in the above results.
select max(t.avgsal) from (select avg(sal) as avgsal from emp group by deptno) t;
±--------------+
| max(t.avgsal) |
±--------------+
| 2916.666667 |
±--------------+
third step:
select
deptno,avg(sal) as avgsal
from
emp
group by
deptno
having
avgsal = (select max(t.avgsal) from (select avg(sal) as avgsal from emp group by deptno) t);
±-------±------------+
| deptno | avgsal |
±-------±------------+
| 10 | 2916.666667 |
±-------±------------+
6. The department name of the department with the highest average salary
select
d.dname,avg(e.sal) as avgsal
from
emp e
join
dept d
on
e.deptno = d.deptno
group by
d.dname
order by
avgsal desc
limit
1;
±-----------±------------+
| dname | avgsal |
±-----------±------------+
| ACCOUNTING | 2916.666667 |
±-----------±------------+
7. Find the department name of the department with the lowest average salary
The average salary is 800 and the
average salary is 900
so they are both level 1.
Step 1: Find the Average Salary in Each Department
select deptno,avg(sal) as avgsal from emp group by deptno;
±-------±------------+
| deptno | avgsal |
±-------±------------+
| 10 | 2916.666667 |
| 20 | 2175.000000 |
| 30 | 1566.666667 |
±-------±------------+
Step 2: Find the grade of the average salary of each department. The
above t table is connected with the salgrade table, and the condition is: t.avgsal between s.losal and s.hisal
select
t.*,s.grade
from
(select d.dname,avg(sal) as avgsal from emp e join dept d on e.deptno = d.deptno group by d.dname) t
join
salgrade s
on
t.avgsal between s.losal and s.hisal;
±-----------±------------±------+
| dname | avgsal | grade |
±-----------±------------±------+
| SALES | 1566.666667 | 3 |
| ACCOUNTING | 2916.666667 | 4 |
| RESEARCH | 2175.000000 | 4 |
±-----------±------------±------+
select
t.*,s.grade
from
(select d.dname,avg(sal) as avgsal from emp e join dept d on e.deptno = d.deptno group by d.dname) t
join
salgrade s
on
t.avgsal between s.losal and s.hisal
where
s.grade = (select grade from salgrade where (select avg(sal) as avgsal from emp group by deptno order by avgsal asc limit 1) between losal and hisal);
±------±------------±------+
| dname | avgsal | grade |
±------±------------±------+
| SALES | 1566.666667 | 3 |
±------±------------±------+
Putting aside the previous ones, what's up with you at the lowest level?
The corresponding grade with the lowest average salary must be the lowest.
select avg(sal) as avgsal from emp group by deptno order by avgsal asc limit 1;
±------------+
| avgsal |
±------------+
| 1566.666667 |
±------------+
select grade from salgrade where (select avg(sal) as avgsal from emp group by deptno order by avgsal asc limit 1) between losal and hisal;
±------+
| grade |
±------+
| 3 |
±------+
8. Get the name of the leader who is higher than the highest salary of ordinary employees (the employee code does not appear on the mgr field)
The leader must be higher than the "highest salary of ordinary employees"!
No problem! ! ! !
mysql> select distinct mgr from emp where mgr is not null;
±-----+ |
mgr |
±-----+ |
7902 |
| 7698 |
| 7839 |
| 7566 |
| 7788 |
| All are ordinary employees.
Step 1: Find out the top salary of the average employee!
When not in is used, remember to exclude NULL in the following parentheses.
select max(sal) from emp where empno not in(select distinct mgr from emp where mgr is not null);
±---------+
| max(sal) |
±---------+
| 1600.00 |
±---------+
Step 2: Find the ones above 1600
select ename,sal from emp where sal > (select max(sal) from emp where empno not in(select distinct mgr from emp where mgr is not null));
±------±--------+
| ename | sal |
±------±--------+
| JONES | 2975.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| FORD | 3000.00 |
±------±--------+
9. Top 5 employees with the highest salary
select ename,sal from emp order by sal desc limit 5;
±------±--------+
| ename | sal |
±------±--------+
| KING | 5000.00 |
| SCOTT | 3000.00 |
| FORD | 3000.00 |
| JONES | 2975.00 |
| BLAKE | 2850.00 |
±------±--------+
10. The sixth to tenth employees with the highest salary
select ename,sal from emp order by sal desc limit 5, 5;
±-------±--------+
| ename | salt |
±-------±--------+
| CLARK | 2450.00 |
| ALL | 1600.00 |
| TURNER | 1500.00 |
| MILLER | 1300.00 |
| MARTIN | 1250.00 |
±-------±--------+
11. Get the last 5 employees
Dates can also be sorted in descending and ascending order.
select ename,hiredate from emp order by hiredate desc limit 5;
±-------±-----------+
| ename | hiredate |
±-------±-----------+
| ADAMS | 1987-05-23 |
| SCOTT | 1987-04-19 |
| MILLER | 1982-01-23 |
| FORD | 1981-12-03 |
| JAMES | 1981-12-03 |
±-------±-----------+
12. Get how many employees in each salary grade
group count
Step 1: Find out each employee's salary grade
select
e.ename,e.sal,s.grade
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal;
±-------±--------±------+
| ename | sal | grade |
±-------±--------±------+
| SMITH | 800.00 | 1 |
| ALLEN | 1600.00 | 3 |
| WARD | 1250.00 | 2 |
| JONES | 2975.00 | 4 |
| MARTIN | 1250.00 | 2 |
| BLAKE | 2850.00 | 4 |
| CLARK | 2450.00 | 4 |
| SCOTT | 3000.00 | 4 |
| KING | 5000.00 | 5 |
| TURNER | 1500.00 | 3 |
| ADAMS | 1100.00 | 1 |
| JAMES | 950.00 | 1 |
| FORD | 3000.00 | 4 |
| MILLER | 1300.00 | 2 |
±-------±--------±------+
Step 2: Continue to group statistics by grade
select
s.grade ,count(*)
from
emp e
join
salgrade s
on
e.sal between s.losal and s.hisal
group by
s.grade;
±------±---------+
| grade | count(*) |
±------±---------+
| 1 | 3 |
| 2 | 3 |
| 3 | 2 |
| 4 | 5 |
| 5 | 1 |
±------±---------+
13. Interview questions:
There are 3 tables S (student table), C (course table), SC (student course selection table)
S (SNO, SNAME) represents (student number, name)
C (CNO, CNAME, CTEACHER) represents (course number, course name) , teacher)
SC (SNO, CNO, SCGRADE) on behalf of (student number, class number, grades)
Questions:
1. Find out the names of all the students who have not chosen the teacher "Dawn".
2. List the names and average grades of students who failed in more than 2 subjects (including 2 subjects).
3. Names of all the students who have studied both the No. 1 course and No. 2 course.
14. List the names of all employees and leaders
select
a.ename '员工', b.ename '领导'
from
emp a
left join
emp b
on
a.mgr = b.empno;
±-------±------+
| 员工 | 领导 |
±-------±------+
| SMITH | FORD |
| ALLEN | BLAKE |
| WARD | BLAKE |
| JONES | KING |
| MARTIN | BLAKE |
| BLAKE | KING |
| CLARK | KING |
| SCOTT | JONES |
| KING | NULL |
| TURNER | BLAKE |
| ADAMS | SCOTT |
| JAMES | BLAKE |
| FORD | JONES |
| MILLER | CLARK |
±-------±------+
15. List the numbers, names, and department names of all employees whose employment dates are earlier than their immediate superiors
emp a employee table
emp b leader table
a.mgr = b.empno and a.hiredate < b.hiredate
select
a.ename '员工', a.hiredate, b.ename '领导', b.hiredate, d.dname
from
emp a
join
emp b
on
a.mgr = b.empno
join
dept d
on
a.deptno = d.deptno
where
a.hiredate < b.hiredate;
±------±-----------±------±-----------±-----------+
| 员工 | hiredate | 领导 | hiredate | dname |
±------±-----------±------±-----------±-----------+
| CLARK | 1981-06-09 | KING | 1981-11-17 | ACCOUNTING |
| SMITH | 1980-12-17 | FORD | 1981-12-03 | RESEARCH |
| JONES | 1981-04-02 | KING | 1981-11-17 | RESEARCH |
| ALLEN | 1981-02-20 | BLAKE | 1981-05-01 | SALES |
| WARD | 1981-02-22 | BLAKE | 1981-05-01 | SALES |
| BLAKE | 1981-05-01 | KING | 1981-11-17 | SALES |
±------±-----------±------±-----------±-----------+
16. List the department names and employee information of these departments, and list those departments that do not have employees
select
e.*,d.dname
from
emp e
right join
dept d
on
e.deptno = d.deptno;
±------±-------±----------±-----±-----------±--------±--------±-------±-----------+
| EMPNO | ENAME | JOB | MGR | HIREDATE | SAL | COMM | DEPTNO | dname |
±------±-------±----------±-----±-----------±--------±--------±-------±-----------+
| 7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450.00 | NULL | 10 | ACCOUNTING |
| 7839 | KING | PRESIDENT | NULL | 1981-11-17 | 5000.00 | NULL | 10 | ACCOUNTING |
| 7934 | MILLER | CLERK | 7782 | 1982-01-23 | 1300.00 | NULL | 10 | ACCOUNTING |
| 7369 | SMITH | CLERK | 7902 | 1980-12-17 | 800.00 | NULL | 20 | RESEARCH |
| 7566 | JONES | MANAGER | 7839 | 1981-04-02 | 2975.00 | NULL | 20 | RESEARCH |
| 7788 | SCOTT | ANALYST | 7566 | 1987-04-19 | 3000.00 | NULL | 20 | RESEARCH |
| 7876 | ADAMS | CLERK | 7788 | 1987-05-23 | 1100.00 | NULL | 20 | RESEARCH |
| 7902 | FORD | ANALYST | 7566 | 1981-12-03 | 3000.00 | NULL | 20 | RESEARCH |
| 7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600.00 | 300.00 | 30 | SALES |
| 7521 | WARD | SALESMAN | 7698 | 1981-02-22 | 1250.00 | 500.00 | 30 | SALES |
| 7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250.00 | 1400.00 | 30 | SALES |
| 7698 | BLAKE | MANAGER | 7839 | 1981-05-01 | 2850.00 | NULL | 30 | SALES |
| 7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500.00 | 0.00 | 30 | SALES |
| 7900 | JAMES | CLERK | 7698 | 1981-12-03 | 950.00 | NULL | 30 | SALES |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | OPERATIONS |
±------±-------±----------±-----±-----------±--------±--------±-------±-----------+
17. List all departments with at least 5 employees
Group by department number, count, filter out >= 5
select
deptno
from
emp
group by
deptno
having
count(*) >= 5;
±-------+
| deptno |
±-------+
| 20 |
| 30 |
±-------+
18. List all employees whose salary is more than "SMITH"
select ename,sal from emp where sal > (select sal from emp where ename = 'SMITH');
±-------±--------+
| ename | sal |
±-------±--------+
| ALLEN | 1600.00 |
| WARD | 1250.00 |
| JONES | 2975.00 |
| MARTIN | 1250.00 |
| BLAKE | 2850.00 |
| CLARK | 2450.00 |
| SCOTT | 3000.00 |
| KING | 5000.00 |
| TURNER | 1500.00 |
| ADAMS | 1100.00 |
| JAMES | 950.00 |
| FORD | 3000.00 |
| MILLER | 1300.00 |
±-------±--------+
19. List the names of all "CLERK" (clerks) and their department names, the number of people in the department
select ename,job from emp where job = 'CLERK';
±-------±------+
| ename | job |
±-------±------+
| SMITH | CLERK |
| ADAMS | CLERK |
| JAMES | CLERK |
| MILLER | CLERK |
±-------±------+
select
e.ename,e.job,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno
where
e.job = 'CLERK';
±-------±------±-----------+
| ename | job | dname |
±-------±------±-----------+
| MILLER | CLERK | ACCOUNTING |
| SMITH | CLERK | RESEARCH |
| ADAMS | CLERK | RESEARCH |
| JAMES | CLERK | SALES |
±-------±------±-----------+
select
e.ename,e.job,d.dname,d.deptno
from
emp e
join
dept d
on
e.deptno = d.deptno
where
e.job = 'CLERK';
±-------±------±-----------±-------+
| ename | job | dname | deptno |
±-------±------±-----------±-------+
| MILLER | CLERK | ACCOUNTING | 10 |
| SMITH | CLERK | RESEARCH | 20 |
| ADAMS | CLERK | RESEARCH | 20 |
| JAMES | CLERK | SALES | 30 |
±-------±------±-----------±-------+
//Number of people in each department?
select deptno, count(*) as deptcount from emp group by deptno;
±-------±----------+
| deptno | deptcount |
±-------±----------+
| 10 | 3 |
| 20 | 5 |
| 30 | 6 |
±-------±----------+
select
t1.*,t2.deptcount
from
(select
e.ename,e.job,d.dname,d.deptno
from
emp e
join
dept d
on
e.deptno = d.deptno
where
e.job = 'CLERK') t1
join
(select deptno, count(*) as deptcount from emp group by deptno) t2
on
t1.deptno = t2.deptno;
±-------±------±-----------±-------±----------+
| ename | job | dname | deptno | deptcount |
±-------±------±-----------±-------±----------+
| MILLER | CLERK | ACCOUNTING | 10 | 3 |
| SMITH | CLERK | RESEARCH | 20 | 5 |
| ADAMS | CLERK | RESEARCH | 20 | 5 |
| JAMES | CLERK | SALES | 30 | 6 |
±-------±------±-----------±-------±----------+
20. List all kinds of jobs with a minimum salary of more than 1500 and the total number of employees engaged in this job to find the minimum value by grouping of jobs.
select job,count(*) from emp group by job having min(sal) > 1500;
±----------±---------+
| job | count(*) |
±----------±---------+
| ANALYST | 2 |
| MANAGER | 3 |
| PRESIDENT | 1 |
±----------±---------+
21. List the names of employees who work in the department "SALES" <sales>, assuming the department number of the sales department is unknown.
select ename from emp where deptno = (select deptno from dept where dname = 'SALES');
±-------+
| ename |
±-------+
| ALLEN |
| WARD |
| MARTIN |
| BLAKE |
| TURNER |
| JAMES |
±-------+
22. List all employees whose salary is higher than the average salary of the company, their department, their superiors, and their salary grades.
select
e.ename '员工',d.dname,l.ename '领导',s.grade
from
emp e
join
dept d
on
e.deptno = d.deptno
left join
emp l
on
e.mgr = l.empno
join
salgrade s
on
e.sal between s.losal and s.hisal
where
e.sal > (select avg(sal) from emp);
±------±-----------±------±------+
| 员工 | dname | 领导 | grade |
±------±-----------±------±------+
| JONES | RESEARCH | KING | 4 |
| BLAKE | SALES | KING | 4 |
| CLARK | ACCOUNTING | KING | 4 |
| SCOTT | RESEARCH | JONES | 4 |
| KING | ACCOUNTING | NULL | 5 |
| FORD | RESEARCH | JONES | 4 |
±------±-----------±------±------+
23. List the names of all employees and departments who do the same job as "SCOTT"
select job from emp where ename = 'SCOTT';
±--------+
| job |
±--------+
| ANALYST |
±--------+
select
e.ename,e.job,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno
where
e.job = (select job from emp where ename = 'SCOTT')
and
e.ename <> 'SCOTT';
±------±--------±---------+
| ename | job | dname |
±------±--------±---------+
| FORD | ANALYST | RESEARCH |
±------±--------±---------+
24. List the names and salaries of other employees whose salaries are equal to those of employees in department 30.
select distinct sal from emp where deptno = 30;
±--------+
| sal |
±--------+
| 1600.00 |
| 1250.00 |
| 2850.00 |
| 1500.00 |
| 950.00 |
±--------+
select
ename,sal
from
emp
where
sal in(select distinct sal from emp where deptno = 30)
and
deptno <> 30;
Empty set (0.00 sec)
25. List the names and salaries of employees whose salaries are higher than the salaries of all employees working in department 30. Department Name
select max(sal) from emp where deptno = 30;
±---------+
| max(sal) |
±---------+
| 2850.00 |
±---------+
select
e.ename,e.sal,d.dname
from
emp e
join
dept d
on
e.deptno = d.deptno
where
e.sal > (select max(sal) from emp where deptno = 30);
±------±--------±-----------+
| ename | sal | dname |
±------±--------±-----------+
| KING | 5000.00 | ACCOUNTING |
| JONES | 2975.00 | RESEARCH |
| SCOTT | 3000.00 | RESEARCH |
| FORD | 3000.00 | RESEARCH |
±------±--------±-----------+
26. List the number of employees working in each department, the average salary and the average length of service
A department with no employees, the department count is 0
select
d.deptno, count(e.ename) ecount,ifnull(avg(e.sal),0) as avgsal, ifnull(avg(timestampdiff(YEAR, hiredate, now())), 0) as avgservicetime
from
emp e
right join
dept d
on
e.deptno = d.deptno
group by
d.deptno;
±-------±-------±------------±---------------+
| deptno | ecount | avgsal | avgservicetime |
±-------±-------±------------±---------------+
| 10 | 3 | 2916.666667 | 38.0000 |
| 20 | 5 | 2175.000000 | 35.8000 |
| 30 | 6 | 1566.666667 | 38.3333 |
| 40 | 0 | 0.000000 | 0.0000 |
±-------±-------±------------±---------------+
How to calculate the "annual difference" between two dates in mysql, how many years is the difference?
TimeStampDiff(interval type, previous date, next date)
timestampdiff(YEAR, hiredate, now())
Interval types:
SECOND seconds,
MINUTE minutes,
HOUR hours,
DAY days,
WEEK weeks
MONTH months,
QUARTER quarters,
YEAR years
27. List the names, departments and salaries of all employees.
select
e.ename,d.dname,e.sal
from
emp e
join
dept d
on
e.deptno = d.deptno;
±-------±-----------±--------+
| ename | dname | sal |
±-------±-----------±--------+
| CLARK | ACCOUNTING | 2450.00 |
| KING | ACCOUNTING | 5000.00 |
| MILLER | ACCOUNTING | 1300.00 |
| SMITH | RESEARCH | 800.00 |
| JONES | RESEARCH | 2975.00 |
| SCOTT | RESEARCH | 3000.00 |
| ADAMS | RESEARCH | 1100.00 |
| FORD | RESEARCH | 3000.00 |
| ALLEN | SALES | 1600.00 |
| WARD | SALES | 1250.00 |
| MARTIN | SALES | 1250.00 |
| BLAKE | SALES | 2850.00 |
| TURNER | SALES | 1500.00 |
| JAMES | SALES | 950.00 |
±-------±-----------±--------+
28. List details and headcount of all departments
select
d.deptno,d.dname,d.loc,count(e.ename)
from
emp e
right join
dept d
on
e.deptno = d.deptno
group by
d.deptno,d.dname,d.loc;
±-------±-----------±---------±---------------+
| deptno | dname | loc | count(e.ename) |
±-------±-----------±---------±---------------+
| 10 | ACCOUNTING | NEW YORK | 3 |
| 20 | RESEARCH | DALLAS | 5 |
| 30 | SALES | CHICAGO | 6 |
| 40 | OPERATIONS | BOSTON | 0 |
±-------±-----------±---------±---------------+
29. List the minimum wage for various jobs and the name of the employee doing the job
select
job,min(sal) as minsal
from
emp
group by
job;
±----------±---------+
| job | minsal |
±----------±---------+
| ANALYST | 3000.00 |
| CLERK | 800.00 |
| MANAGER | 2450.00 |
| PRESIDENT | 5000.00 |
| SALESMAN | 1250.00 |
±----------±---------+
emp e and above t are connected
select
e.ename,t.*
from
emp e
join
(select
job,min(sal) as minsal
from
emp
group by
job) t
on
e.job = t.job and e.sal = t.minsal;
±-------±----------±--------+
| ename | job | minsal |
±-------±----------±--------+
| SMITH | CLERK | 800.00 |
| WARD | SALESMAN | 1250.00 |
| MARTIN | SALESMAN | 1250.00 |
| CLARK | MANAGER | 2450.00 |
| SCOTT | ANALYST | 3000.00 |
| KING | PRESIDENT | 5000.00 |
| FORD | ANALYST | 3000.00 |
±-------±----------±--------+
30. List the minimum salary of MANAGER (leader) in each department
select
deptno, min(sal)
from
emp
where
job = 'MANAGER'
group by
deptno;
±-------±---------+
| deptno | min(sal) |
±-------±---------+
| 10 | 2450.00 |
| 20 | 2975.00 |
| 30 | 2850.00 |
±-------±---------+
31. List the annual salary of all employees, sorted by annual salary from low to high
select
ename,(sal + ifnull(comm,0)) * 12 as yearsal
from
emp
order by
yearsal asc;
±-------±---------+
| ename | yearsal |
±-------±---------+
| SMITH | 9600.00 |
| JAMES | 11400.00 |
| ADAMS | 13200.00 |
| MILLER | 15600.00 |
| TURNER | 18000.00 |
| WARD | 21000.00 |
| ALLEN | 22800.00 |
| CLARK | 29400.00 |
| MARTIN | 31800.00 |
| BLAKE | 34200.00 |
| JONES | 35700.00 |
| FORD | 36000.00 |
| SCOTT | 36000.00 |
| KING | 60000.00 |
±-------±---------+
32. Find out the names and leaders of employees whose salaries exceed 3,000
select
a.ename '员工',b.ename '领导'
from
emp a
join
emp b
on
a.mgr = b.empno
where
b.sal > 3000;
±------±-----+
| Staff | Leaders |
±------±-----+
| JONES | KING |
| BLAKE | KING |
| CLARK | KING |
±- -----±-----+
33. Find the total salary and number of employees in the department with the 'S' character in the department name
select
d.deptno,d.dname,d.loc,count(e.ename),ifnull(sum(e.sal),0) as sumsal
from
emp e
right join
dept d
on
e.deptno = d.deptno
where
d.dname like '%S%'
group by
d.deptno,d.dname,d.loc;
±-------±-----------±--------±---------------±---------+
| deptno | dname | loc | count(e.ename) | sumsal |
±-------±-----------±--------±---------------±---------+
| 20 | RESEARCH | DALLAS | 5 | 10875.00 |
| 30 | SALES | CHICAGO | 6 | 9400.00 |
| 40 | OPERATIONS | BOSTON | 0 | 0.00 |
±-------±-----------±--------±---------------±---------+
34. Give a 10% salary increase to employees who have served for more than 30 years.
update emp set sal = sal * 1.1 where timestampdiff(YEAR, hiredate, now()) > 30;
Finally, thank you for your support