当前位置:网站首页>Chapter IX sub query (key)
Chapter IX sub query (key)
2022-07-28 05:58:00 【Big data management】
Chapter nine Subquery
# Query salary ratio of employees Abel Many people
# Subquery
SELECT e2.last_name,e2.salary
FROM employees e1,employees e2
WHERE e1.last_name=‘Abel’ AND e1.salary<e2.salary;
# Subquery
SELECT last_name,salary
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE last_name=‘Abel’
);
# Queries can be divided into single line sub queries and multi line sub queries , It can also be divided into related sub queries and unrelated sub queries
# Correlation subquery : Query employee information whose salary is greater than the average salary of the Department
SELECT last_name,department_id,salary
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
)
GROUP BY department_id;
# Uncorrelated subqueries : Query the information of employees whose salary is greater than the average salary of the company
SELECT last_name,salary
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
)
# Query salary greater than 149 Information of the employee whose salary is No
SELECT employee_id,last_name,salary
FROM employees
WHERE salary>(
SELECT salary
FROM employees
WHERE employee_id=149
);
# return job_id And 141 Same as employee No ,salary Than 143 The name of the employee with a large number of employees ,job_id And wages
SELECT last_name,job_id,salary
FROM employees
WHERE job_id=(
SELECT job_id
FROM employees
WHERE employee_id=141
) AND salary>(
SELECT salary
FROM employees
WHERE employee_id=143
);
# Return to the lowest paid employees of the company last_name,job_id and salary
SELECT last_name,job_id,salary
FROM employees
WHERE salary=(
SELECT MIN(salary)
FROM employees
);
# Query and 141 Number (employee_id) Staff manager_id and department_id Of the same other employees employee_id,manager_id,department_id
# Mode one : Unpaired comparison , Compare them separately
SELECT employee_id,manager_id,department_id
FROM employees
WHERE manager_id=(
SELECT manager_id
FROM employees
WHERE employee_id=141
)
AND department_id=(
SELECT department_id
FROM employees
WHERE employee_id=141
)
AND employee_id<>141;
# Mode two : Compare in pairs
SELECT employee_id,manager_id,department_id
FROM employees
WHERE (manager_id,department_id)=(
SELECT manager_id,department_id
FROM employees
WHERE employee_id=141
)
AND employee_id <>141;
# Query and 141 Number or 174 Staff manager_id and department_id Of the same other employees employee_id,manager_id,department_id
# Mode one : Unpaired comparison
SELECT employee_id,manager_id,department_id
FROM employees
WHERE manager_id IN
(SELECT manager_id
FROM employees
WHERE employee_id IN(141,174))
AND department_id IN
(SELECT department_id
FROM employees
WHERE employee_id IN(141,174))
AND employee_id NOT IN(141,174);
# Compare in pairs
SELECT employee_id,manager_id,department_id
FROM employees
WHERE (manager_id,department_id)IN(
SELECT manager_id,department_id
FROM employees
WHERE employee_id IN(141,174))
AND employee_id NOT IN(141,174);
#having Subqueries in
# The minimum wage is greater than 50 The Department of minimum wage id And its minimum wage
SELECT department_id,MIN(salary)
FROM employees
GROUP BY department_id
HAVING MIN(salary)>(
SELECT MIN(salary)
FROM employees
WHERE department_id=50
);
#case Subqueries in
# Displays the employee's employee_id,last_name and location, among , If employee department_id And location_id by 1800 Of department_id identical , be lacation by ’Canada’, Others are ’USA’
SELECT employee_id,last_name,
(CASE department_id
WHEN
(SELECT department_id FROM departmentsemployees
WHERE location_id=1800)
THEN ‘Canada’ ELSE ‘USA’ END)
AS location
FROM employees;
# Check the title and type of the book , among note The value is novel Show novel ,law Show legal ,
medicine Show medicine ,cartoon Show cartoon ,joke Show jokes
SELECT name’ Title ’,note,
CASE note
WHEN’novel’ THEN’ A novel ’
WHEN’law’THEN’ law ’
WHEN’medicine’THEN’ medicine ’
WHEN’cartoon’THEN’ cartoon ’
WHEN’joke’THEN’ joke ’
ELSE’ other ’
END
AS ‘ type ’
FROM books;
# Check the title of the book 、 stock , among num Value exceeds 30 Ben's , Show unsalable , Greater than 0 And below 10 Of , Show best sellers , by 0 The display needs to be out of stock
SELECT nameAS’ Title ’,num AS’ stock ’,
CASE WHEN num>30 THEN’ Unsalable ’
WHEN 0<num<10 THEN’ Sell well ’
WHEN num=0 THEN’ No goods ’
ELSE ‘ normal ’
END
AS ‘ Display state ’
FROM books;
# Null value of subquery : Because the subquery inside is empty , No call Haas People who , All eventually did not return any rows
SELECT last_name,job_id
FROM employees
WHERE job_id=(
SELECT job_id
FROM employees
WHERE last_name=‘Haas’
)
# Illegal use of subquery : Using equal to will report an error , Because this sub query will return multiple rows of data , Change the equal sign to in that will do
SELECT employee_id,last_name
FROM employees
WHERE salary =(
SELECT MIN(salary)
FROM employees
GROUP BY department_id
)
# Because different employees receive the same minimum wage
SELECT employee_id,last_name
FROM employees
WHERE salary IN(
SELECT MIN(salary)
FROM employees
GROUP BY department_id
)
# Multi line sub query
# Operator of multi row subquery :in() any all some( yes any Another name for , It's usually used any)
# Return to other job_id Middle ratio job_id by ’IT_PROG’ The employee number of any employee in the Department with low salary 、 full name 、job_id as well as salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary<ANY(
SELECT salary
FROM employees
WHERE job_id=‘IT_PROG’)
AND job_id!=‘IT_PROG’
SELECT* FROM employees
WHERE employee_id!=101;
# Return to other job_id Middle ratio job_id by ’IT_PROG’ Employee number with low salary in the Department 、 full name 、job_id as well as salary
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary<(
SELECT MIN(salary)
FROM employees
WHERE job_id=‘IT_PROG’)
AND job_id!=‘IT_PROG’
# perhaps
SELECT employee_id,last_name,job_id,salary
FROM employees
WHERE salary<ALL(
SELECT salary
FROM employees
WHERE job_id=‘IT_PROG’)
AND job_id!=‘IT_PROG’
# Query the Department with the lowest average wage id
# Mode one : Or is it a way to use any,all This is easy to understand
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)<=ALL(
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
# Mode two : The three field aliases must have , Otherwise, the report will be wrong ,every derived table must have its own itias
# Each reserved table should have its own alias
SELECT department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)=(
SELECT MIN(avg_sal)
FROM (SELECT AVG(salary)avg_sal
FROM employees
GROUP BY department_id)dept_avg_sal
)
# Look at the first mistake below , The second correct : Nested functions should be named by their aliases
SELECT MIN(AVG(salary))
FROM (
SELECT AVG(salary)
FROM employees
GROUP BY department_id
)
SELECT MIN(ll)
FROM (
SELECT AVG(salary)ll
FROM employees
GROUP BY department_id
)kkk
# Correlation subquery : More important , Every time, you need to make an internal query
# review : Query employees whose salary is greater than the average salary of the Department last_name,salary And its department_id
SELECT last_name,salary,department_id
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
)
# Query employees whose salary is greater than 60 Of the average salary of department No last_name,salary And its department_id
SELECT last_name,salary,department_id
FROM employees
WHERE salary>(
SELECT AVG(salary)
FROM employees
WHERE department_id=60
)
# Correlation subquery : Query employees whose salary is greater than 60 Of the average salary of department No last_name,salary And its department_id Query employees whose salary is greater than the average salary of the Department last_name,salary And its department_id
SELECT last_name,salary,department_id
FROM employees e1
WHERE salary>(
SELECT AVG(salary)
FROM employees
WHERE department_id=e1.department_id
)
# stay from Use subqueries in
SELECT last_name,salary,e1.department_id
FROM employees e1,(
SELECT department_id,AVG(salary)dept_avg_sal # It appears as a field of the table instead of a function , Alias is required
FROM employees
GROUP BY department_id)e2
WHERE e1.department_id=e2.department_id
AND e2.dept_avg_sal<e1.salary;
# Check the employee's id,salary, according to department_name Sort
SELECT e1.department_id,employee_id,salary
FROM employees e1
ORDER BY(
SELECT department_name
FROM departments e2
WHERE e1.department_id=e2.department_id
)ASC;
# Check the employee's id,salary, according to department_name Sort
# If there are fields in the table of the query and between the two tables join, The fields of the table should be specified
SELECT employee_id,salary
FROM employees e1
ORDER BY(
SELECT department_id
FROM departments e2
WHERE e1.department_id=e2.department_id
);
# if employees In the table employee_id And job_history In the table employee_id The same number is not less than 2, Output these same
#id The employees' employee_id,last_name,job_id
# The following is wrong
SELECT employees.employee_id,last_name,employees.job_id
FROM employees JOIN job_history
ON employees.employee_id=job_history.department_id;
# The correct ones are as follows ,count(*) Can be written as count( Field name )
SELECT e.employee_id,last_name,e.job_id
FROM employees e
WHERE 2<=(
#select count() // Sure count() perhaps count( Field )
SELECT COUNT(employee_id)
FROM job_history
WHERE employee_id=e.employee_id
);
#exists And not exists keyword
# The following two questions are self connected
# Query the company employees employee_id,last_name,job_id,department_id Information
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE employee_id NOT IN(
SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL
);
# Query company managers employee_id,last_name,job_id,department_id Information : Self join , want distinct De emphasis on managers
SELECT DISTINCT e2.employee_id,e2.last_name,e2.job_id,e2.department_id
FROM employees e1
JOIN employees e2
ON e1.manager_id=e2.employee_id
# Query company managers employee_id,last_name,job_id,department_id Information : Subquery used
SELECT employee_id,last_name,job_id,department_id
FROM employees
WHERE employee_id IN(
SELECT manager_id
FROM employees
);
# Query company managers employee_id,last_name,job_id,department_id Information : use exists Realization
SELECT employee_id,last_name,job_id,department_id
FROM employees e1
WHERE EXISTS(
SELECT * FROM employees e2
WHERE e1.employee_id=e2.manager_id
)
# Inquire about departments In the table , There is no such thing as employees Of the departments in the table department_id and department_name
# That is, find out that there are no employees in some departments
# Mode one :
SELECT d.department_id,d.department_name
FROM employees e RIGHT JOIN departments d
ON e.department_id=d.department_id
WHERE e.department_id IS NULL;
# Mode two
SELECT department_id,department_name
FROM departments d
WHERE EXISTS(
SELECT* FROM employees e
WHERE d.department_id=e.department_id
);
边栏推荐
猜你喜欢
随机推荐
(php毕业设计)基于php水果销售商店管理系统获取
JS!!
浅谈数字藏品与实体如何相互赋能
Invalid packaging for parent POM x, must be “pom“ but is “jar“ @
uniapp uview组件库的使用方法(下载方式)
1: Enable slow query log and find slow SQL
Installation and use of flinkx
命令注入绕过方式总结
Some problems of ArcGIS Engine Installation
(PHP graduation project) obtained based on PHP student homework submission management system
JS中的!!
疫情当下,线下文旅受困,看数字藏品能否解围?
Cad-gis data conversion
书籍-清醒思考的艺术
Books - Templeton teaches you reverse
JS macro task and micro task
话题功能实现
MarsNFT :个人如何发行数字藏品?
登录时密码错误次数过多,对该用户进行封禁,
regular expression








