Mysql分组查询 连接查询 子查询 分页查询

Mysql分组查询

案例一 查询每个工种的最高工资

SELECT MAX(salary),job_id
FROM employees
GROUP BY job_id;

案例二 查询每个位置的部门个数

SELECT COUNT(*),location_id
FROM departments
GROUP BY location_id;

案例三 查询邮箱中包含a字符 每个部门的平均工资

SELECT AVG(salary),department_id
FROM employees
WHERE email LIKE '%a%'
GROUP BY department_id;

案例四 查询有奖金的每个领导手下的员工的最高工资

SELECT MAX(salary),manager_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY manager_id;

案例五 查询哪个部门的员工个数大于2

(1) 查询每个部门的员工个数

SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id;

(2) 根据(1)的结果查询哪个部门员工的个数大于2

SELECT COUNT(*),department_id
FROM employees
GROUP BY department_id
HAVING COUNT(*)>2;

案例六 查询每个工种有奖金的员工的最高工资 而且最高工资大于12000的工种编号和最高工资

SELECT MAX(salary),job_id
FROM employees
WHERE commission_pct IS NOT NULL
GROUP BY job_id
HAVING MAX(salary)>12000;

小结

分组查询中的筛选条件分为2类

分组筛选前的数据源是原始表

分组筛选后的数据源是分组后的结果集

在这里插入图片描述

案例七 查询每个部门每个工种的员工的平均工资 并且按照平均工资的高低排序

SELECT AVG(salary),job_id,department_id
FROM employees
GROUP BY job_id,department_id
ORDER BY AVG(salary) DESC;

案例八 查询各个管理者手下员工的最低工资 其中最低工资大于6000 没有管理者的员工不计算在内

SELECT MIN(salary),manager_id
FROM employees
WHERE manager_id IS NOT NULL
GROUP BY manager_id
HAVING MIN(salary)>6000;

案例九 查询所有部门的编号 员工数量和工资平均值 并且按照平均工资降序

SELECT department_id,COUNT(*),AVG(salary)
FROM employees
GROUP BY department_id
ORDER BY AVG(salary) DESC;

Mysql连接查询 (92标准)

案例一 查询有奖金的员工名和部门名

SELECT last_name,department_name
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND e.`commission_pct` IS NOT NULL;

案例二 查询城市名字中第二个字符为o的对应的城市名和部门名

SELECT city,department_name
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
AND city LIKE '%_o%';

案例三 查询每个城市的部门个数

SELECT COUNT(*),city
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY city;

案例四 查询有奖金的每个部门的部门名和部门的领导编号和该部门的最低工资

SELECT d.manager_id,d.department_name,MIN(salary)
FROM employees e,departments d
WHERE e.`department_id`=d.`department_id`
AND e.`commission_pct` IS NOT NULL
GROUP BY department_name;

案例五 查询每个工种的工种名和员工的个数 并且按照员工的个数降序

SELECT job_title,COUNT(*)
FROM jobs j,employees e
WHERE e.`job_id`=j.`job_id`
GROUP BY e.`job_id`
ORDER BY COUNT(*) DESC;

案例六 查询员工名和部门名和所在的城市(三表连接)

SELECT last_name,department_name,city
FROM employees e,locations l,departments d
WHERE	e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`;

案例七 选择所有有奖金的员工的名字 部门名称 位置编号 城市名字

SELECT last_name,department_name,d.location_id,l.city
FROM employees e,departments d,locations l
WHERE e.`department_id`=d.`department_id`
AND d.`location_id`=l.`location_id`
AND e.`commission_pct` IS NOT NULL;

案例八 查询每个工种 每个部门的部门名 工种名和最低工资

SELECT d.department_name,j.job_title,MIN(salary)
FROM employees e,departments d,jobs j
WHERE e.`department_id`=d.`department_id`
AND e.`job_id`=j.`job_id`
GROUP BY department_name,j.job_id;

案例九 查询每个国家下的部门个数大于2的国家的编号

SELECT country_id,COUNT(*)
FROM departments d,locations l
WHERE d.`location_id`=l.`location_id`
GROUP BY country_id
HAVING COUNT(*)>2;

Mysql连接查询 (99标准)

语法

select 查询列表
		from 表1 别名【连接类型】
		join   表2 别名  
		on 连接条件
		【where  筛选条件】
		【group by 分组】
		【having 筛选条件】

案例一到五展示内连接

案例一 查询员工名和部门名

SELECT last_name,department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`;

案例二 查询名字中包含e的员工名和工种名

SELECT last_name,job_title
FROM employees e
INNER JOIN jobs j
ON e.`job_id`=j.`job_id`
WHERE e.`last_name` LIKE '%e%';

案例三 查询部门个数>3的城市名和部门个数

SELECT city,COUNT(*)
FROM locations l
INNER JOIN departments d
ON d.`location_id`=l.`location_id`
GROUP BY city
HAVING COUNT(*)>3;

案例四 查询哪个部门的员工个数>3 显示查询出的部门结果的部门名和员工个数 并且按照个数降序

SELECT COUNT(*),department_name
FROM employees e
INNER JOIN departments d
ON e.`department_id`=d.`department_id`
GROUP BY e.department_id
HAVING COUNT(*)>3
ORDER BY COUNT(*) DESC;

案例五 查询员工名 部门名 工种名 并且按照部门名降序

SELECT last_name,department_name,job_title
FROM employees e
INNER JOIN departments d ON e.`department_id`=d.`department_id`
INNER JOIN jobs j ON e.`job_id`=j.`job_id`
ORDER BY d.department_name DESC;

小结

筛选条件放在where后面 连接条件放在on后面 提高分离性 便于阅读

支持排序 分组 筛选

案例六到八展示外连接

案例六 查询哪个部门没有员工

SELECT department_name,e.`employee_id`
FROM departments  d 
LEFT OUTER JOIN employees e
ON d.`department_id`=e.`department_id`
WHERE e.`employee_id` IS NULL;

案例七 查询哪个城市没有部门

SELECT city,d.department_name
FROM locations l
LEFT OUTER JOIN departments d
ON d.`location_id`=l.`location_id`
WHERE d.`department_name` IS NULL;

案例八 查询部门名为SAL或IT的员工信息

SELECT e.`last_name`,e.`employee_id`,d.`department_name`
FROM departments d
LEFT OUTER JOIN employees e
ON e.`department_id`=d.`department_id`
WHERE d.`department_name` IN ('SAL','IT');

小结

外连接的查询结果=内连接结果+主表有而从表没有的记录

Mysql子查询

案例一 查询谁的工资比Abel高

SELECT *
FROM employees e
WHERE salary>(
	SELECT salary 
	FROM employees e
	WHERE e.`last_name`='Abel'
);

案例二 返回job_id与141号员工相同 salary比143号员工多的所有员工的姓名 job__id和工资salary

SELECT job_id,last_name,salary
FROM employees e
WHERE salary>(
	SELECT salary
	FROM employees e
	WHERE e.`employee_id`='143'
)AND job_id=(
	SELECT job_id
	FROM employees e
	WHERE	e.`employee_id`='141'
);

案例三 查询最低工资大于50号部门最低工资的部门id和其最低工资

SELECT MIN(salary),department_id
FROM employees e
GROUP BY department_id
HAVING MIN(salary)>(
	SELECT MIN(salary)
	FROM employees e
	WHERE e.`department_id`=50
);

案例四 返回其他工种中比job_id为’IT_PROG’工种任意工资低的员工的工号 名字 job_id 和 salary

SELECT last_name,salary,job_id,employee_id
FROM employees e
WHERE salary<ANY(
SELECT salary 
FROM employees e
WHERE job_id='IT_PROG'
)AND job_id<>'IT_PROG';

案例五 查询每个部门的员工个数

SELECT d.*,(
SELECT COUNT(*)
FROM employees e
WHERE e.department_id=d.`department_id`)
每个部门员工个数
FROM departments d;

案例六 查询和Zlotkey相同部门的员工姓名和工资

SELECT last_name,salary
FROM employees e
WHERE e.`department_id`=(
	SELECT department_id
	FROM employees e
	WHERE e.`last_name`='Zlotkey'
);

案例七 查询工资比公司平均工资高的员工的员工号 姓名 和工资

SELECT last_name,salary,employee_id
FROM employees e 
WHERE e.`salary`>(
	SELECT AVG(salary)
	FROM employees 
);

案例八 查询各部门中工资比本部门平均工资高的员工的员工号 姓名 工资

SELECT last_name,employee_id,salary,e.department_id
FROM employees e
INNER JOIN(
	SELECT AVG(salary) ag,e.department_id
	FROM employees e
	GROUP BY department_id
)ag_dep
ON e.`department_id`=ag_dep.department_id
WHERE e.`salary`>ag_dep.ag;

案例九 查询和姓名中包含字母u的员工在相同部门工作的员工的员工号和姓名

SELECT e.`employee_id`,e.`last_name`
FROM employees e
INNER JOIN (
	SELECT DISTINCT department_id
	FROM employees e
	WHERE e.`last_name` LIKE '%u%'
)dl
ON e.`department_id`=dl.department_id;
SELECT e.`employee_id`,e.`last_name`
FROM employees e
WHERE e.`department_id` IN(
	SELECT DISTINCT department_id
	FROM employees e
	WHERE e.`last_name` LIKE '%u%'
);

案例十 查询平均工资最低的部门信息

方法一

SELECT d.*
FROM departments d
WHERE department_id=(
	SELECT department_id
	FROM employees e
	GROUP BY department_id
	HAVING AVG(salary)=(
		SELECT MIN(mini)
		FROM(
			SELECT AVG(salary) mini,department_id
			FROM employees e
			GROUP BY department_id
		)ad
	)
);

方法二

SELECT *
FROM departments 
WHERE department_id=(
	SELECT department_id
	FROM employees e
	GROUP BY department_id
	ORDER BY AVG(salary)
	LIMIT 1
);

案例十一 查询平均工资最低的部门信息和该部门的平均工资

SELECT *
FROM departments d
INNER JOIN (
	SELECT department_id,AVG(salary)
	FROM employees e
	GROUP BY department_id
	ORDER BY AVG(salary)
	LIMIT 1
)ad
ON d.`department_id`=ad.department_id;

案例十二 查询平均工资最高的工种信息

SELECT *
FROM jobs
WHERE job_id=(
	SELECT job_id
	FROM employees e
	GROUP BY job_id
	ORDER BY AVG(salary) DESC
	LIMIT 1
);

案例十三 查询平均工资高于公司平均工资的部门有哪些

SELECT AVG(salary),department_id
FROM employees
GROUP BY department_id
HAVING AVG(salary)>(
	SELECT AVG(salary)
	FROM employees e
);

案例十四 查询出公司中所有manager的详细信息

SELECT *
FROM employees e
WHERE e.`employee_id` IN(
	SELECT  DISTINCT 
	manager_id
	FROM employees 
);

案例十五 查询平均工资最高的部门的manager的详细信息

SELECT last_name,department_id,email,salary
FROM employees e
WHERE employee_id=(
	SELECT manager_id
	FROM departments d
	WHERE d.`department_id`=(
		SELECT department_id
		FROM employees e
		GROUP BY department_id
		ORDER BY AVG(salary)DESC
		LIMIT 1
	)
);

Mysql 分页查询

案例一 查询前5条的员工信息

SELECT *
FROM employees e 
LIMIT 0,5;

案例二 查询有奖金的员工信息 并且把工资较高的前10名显示出来

SELECT *
FROM employees e
WHERE e.`commission_pct` IS NOT NULL
ORDER BY salary DESC 
LIMIT 10;

你可能感兴趣的:(Mysql)