当前位置:网站首页>MySQL compound query (important)
MySQL compound query (important)
2022-07-29 02:54:00 【GSX_ MI】
Catalog
1. Basic query review
(1) Check that the salary is higher than 500 Or position is MANAGER Of employees , At the same time, their initials should be capitalized J

(2) Sort by department number in ascending order and employee's salary in descending order

(3) Use annual salary to sort in descending order

(4) Displays the name and position of the highest paid employee

(5) Displays information about employees whose wages are higher than the average

(6) Displays the average wage and maximum wage for each department

(7) It shows that the average wage is lower than 2000 Your department number and its average salary

(8) Displays the total number of employees for each position , Average wage

2. Multi-table query
- In actual development, data often comes from different tables , Therefore, you need to query multiple tables . In this section, we use a simple company management system , There are three tables EMP,DEPT,SALGRADE To demonstrate how to query multiple tables .
(1) Show employee name 、 The employee's salary and the name of the Department, because the above data comes from EMP and DEPT surface , Therefore, joint query


① In fact, we only need emp In the table deptno = dept In the table deptno Field records
② The cartesian product
- generally speaking , Our subsequent multi table queries , Should be a subset of the new table formed by Cartesian product !

(2) The display department number is 10 The name of your department , Employee name and salary

(3) Show the name of each employee , Wages , And salary level

3. Self join
(1) Self join refers to joining queries in the same table , A table can be Cartesian with others , Of course, you can also product with your own Cartesian

(2) Show employees FORD The number and name of the superior (mgr Is the employee leader's number --empno)
① Use subquery

② Use multi table query ( Self query )
- Use the alias to the table
- from emp leader, emp worker, Alias your watch , Because we have to do Cartesian product first , So aliases can be identified first

4. Subquery
- Subqueries are embedded in other sql Statement select sentence , Also called nested query
(1) Single line sub query ( The result of the subquery is a single line )
- Show SMITH Employees in the same department

(2) Multi line sub query
①in keyword ; Query and 10 The name of the employee with the same position in department No , Position , Wages , Department number , But it doesn't include 10 Their own
- First find 10 Position in department No

- Find again and 10 The name of the employee with the same position in department No , Position , Wages , Department number

- Finally, the department number is deleted as 10 The data of

②all keyword ; Show salary ratio Department 30 The names of all the employees with high salaries 、 Salary and department number

③any keyword ; Show salary ratio Department 30 Name of any employee with a higher salary 、 Salary and department number ( Including employees in their own department )


(3) Multi column subquery
- A single row subquery means that a subquery returns only a single column , Single line data ; Multi row sub query is to return single column and multi row data , It's all about single columns , Multi column subquery refers to the subquery statement that returns multiple column data
Case study : Query and SMITH All employees in the same department and position , Not included SMITH I am

(4) stay from Clause using subqueries
- Subquery statements appear in from clause . Here we need to use the technique of data query , Use a subquery as a temporary table .
- Get the average salary of each department , Think of it as a temporary table

② Find the name of the highest paid person in each department 、 Wages 、 department 、 Maximum wage

③ Display information for each department ( Department name , Number , Address ) And the number of people
- Method 1: Use multiple tables

- Method 2: Use subquery

(5) Merge query
- in application , In order to merge multiple select The results of the implementation of , You can use the set operator union,union all
①union This operator is used to get the union of two result sets . When using this operator , Duplicate lines in the result set will be automatically removed .
- Pay more than 2500 Or position is MANAGER Find out

②union all This operator is used to get the union of two result sets . When using this operator , Duplicate rows in the result set are not removed .
- Pay more than 25000 Or position is MANAGER Find out

③ The information columns must be the same , Otherwise there will be problems

(6) Summary
- Subqueries can appear in two places ( routine , important )
- 1. where In words , Use as a filter
- 2. from In words , Used to do Cartesian product with a specific table
边栏推荐
- MongoDB索引 (3)
- OWT server source code analysis (4) -- video module analysis of mixer out
- 多重继承与派生类成员标识
- 11. Writing rules - pseudo target
- K210 - sound source location and sound recognition
- 这个博主,qt归类比较全,有空去学习总结,记录一下。
- Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
- Summary of classic problems in Flink production environment
- C语言:小乐乐与欧几里得
- Groupby method
猜你喜欢

STP protocol (spanning tree protocol)

架构师进阶,微服务设计与治理的 16 条常用原则

This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.

New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version

Idea replaces the contents of all files

etcd实现大规模服务治理应用实战

金山云回港上市:中国TO B云厂商的港股奔袭

Analysis of Project-based Learning Creativity in steam Education

自动分账系统哪家好?

C language: judging letters
随机推荐
第六天笔记
Day 10 notes
解析机器人与人类情感共鸣的主观意识
Interpretation of ue4.25 slate source code
DataGrip 如何导出和恢复整个数据库数据,使用单个 SQL 文件
sqlilabs less-32~less-33
STP协议(生成树协议)
STP protocol (spanning tree protocol)
瀚高数据库最佳实践配置工具HG_BP日志采集内容
etcd实现大规模服务治理应用实战
11. Writing rules - pseudo target
多重继承与派生类成员标识
场景分类任务可用数据集(部分)
Polygon zkevm - Introduction to HERMEZ 2.0
MySQL 操作数据库数据报错:Fatal error encountered during command execution
并发模式之异步回调Future模式
nacos名字的由来
区区区间---线段树lazy标记板子题
6-21漏洞利用-mysql弱口令破解
SQL查询数据之多表(关联)查询