当前位置:网站首页>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'
)
边栏推荐
猜你喜欢
[width first search] Ji Suan Ke: Suan tou Jun goes home (BFS with conditions)
Redis-字符串类型
Leetcode skimming questions_ Verify palindrome string II
[depth first search] Ji Suan Ke: Betsy's trip
Jisuanke - t2063_ Missile interception
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
02. Go language development environment configuration
leetcode3、實現 strStr()
Redis如何实现多可用区?
How does redis implement multiple zones?
随机推荐
01. Go language introduction
02.Go语言开发环境配置
01.Go语言介绍
[ssrf-01] principle and utilization examples of server-side Request Forgery vulnerability
LeetCode 322. Change exchange (dynamic planning)
竞价推广流程
A basic lintcode MySQL database problem
Leetcode skimming questions_ Sum of squares
Computer graduation design PHP campus restaurant online ordering system
D22:indeterminate equation (indefinite equation, translation + problem solution)
Selenium element positioning (2)
Computer graduation design PHP college student human resources job recruitment network
通过PHP 获取身份证相关信息 获取生肖,获取星座,获取年龄,获取性别
How does the crystal oscillator vibrate?
3D视觉——4.手势识别(Gesture Recognition)入门——使用MediaPipe含单帧(Singel Frame)和实时视频(Real-Time Video)
剑指 Offer 12. 矩阵中的路径
It's wrong to install PHP zbarcode extension. I don't know if any God can help me solve it. 7.3 for PHP environment
How to improve the level of pinduoduo store? Dianyingtong came to tell you
[technology development -28]: overview of information and communication network, new technology forms, high-quality development of information and communication industry
UE4 unreal engine, editor basic application, usage skills (IV)