[MySQL Advanced]: The perfect combination of subquery and HAVING/SELECT

foreword

✨Welcome to Xiao K 's MySQL columnHAVING/SELECTWITH/EXISTS , this section will bring you an explanation of the use of MySQL subqueries in sentences, and related subqueries and sentences✨

1. Use subqueries in the HAVING/SELECT clause

✨✨ HAVING words

  • Query the department number, number of employees, and average salary, and require that the average salary of these departments is higher than the company's average salary.
SELECT deptno,COUNT(deptno) cnt,AVG(sal) avgsal 
FROM emp 
GROUP BY deptno
HAVING avgsal>
(
	SELECT AVG(sal) FROM emp
);
  • Query the name and average salary of the department with the highest average salary among all departments
SELECT e.deptno,d.dname,ROUND(AVG(sal),2) avgsal
FROM emp e,dept d
WHERE e.deptno=d.deptno
GROUP BY e.deptno
HAVING avgsal>
(
    #查询出所有部门平均工资中最高的薪资
	 SELECT MAX(avgsal) FROM 
		(SELECT AVG(sal) avgsal FROM emp GROUP BY deptno) AS temp
)

insert image description here

✨✨ SELECT statement

  • Query the number, name, location, number of people in each department, and average salary of each department of the company
#1多表查询
SELECT d.deptno,d.dname,d.loc,COUNT(e.deptno),AVG(e.sal)
FROM emp e,dept d
WHERE e.deptno=d.deptno
GROUP BY e.deptno;
#2
SELECT d.deptno,d.dname,d.loc,temp.cnt,temp.avgsal
FROM dept d,(SELECT deptno,COUNT(deptno) cnt,AVG(sal) avgsal FROM emp GROUP BY deptno) temp
WHERE d.deptno=temp.deptno;
#3 关联子查询
SELECT d.deptno,d.dname,d.loc,
(SELECT COUNT(deptno) FROM emp WHERE deptno=d.deptno GROUP BY deptno) cnt,
(SELECT AVG(sal) FROM emp WHERE deptno=d.deptno GROUP BY deptno) avgsal
FROM dept d;

insert image description here

2. Correlated subqueries

✨If the execution of the subquery depends on the external query, it is usually because the tables in the subquery use external tables and are associated with conditions. Therefore, every time the external query is executed, the subquery must be recalculated. The subquery is called a correlated subquery. Correlated subqueries are indexed row by row, with each row of the main query pointing to the subquery once.

insert image description here

✨Query demand

  • Query the department number, name, and salary of employees whose salary is greater than the average salary of the department
SELECT e.deptno,e.ename,e.sal
FROM emp e
WHERE e.sal>(SELECT AVG(sal) FROM emp WHERE deptno=e.deptno );

insert image description here

3. WITH/EXISTS, NOT EXISTS clauses

✨✨ WITH clause

  • Query the number, name, location, average salary of each department, and the number of people
-- 多表查询
SELECT d.deptno,d.dname,d.loc,AVG(e.sal) avgsal ,COUNT(e.deptno) cnt
FROM dept d,emp e
WHERE d.deptno=e.deptno
GROUP BY e.deptno;

-- 子查询
SELECT d.deptno,d.dname,d.loc,temp.avgsal,temp.cnt
FROM dept d,(
							SELECT deptno,AVG(sal) avgsal,COUNT(deptno) cnt
							FROM emp
							GROUP BY deptno
						)temp
WHERE d.deptno=temp.deptno;

-- 使用with
WITH temp AS(
							SELECT deptno,AVG(sal) avgsal,COUNT(deptno) cnt
							FROM emp
							GROUP BY deptno
						)
SELECT d.deptno,d.dname,d.loc,temp.avgsal,temp.cnt
FROM dept d,temp
WHERE d.deptno=temp.deptno;
									
  • Query the employee number, name, position, employment date, salary, department number, and department name with the highest salary in each department, and the displayed results are sorted by department number
-- 相关子查询
SELECT e.empno,e.ename,e.job,e.hiredate,e.sal,e.deptno,d.dname
FROM emp e,dept d
WHERE e.deptno=d.deptno
AND e.sal=(SELECT MAX(sal) FROM emp WHERE deptno=e.deptno)
ORDER BY e.deptno;
-- 表子查询
SELECT e.empno,e.ename,e.job,e.hiredate,e.sal,e.deptno,d.dname
FROM emp e,dept d,(SELECT deptno,MAX(sal) maxsal FROM emp GROUP BY deptno) temp
WHERE e.deptno=d.deptno
AND e.sal=temp.maxsal
AND e.deptno = temp.deptno
ORDER BY e.deptno;

insert image description here
insert image description here

✨✨EXISTS/NOT EXISTS字句

An exixts structure is provided in SQL to determine whether a subquery has data to return. The exists structure returns true if there is data returned in the subquery, otherwise it returns false.

  • Query the company manager's number, name, job, department number
-- 多表查询
SELECT DISTINCT e.empno,e.ename,e.job,e.deptno
FROM emp e JOIN emp mgr
ON e.empno=mgr.mgr;
-- 使用EXISTS
SELECT e.empno,e.ename,e.job,e.deptno
FROM emp e
WHERE EXISTS (SELECT * FROM emp WHERE e.empno=mgr);
  • Query the department information in the department table that does not exist in the employee table
-- 多表查询
SELECT e.deptno,d.deptno,d.dname,d.loc
FROM emp e RIGHT JOIN dept d
ON e.deptno=d.deptno
WHERE e.deptno IS NULL;

-- 使用EXISTS
SELECT d.deptno,d.dname,d.loc
FROM  dept d
WHERE NOT EXISTS (SELECT deptno FROM emp WHERE deptno=d.deptno);

insert image description here
insert image description here

Four. Summary

  • ✨✨Subqueries allow structured queries so that each part of a query statement can be separated.
  • ✨✨Subqueries provide another way to perform operations that require complex joins and unions.
  • ✨✨ Subqueries are considered more readable by many people. In fact, this is where subqueries come in.

Guess you like

Origin blog.csdn.net/qq_72157449/article/details/131303896