MySQL summary exercises

Table of contents

1. Prepare data sheet

2. Relationship between tables

3. Question

3.1 Name of the person with the highest salary in each department

3.2 Whose salary is above the average salary of the department

3.3 Obtain the average salary grade (of all persons) in the department

3.4 It is not allowed to use the group function (Max) to obtain the highest salary

3.5 Get the department number of the department with the highest average salary

3.5 Get the department name of the department with the highest average salary

3.6 Get the sixth to tenth highest paid employees

3.7 Get how many employees there are in each salary grade

3.8 List the names of all employees and leaders

3.9 List all departments with at least 5 employees

3.10 List all employee information whose salary is more than "SMITH"

3.11 List the names of all "CLERK" (clerks), their department names, and the number of people in the department

3.12 List the various jobs with a minimum salary greater than 1,500 and the total number of employees engaged in this job and find the minimum value according to job grouping.

3.13 List the names of employees working in the department "SALES" < Sales Department >, assuming that the department number of the Sales Department is not known.

3.14 List all employees whose salary is higher than the company's average salary, their departments, superiors, and employees' salary grades.

3.15 List all employees and department names engaged in the same work as "SCOTT"

3.16 List the names and salaries of employees whose salaries are higher than the salaries of all employees working in department 30. Department Name

3.17 List the number of employees working in each department, and the average salary

3.18 List the names, department names and salaries of all employees.

3.19 List details and headcount of all departments

3.20 List the minimum wages for various jobs and the names of employees performing the jobs


Do some exercises on the basic and advanced aspects of mysql that you have learned before, and consolidate the knowledge you have learned previously.

1. Prepare data sheet


CREATE TABLE DEPT
       (DEPTNO int(2) not null ,
    DNAME VARCHAR(14) ,
    LOC VARCHAR(13),
    primary key (DEPTNO)
    );
CREATE TABLE EMP
       (EMPNO int(4)  not null ,
    ENAME VARCHAR(10),
    JOB VARCHAR(9),
    MGR INT(4),
    HIREDATE DATE  DEFAULT NULL,
    SAL DOUBLE(7,2),
    COMM DOUBLE(7,2),
    primary key (EMPNO),
    DEPTNO INT(2)
    )
    ;

CREATE TABLE SALGRADE
      ( GRADE INT,
    LOSAL INT,
    HISAL INT );




INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
10, 'ACCOUNTING', 'NEW YORK');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
20, 'RESEARCH', 'DALLAS');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
30, 'SALES', 'CHICAGO');
INSERT INTO DEPT ( DEPTNO, DNAME, LOC ) VALUES (
40, 'OPERATIONS', 'BOSTON');
commit;

INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7369, 'SMITH', 'CLERK', 7902,  '1980-12-17'
, 800, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7499, 'ALLEN', 'SALESMAN', 7698,  '1981-02-20'
, 1600, 300, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7521, 'WARD', 'SALESMAN', 7698,  '1981-02-22'
, 1250, 500, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7566, 'JONES', 'MANAGER', 7839,  '1981-04-02'
, 2975, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7654, 'MARTIN', 'SALESMAN', 7698,  '1981-09-28'
, 1250, 1400, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7698, 'BLAKE', 'MANAGER', 7839,  '1981-05-01'
, 2850, NULL, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7782, 'CLARK', 'MANAGER', 7839,  '1981-06-09'
, 2450, NULL, 10);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7788, 'SCOTT', 'ANALYST', 7566,  '1987-04-19'
, 3000, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7839, 'KING', 'PRESIDENT', NULL,  '1981-11-17'
, 5000, NULL, 10);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7844, 'TURNER', 'SALESMAN', 7698,  '1981-09-08'
, 1500, 0, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7876, 'ADAMS', 'CLERK', 7788,  '1987-05-23'
, 1100, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7900, 'JAMES', 'CLERK', 7698,  '1981-12-03'
, 950, NULL, 30);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7902, 'FORD', 'ANALYST', 7566,  '1981-12-03'
, 3000, NULL, 20);
INSERT INTO EMP ( EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM,
DEPTNO ) VALUES (
7934, 'MILLER', 'CLERK', 7782,  '1982-01-23'
, 1300, NULL, 10);
commit;

INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
1, 700, 1200);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
2, 1201, 1400);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
3, 1401, 2000);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
4, 2001, 3000);
INSERT INTO SALGRADE ( GRADE, LOSAL, HISAL ) VALUES (
5, 3001, 9999);
commit;

By running the above sql, the required data tables are created.

2. Relationship between tables

  1. DEPT (department table):

    • DEPTNO: department number, integer type, primary key.
    • DNAME: Department name.
    • LOC: Department location.
  2. EMP (employee table):

    • EMPNO: employee number, integer type, primary key.
    • ENAME: Employee name.
    • JOB: employee position.
    • MGR: employee number of the superior manager, integer type.
    • HIREDATE: Joining date, date type, default is NULL.
    • SAL: Salary.
    • COMM: bonus.
    • DEPTNO: department number, integer type.
  3. SALGRADE (salary grade table):

    • GRADE: salary grade, integer type.
    • LOSAL: minimum salary, integer type.
    • HISAL: Highest salary, integer type.

3. Question

3.1 Name of the person with the highest salary in each department

Idea:

  1. Use INNER JOIN to connect the EMP table and DEPT table to obtain information about employees and their departments.

  2. Using a subquery, calculate the maximum salary for each department. Use the GROUP BY and MAX functions in the subquery to find the highest salary for each department.

  3. In the main query, filter out employees whose salary is equal to the highest salary by comparing their salary with the highest salary in their department.

  4. The result set includes the name, department name, and salary of the highest-paid employee in each department.

select d.dname as "部门名称", e.ename as "员工姓名", e.sal as "薪水"
from emp e
inner join dept d on e.deptno = d.deptno
where (e.deptno, e.sal) in (
    select deptno, max(sal)
    from emp
    group by deptno
);

result:

3.2 Whose salary is above the average salary of the department

Idea:

  1. In the main query, INNER JOIN is used to connect the EMP table (e) and the DEPT table (d) to obtain relevant information about employees and their departments.

  2. Use an INNER JOIN to join a subquery that calculates the average salary for each department. The GROUP BY and AVG functions are used in the subquery to calculate the average salary of each department.

  3. In the main query, by comparing the employee's salary (e.sal) with the average salary of his department (dept_avg.avg_dept_salary), employees whose salary is higher than the average salary of their department are filtered out.

  4. The result set includes employee name, employee salary, department name, and department average salary to compare employee salary with department average salary.

SELECT e.ename AS "员工姓名", e.sal AS "员工薪水", d.dname AS "部门名称", avg_dept_salary AS "部门平均薪水"
FROM emp e
INNER JOIN dept d ON e.deptno = d.deptno
INNER JOIN (
    SELECT deptno, AVG(sal) AS avg_dept_salary
    FROM emp
    GROUP BY deptno
) dept_avg ON e.deptno = dept_avg.deptno
WHERE e.sal > dept_avg.avg_dept_salary;

result:

3.3 Obtain the average salary grade (of all persons) in the department

1. First, get everyone’s salary grade and then group them by department number.

2. Group by department number and then take the average

select e.DEPTNO as '部门编号',avg(s.GRADE) as '部门平均薪资等级'
    from emp e
    inner join salgrade s
    on e.SAL between s.LOSAL and s.HISAL group by  e.DEPTNO;

3.4 It is not allowed to use the group function (Max) to obtain the highest salary

The first method: use descending order to get the first data

select ENAME,SAL from emp order by SAL desc limit 1;

The second method: use self-connection and then perform not in to search

select SAL from emp where sal 
not in (select distinct a.SAL from emp a join emp b on a.SAL < b.SAL)

3.5 Get the department number of the department with the highest average salary

Use avg to take the average, group by deptno, and finally sort in descending order to take the first value

select DEPTNO as '部门编号',avg(SAL) as '平均薪水' from emp 
group by DEPTNO order by avg(SAL) desc limit 1;

3.5 Get the department name of the department with the highest average salary

Group by dname and sort in descending order

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;

3.6 Get the sixth to tenth highest paid employees

Sort in descending order and take 6-10

select ENAME,SAL from emp order by sal desc limit 5,5;

3.7 Get how many employees there are in each salary grade

Group by grand and then sum

select s.GRADE,COUNT(*) from emp e 
inner join salgrade s 
on e.SAL between s.LOSAL and s.HISAL 
group by s.GRADE;

3.8 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;

3.9 List all departments with at least 5 employees

  1. Use INNER JOIN to connect the EMP table (e) and the DEPT table (d) to obtain information about employees and their departments.

  2. Use GROUP BY clause to group by department name (d.dname) to count the number of employees in each department.

  3. Use the HAVING clause to filter out departments with at least 5 employees.

select d.DNAME as '部门名称',count(*) as '员工数量'  
from emp e inner join dept d on e.DEPTNO = d.DEPTNO 
group by d.DNAME having count(*) >= 5;

3.10 List all employee information whose salary is more than "SMITH"

Just filter them out and compare them.

select ENAME,SAL from emp 
where SAL > (select SAL from emp where ename = 'SMITH') order by SAL desc ;

3.11 List the names of all "CLERK" (clerks), their department names, and the number of people in the department

1. Select the name (ename) and department number (deptno) of the employee with the position "CLERK" from the EMP table.

2. Use INNER JOIN to connect the above query results with the DEPT table to obtain the name (dname) of the employee's department.

3. Use a subquery to calculate the number of employees in each department and use the GROUP BY clause to group by department number.

4. Finally, use the results of the above query as a subquery and use INNER JOIN again to connect the department headcount (deptcount) with the department name (dname) to get the final results.

SELECT t1.ename AS "员工姓名", t1.dname AS "部门名称", t2.deptcount AS "部门人数"
FROM (
    SELECT e.ename, d.dname, e.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;

3.12 列出最低薪金大于 1500 的各种工作及从事此工作的全部雇员人数按照工作岗位分组求最小值。

select job,sal, count(*) as '从事该工作工资低于1500的人数' from emp  
group by job having min(SAL) > 1500;

3.13 List the names of employees working in the department "SALES" < Sales Department >, assuming that the department number of the Sales Department is not known.

After querying the department number of "SALES", perform conditional query in the emp table.

select ENAME as '名字'  from emp 
where deptno = (select DEPTNO from dept where DNAME = 'SALES')

3.14 List all employees whose salary is higher than the company's average salary, their departments, superiors, and employees' salary grades.

  1. Use INNER JOIN to connect the EMP table (e) and the DEPT table (d) to obtain relevant information about employees and departments.

  2. Use INNER JOIN to connect the employee's salary with the salary grade table (SALGRADE) to obtain salary grade information.

  3. Use LEFT JOIN to connect the employee's superior leader (associated through the mgr column) with the employee table to obtain the name of the superior leader.

  4. Use the WHERE clause to filter out employees whose salary is higher than the company's average salary.

select e.ENAME as '名字',e.SAL as '薪资', (select avg(SAL) from emp) as '平均薪资', d.DNAME as '部门名称',s.GRADE as '薪资等级',e2.ENAME as '上级领导'
from emp e
    inner join dept d on e.DEPTNO = d.DEPTNO
    inner join salgrade s on e.SAL between s.LOSAL and s.HISAL
    left join emp e2 on e.mgr = e2.EMPNO
    where e.SAL > (select avg(SAL) from emp);

 

3.15 List all employees and department names engaged in the same work as "SCOTT"

First find out the job "scott" does, then connect with dept to find out the people and work departments who have the same job as him, and then use a negative to filter out "scott"

select e.ENAME as '名字',d.DNAME as '部门名称',e.JOB as '工作'
from emp e
inner join dept d on e.DEPTNO = d.DEPTNO
where e.JOB = (select JOB as '名字' from emp where ENAME = 'SCOTT')
and e.ENAME <> 'SCOTT';

 

3.16 List the names and salaries of employees whose salaries are higher than the salaries of all employees working in department 30. Department Name

Filter out the highest salary of the department with department number: 30, use this as a condition, and then connect to the dept table

select e.ENAME ,e.SAL ,d.DNAME from emp e
inner join dept d on e.DEPTNO = d.DEPTNO
where e.SAL > (select max(SAL) from emp where DEPTNO = 30);

3.17 List the number of employees working in each department, and the average salary

Connect the emp table and the dept table, group according to DNAME, use the count function to calculate the total and avg to calculate the average salary

select d.DNAME as '部门名称' ,count(*) as '员工数量' ,avg(e.SAL) from emp e 
inner join dept d where e.DEPTNO = d.DEPTNO group by d.DNAME;

 

3.18 List the names, department names and salaries of all employees.

This can be done directly by connecting two tables.

select e.ENAME,d.DNAME,e.SAL from emp e inner join dept d on e.DEPTNO = d.DEPTNO;

 

3.19 List details and headcount of all departments

select d.DEPTNO, d.DNAME, d.LOC,count(e.ENAME) as '部门人数' from emp e
right join dept d on d.DEPTNO = e.DEPTNO group by d.deptno;

3.20 List the minimum wages for various jobs and the names of employees performing the jobs

Here you can use jobs to perform grouping and filtering, but the problem that may be overlooked is that there are multiple employees with the lowest salary.

This can be combined using GROUP_CONCAT(e.ename). The function of this function is to combine multiple values ​​​​in a group into a comma-separated string.

SELECT e.job AS "工作岗位", MIN(e.sal) AS "最低工资", GROUP_CONCAT(e.ename) AS "雇员姓名"
FROM emp e
GROUP BY e.job;

More questions will be added in the future. . . . . .

Guess you like

Origin blog.csdn.net/m0_64642443/article/details/133748500