Subquery: It is the result of one query as the data source or condition of another query.
How to find out the name of the person with the highest salary?
mysql> select max (sal) from emp; --Query the maximum salary + ---------- + | max (sal) | + ---------- + | 5000.00 | + ---------- + 1 row in set (0.00 sec) mysql> select ename, sal from emp where sal = (select max (sal) from emp);-the maximum wage can be the most A filter condition, select ename, sal from emp is assigned to sal sal after execution (select max (sal) from emp); + ------- + --------- + | ename | sal | + ------- + --------- + | KING | 5000.00 | + ------- + --------- + 1 row in set ( 0.32 sec) mysql> select ename, sal from emp where sal = (select min (sal) from emp); --Query the minimum salary + ------- + -------- + | ename | sal | + ------- + -------- + | SMITH | 800.00 | + ------- + -------- + 1 row in set (0.00 sec)
Subquery situation; use the query result as a condition for another query
Check out the companies have people who are managers; see those corresponding to the empno mgr appear in the field, who is the manager
mysql> select mgr from emp; 查询 +------+ | mgr | +------+ | 7902 | | 7698 | | 7698 | | 7839 | | 7698 | | 7839 | | 7839 | | 7566 | | NULL | | 7698 | | 7788 | | 7698 | | 7566 | | 7782 | | NULL | +------+ 15 rows in set (0.00 sec) mysql> select distinct mgr from emp; --去重 +------+ | mgr | +------+ | 7902 | | 7698 | | 7839 | | 7566 | | NULL | | 7788 | | 7782 | +------+ 7 rows in set (0.00 sec) mysql> select distinct mgr from emp where mgr is not null; --remove the null value, the information obtained by this statement is what empno appears in the mgr field + ------ + | mgr | +- + ----- | 7902 | | 7698 | | 7839 | | 7566 | | 7788 | | 7782 | + ------ + . 6 in rows SET (0.01 sec) MySQL> SELECT ename, empmo from EMP WHERE empmo in (select distinct mgr from emp);-Query which people in the company are managers in keywords + ------- + ------- + | ename | empmo | + ---- + ------- + --- | JONES | 7566 | | BLAKE | 7698 | | CLARK | 7782 | | SCOTT | 7788 | | KING | 7839 | | FORD | 7902 | + ------- + ------- + 6 rows in set (0.00 sec) mysql> select ename,empmo from emp where empmo in(select distinct mgr from emp where mgr is not null); +-------+-------+ | ename | empmo | +-------+-------+ | JONES | 7566 | | BLAKE | 7698 | | CLARK | 7782 | | SCOTT | 7788 | | KING | 7839 | | FORD | 7902 | +-------+-------+ 6 rows in set (0.00 sec)
Note: In the above two cases, one result of the entire query is used as the condition of another query. When used as a condition, equivalent comparison and in are used here. . Which department has the largest average salary found?
1. The average salary of each department needs to be checked out first. Group functions cannot be nested.
As a data source of another query, the query result can be regarded as a table.
Note: The alias must be used in the process of being a table
mysql> select avg (sal), deptno from emp group by deptno; --Query the average salary and the corresponding department number + ------------- + -------- + | avg (sal) | deptno | + ------------- + -------- + | 2356.540000 | NULL | | 2916.666667 | 10 | | 2175.000000 | 20 | | 1566.666667 | 30 | + ------------- + -------- + 4 rows in set (0.00 sec)
MySQL multi-table joint query
Query the name of the employee and the name of the employee's department
mysql> select ename,DNAME from emp,dept; +------------+------------+ | ename | DNAME | +------------+------------+ | SMITH | ACCOUNTING | | SMITH | RESEARCH | | SMITH | SALES | | SMITH | OPERATIONS | | ALLEN | ACCOUNTING | | ALLEN | RESEARCH | | ALLEN | SALES | | ALLEN | OPERATIONS | | WARD | ACCOUNTING | | WARD | RESEARCH | | WARD | SALES | | WARD | OPERATIONS | | JONES | ACCOUNTING | | JONES | RESEARCH | | JONES | SALES | | JONES | OPERATIONS | | MARTIN | ACCOUNTING | | MARTIN | RESEARCH | | MARTIN | SALES | | MARTIN | OPERATIONS | | BLAKE | ACCOUNTING | | BLAKE | RESEARCH | | BLAKE | SALES | | BLAKE | OPERATIONS | | CLARK | ACCOUNTING | | CLARK | RESEARCH | | CLARK | SALES | | CLARK | OPERATIONS | | SCOTT | ACCOUNTING | | SCOTT | RESEARCH | | SCOTT | SALES | | SCOTT | OPERATIONS | | KING | ACCOUNTING | | KING | RESEARCH | | KING | SALES | | KING | OPERATIONS | | TURNER | ACCOUNTING | | TURNER | RESEARCH | | TURNER | SALES | | TURNER | OPERATIONS | | ADAMS | ACCOUNTING | | ADAMS | RESEARCH | | ADAMS | SALES | | ADAMS | OPERATIONS | | JAMES | ACCOUNTING | | JAMES | RESEARCH | | JAMES | SALES | | JAMES | OPERATIONS | | FORD | ACCOUNTING | | FORD | RESEARCH | | FORD | SALES | | FORD | OPERATIONS | | MILLER | ACCOUNTING | | MILLER | RESEARCH | | MILLER | SALES | | MILLER | OPERATIONS | | zhang | ACCOUNTING | | zhang | RESEARCH | | zhang | SALES | | zhang | OPERATIONS | +------------+------------+ 60 rows in set (0.29 sec)
Cartesian Product
(14 * 4) Each data of emp is combined with each data of dept table. That is 56 data.
Multi-table joint query
The deptno in the two tables are equal
mysql> select ename,DNAME from emp,dept where emp.deptno = dept.deptno; +--------+------------+ | ename | DNAME | +--------+------------+ | SMITH | RESEARCH | | ALLEN | SALES | | WARD | SALES | | JONES | RESEARCH | | MARTIN | SALES | | BLAKE | SALES | | CLARK | ACCOUNTING | | SCOTT | RESEARCH | | KING | ACCOUNTING | | TURNER | SALES | | ADAMS | RESEARCH | | JAMES | SALES | | FORD | RESEARCH | | MILLER | ACCOUNTING | +--------+------------+ 14 rows in set (0.01 sec)
Keyword j oin = union and on = condition; query ename and dname in the emp table and dept table, query the corresponding data on emp.deptno = dept.deptno
mysql> select ename,dname from emp join dept on emp.deptno = dept.deptno; +--------+------------+ | ename | dname | +--------+------------+ | SMITH | RESEARCH | | ALLEN | SALES | | WARD | SALES | | JONES | RESEARCH | | MARTIN | SALES | | BLAKE | SALES | | CLARK | ACCOUNTING | | SCOTT | RESEARCH | | KING | ACCOUNTING | | TURNER | SALES | | ADAMS | RESEARCH | | JAMES | SALES | | FORD | RESEARCH | | MILLER | ACCOUNTING | +--------+------------+ 14 rows in set (0.00 sec)
Query a person's name, department, salary level
mysql> select ename,DNAME,GRADE from emp join dept join salgrade on emp.deptno = dept.deptno and emp.sal between losal and hisal; +--------+------------+-------+ | ename | DNAME | GRADE | +--------+------------+-------+ | SMITH | RESEARCH | 1 | | ADAMS | RESEARCH | 1 | | JAMES | SALES | 1 | | WARD | SALES | 2 | | MARTIN | SALES | 2 | | MILLER | ACCOUNTING | 2 | | ALLEN | SALES | 3 | | TURNER | SALES | 3 | | JONES | RESEARCH | 4 | | BLAKE | SALES | 4 | | CLARK | ACCOUNTING | 4 | | SCOTT | RESEARCH | 4 | | FORD | RESEARCH | 4 | | KING | ACCOUNTING | 5 | +--------+------------+-------+ 14 rows in set (0.01 sec)