当前位置:网站首页>MySQL learning notes - subquery exercise
MySQL learning notes - subquery exercise
2022-07-06 02:01:00 【Fried Crazy Rabbit】
This paper records the learning MySQL The notes , Among them, all courseware resources come from Shang Silicon Valley , Please move to the next step for more information b standing
MySQL All note links
Subquery exercise
Subquery should be regarded as the difficulty ceiling of the basic chapter ...
1. Query and Zlotkey Name and salary of employees in the same department
# Since ancient times, the first question is not very difficult , Just use sub query directly
# Use it carefully IN instead of =, In case there are multiple employees who are called Zlotkey Well
SELECT department_id,last_name,salary
FROM employees
WHERE department_id IN (
SELECT department_id
FROM employees
WHERE last_name = 'Zlotkey'
)
2. Query the employee number of the employee whose salary is higher than the average salary of the company , full name , Wages
# Internal query : Query average wage
SELECT employee_id, last_name, salary
FROM employees
WHERE salary > (
SELECT AVG(salary)
FROM employees
)
3. Select salary greater than all JOB_ID = 'SA_MAN’ Employee's salary, employee's information
# Use ALL, More than all the wages
SELECT last_name, salary
FROM employees
WHERE salary > ALL(
SELECT salary
FROM employees
WHERE job_id = 'SA_MAN'
)
4. Queries and names contain letters u Employee number and name of employees in the same department
SELECT employee_id, last_name
FROM employees
WHERE department_id IN (
SELECT DISTINCT department_id
FROM employees
WHERE last_name LIKE '%u%'
)
5. Check the... In the Department location_id by 1700 The employee number of the employee working in the Department
# You need to query the Department in the Department table id( Internal query )
SELECT employee_id
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE location_id = 1700
)
6. The query manager is King The name and salary of the employee
# There is no manager_name, But there are manager_id, We find the leader by name id That's it ( Remember to use IN)
SELECT last_name, salary, manager_id
FROM employees
WHERE manager_id IN (
SELECT employee_id
FROM employees
WHERE last_name = 'King'
)
7. Query the information of the lowest paid employee
# Check the minimum wage , Inquire who is this salary
SELECT last_name, salary
FROM employees
WHERE salary = (
SELECT MIN(salary)
FROM employees
)
8. Search the Department with the lowest average wage 🤯🤯
The first is to group by Department , Query the average salary of each department .
Then treat the obtained data as a table ( So we need another alias ), Use MIN Function can get the minimum wage
Because what we need to inquire is the information of the Department , Then match the above minimum wage according to the salary , You can get the Department id
Then query the information of the Department
SELECT MIN(avg_sal) # Query minimum wage
FROM (
SELECT AVG(salary) avg_sal # The average salary of each department
FROM employees
GROUP BY department_id
) t_dept_avg_sal
# People are numb , What about this set of dolls
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary) avg_sal # The average salary of each department
FROM employees
GROUP BY department_id
) t_dept_avg_sal
)
)
# The second way
SELECT *
FROM departments
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) <= ALL(
SELECT AVG(salary) # The average salary of each department
FROM employees
GROUP BY department_id
)
)
# How to query the minimum wage Department 2( Use LIMIT):
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
ORDER BY avg_sal ASC
LIMIT 1
The above demonstrates two ways , There are two other uses LIMIT Determine the highest or lowest method
9. Query the information of the Department with the lowest average wage and the average wage of the Department ( Correlation subquery )
The front is the same as question 8 , There is an additional demand to show the average salary
Query the average salary in the row of the outermost query information , One of the departments id Get from the table ( Be careful SQL Execution order of )
SELECT d.*,(SELECT AVG(salary) FROM employees WHERE department_id = d.department_id) avg_sal
FROM departments d
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MIN(avg_sal)
FROM (
SELECT AVG(salary) avg_sal # The average salary of each department
FROM employees
GROUP BY department_id
) t_dept_avg_sal
)
)
10. Find the highest average wage job Information about
Basically the same as question 8
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
HAVING AVG(salary) = (
SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY job_id
) t_job_avg_sal
)
)
SELECT *
FROM jobs
WHERE job_id = (
SELECT job_id
FROM employees
GROUP BY job_id
HAVING AVG(salary) >= ALL(
SELECT AVG(salary)
FROM employees
GROUP BY job_id
)
)
11. Query the departments whose average salary is higher than the average salary of the company
SELECT department_id
FROM employees
WHERE department_id IS NOT NULL
GROUP BY department_id
HAVING AVG(salary) > (
SELECT AVG(salary)
FROM employees
)
12. Query in company manager Details of
# Self connection mode :
SELECT DISTINCT mgr.employee_id, mgr.last_name, mgr.job_id, mgr.department_id
FROM employees emp JOIN employees mgr
ON emp.manager_id = mgr.employee_id
# Subquery method :
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE employee_id IN (
SELECT DISTINCT manager_id# Query all mgr_id
FROM employees
)
13. In all departments , Sort according to their highest wages , The minimum wage in the lowest ranked Department ?
SELECT MIN(salary)
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) = (
SELECT MIN(max_sal)
FROM (
SELECT max(salary) max_sal
FROM employees
GROUP BY department_id
) t_dept_max_sal
)
)
SELECT MIN(salary)
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING MAX(salary) <= ALL(
SELECT max(salary) max_sal
FROM employees
GROUP BY department_id
)
)
14. Check the Department with the highest average wage manager Details of ( I'm almost dizzy )
SELECT last_name, department_id, email, salary
FROM employees
WHERE employee_id = ANY(
SELECT DISTINCT manager_id
FROM employees
WHERE department_id = (
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary) = (
SELECT MAX(avg_sal)
FROM (
SELECT AVG(salary) avg_sal
FROM employees
GROUP BY department_id
) t_dept_avg_sal
)
)
);
15. Query the department number of the Department , It does not include job_id yes ‘ST_CELRK’ Your department number
SELECT department_id
FROM departments
WHERE department_id NOT IN (
SELECT DISTINCT department_id
FROM employees
WHERE job_id = 'ST_CLERK'
)
SELECT department_id
FROM departments d
WHERE NOT EXISTS (
SELECT *
FROM employees e
WHERE d.department_id = e.department_id
AND e.job_id = 'ST_CLERK'
)
边栏推荐
- A basic lintcode MySQL database problem
- Leetcode skimming questions_ Invert vowels in a string
- I like Takeshi Kitano's words very much: although it's hard, I will still choose that kind of hot life
- 3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
- Gbase 8C database upgrade error
- Using SA token to solve websocket handshake authentication
- [Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice
- [flask] official tutorial -part2: Blueprint - view, template, static file
- 500 lines of code to understand the principle of mecached cache client driver
- [ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
猜你喜欢

Derivation of Biot Savart law in College Physics

干货!通过软硬件协同设计加速稀疏神经网络

【clickhouse】ClickHouse Practice in EOI

Accelerating spark data access with alluxio in kubernetes

Redis如何实现多可用区?

插卡4G工业路由器充电桩智能柜专网视频监控4G转以太网转WiFi有线网速测试 软硬件定制
![[flask] official tutorial -part3: blog blueprint, project installability](/img/fd/fc922b41316338943067469db958e2.png)
[flask] official tutorial -part3: blog blueprint, project installability
![[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice](/img/3a/63f3e89ddf84f23f950ed9620b4405.jpg)
[Clickhouse] Clickhouse based massive data interactive OLAP analysis scenario practice

Campus second-hand transaction based on wechat applet
![[depth first search] Ji Suan Ke: Betsy's trip](/img/b5/f24eb28cf5fa4dcfe9af14e7187a88.jpg)
[depth first search] Ji Suan Ke: Betsy's trip
随机推荐
01. Go language introduction
安装Redis
3D vision - 4 Getting started with gesture recognition - using mediapipe includes single frame and real time video
Mongodb problem set
Selenium waiting mode
Using SA token to solve websocket handshake authentication
Paddle框架:PaddleNLP概述【飞桨自然语言处理开发库】
Win10 add file extension
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
selenium 元素定位(2)
Gbase 8C database upgrade error
国家级非遗传承人高清旺《四大美人》皮影数字藏品惊艳亮相!
NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
FTP server, ssh server (super brief)
leetcode-2.回文判断
PHP campus financial management system for computer graduation design
I like Takeshi Kitano's words very much: although it's hard, I will still choose that kind of hot life
Basic operations of databases and tables ----- default constraints