当前位置:网站首页>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'
)
边栏推荐
- Derivation of Biot Savart law in College Physics
- UE4 unreal engine, editor basic application, usage skills (IV)
- 【clickhouse】ClickHouse Practice in EOI
- NLP第四范式:Prompt概述【Pre-train,Prompt(提示),Predict】【刘鹏飞】
- Computer graduation design PHP part-time recruitment management system for College Students
- Selenium element positioning (2)
- Sword finger offer 38 Arrangement of strings
- Selenium waiting mode
- Gbase 8C database upgrade error
- Cadre du Paddle: aperçu du paddlelnp [bibliothèque de développement pour le traitement du langage naturel des rames volantes]
猜你喜欢
Force buckle 1020 Number of enclaves
[flask] official tutorial -part1: project layout, application settings, definition and database access
You are using pip version 21.1.1; however, version 22.0.3 is available. You should consider upgradin
Card 4G industrial router charging pile intelligent cabinet private network video monitoring 4G to Ethernet to WiFi wired network speed test software and hardware customization
Grabbing and sorting out external articles -- status bar [4]
Executing two identical SQL statements in the same sqlsession will result in different total numbers
Folio. Ink is a free, fast and easy-to-use image sharing tool
Leetcode skimming questions_ Invert vowels in a string
PHP campus financial management system for computer graduation design
抓包整理外篇——————状态栏[ 四]
随机推荐
Basic operations of databases and tables ----- unique constraints
Cookie concept, basic use, principle, details and Chinese transmission
Redis如何实现多可用区?
Paddle framework: paddlenlp overview [propeller natural language processing development library]
【clickhouse】ClickHouse Practice in EOI
Computer graduation design PHP animation information website
[flask] official tutorial -part2: Blueprint - view, template, static file
Basic operations of databases and tables ----- non empty constraints
[flask] static file and template rendering
leetcode-2.回文判断
How does redis implement multiple zones?
[flask] official tutorial -part1: project layout, application settings, definition and database access
【Flask】官方教程(Tutorial)-part2:蓝图-视图、模板、静态文件
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
Xshell 7 Student Edition
安装Redis
[detailed] several ways to quickly realize object mapping
Comments on flowable source code (XXXV) timer activation process definition processor, process instance migration job processor
Basic operations of databases and tables ----- primary key constraints
Pangolin Library: subgraph