当前位置:网站首页>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'
								)

原网站

版权声明
本文为[Fried Crazy Rabbit]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140048326539.html