1. Group query:group by
It is usually used in combination with aggregate functions to perform statistics in groups. Generally, each XX in the title is written after group by;
- Example: Query the maximum salary of each department:
select deptno,max(sal) from emp group by deptno;
- Example: Query the maximum price of goods under each category:
select category_id,max(price) from t_item group by category_id;
- Example: Query the number of people whose salary is greater than 1500 in each department:
select deptno,sal from emp where sal > 1500 group by deptno;
- Example: Query the number of people under each leader:
select mgr,count(*) from emp where mgr is not null group by mgr;
If you need to use multiple fields for grouping, write multiple field names directly after group by separated by commas:
- Example: Query the number of subordinates of each leader under each department:
select deptno,mgr,count(*) from emp where mgr is not null group by deptno,mgr;
having: to be written after group by and used in combination with it:
- where: The condition that can only write ordinary fields can not write aggregate functions;
- having: You can write common field conditions or aggregate functions later. It is recommended to write only aggregate functions after having;
- Example: Query the average salary of each department and require the average salary to be greater than 2000:
select deptno,avg(sal) a from emp group by deptno having a > 2000;
- Example: Query the average unit price of each category and filter out the average unit price below 100:
select category_id,avg(price) a from t_item group by category_id having a >= 100;
2. Subquery:
- Example: Query the information of the employee with the highest salary in the emp table:
Under normal circumstances, you need to find the employee with the highest salary, and then find the employee's information:
select max(sal) from emp;
(the query result is 5000)
select * from emp where sal = 5000;
Use a subquery:
select * from emp where sal = (select max(sal) from emp);
- Example: Query the information of colleagues in the department of
the employee
select min(sal) from emp;
with the lowest salary : first find the employee with the lowest salary: find the department number by the minimum wage:
select deptno from emp where sal = (select min(sal) from emp);
find other employee information by the number:
select * from emp where deptno = (select deptno from emp where sal = (select min(sal) from emp));
- Extended question: query the department information with the highest
average salary:
select deptno,avg(sal) from emp group by deptno;
get the average salary of each department: get the highest average salary:
select avg(sal) a from emp group by deptno order by a desc limit 0,1;
get the department number through the highest average salary: (here for rigorous consideration, there are multiple departments with the same average salary and the highest Average salary)
select deptno from emp group by deptno having avg(sal) = (select avg(sal) a from emp group by deptno order by a desc limit 0,1);
Obtain department information through department number:
select * from dept where deptno in(select deptno from emp group by deptno having avg(sal) = (select avg(sal) a from emp group by deptno order by a desc limit 0,1));
Where can the subquery be written:
- Written after where or having, as the value of the query condition;
- When writing to create a table, save the query result as a new table;
- Write after from as a virtual table, must have an alias .
3. Association query:
Querying data from multiple tables at the same time is called associative query (only the intersection of two tables can be queried).
- Example: Query the name of each employee and the corresponding department name:
select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno;
Cartesian product: If the association query does not write the association relationship, the result is the product of the two tables. This product is called the Cartesian product. The Cartesian product is a wrong query result. Remember not to appear in your work.
The query method of related query: equal join and inner join
- Equivalent connection:
select * from A,B where A.x = B.x and A.age = 18;
- Inner connection:
select * from A [inner] join B on A.x = B.x where A.age = 18;
[inner] can be omitted without writing
Equivalent joins and inner joins query the same content, which are related data (intersection part) in two tables.
-
Example: Query the name of each employee and the name of the corresponding department: (internal connection)
select e.ename,d.deptno from emp e join dept d on e.deptno = d.deptno;
-
Outer join: The inner join and the equivalent join query the intersection data, and the outer join queries all the data of a table + the intersection data of another table.
-
Left/right outer join:
select * from A left/right join B on A.x = B.x where A.age = 18;
(Left outer join is the intersection of the left table, A, as the main table and the right table)
Summary of related queries:
- The query mode of the related query: equivalent join, inner join, outer join;
- If the data you want to query is the intersection data of two tables, use equal join and inner join (recommended);
- If the data to be queried is the intersection of all data in a table and the intersection data of a table, use outer join;