IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> SQL_select_题型总结 -> 正文阅读

[大数据]SQL_select_题型总结

涉及的数据库:链接:https://pan.baidu.com/s/17p2QxOt-wlLuTciQchm6Qw
提取码:1234

1.运算符

#1.算数运算符:+ - * / div % mod
#'a'此时看作0处理
SELECT 100+'a';

#null 值参与运算,结果为null
SELECT 100+NULL;

#1.选择工资不在5000和12000的员工的姓名和工资
SELECT
	`last_name`,
	`salary`
FROM
	`employees`
WHERE 
	`salary` NOT BETWEEN 5000 AND 12000;

#2.选择在20或者50号部门工作的员工姓名和部门号
SELECT
	`last_name`,
	`department_id`
FROM
	`employees`
#where `department_id`in(20,50)
WHERE 
	`department_id`=20 OR `department_id`=50;

#3.选择公司中没有管理者的员工姓名和`job_id`
SELECT
	`last_name`,
	`job_id`
FROM
	`employees`
WHERE 
	`manager_id` IS NULL;
	
#4.选择公司中有奖金的员工姓名,工资和奖金级别
SELECT
	`last_name`,
	`salary`
FROM
	`employees`
WHERE 
	`commission_pct` IS NOT NULL;
	
#5.选择员工姓名的第三个字母是a的员工姓名
SELECT
	`last_name`
FROM
	`employees`
WHERE 
	`last_name` LIKE '__a%';

#6.选择姓名中有字母a和k的员工姓名
SELECT
	`last_name`
FROM
	`employees`
WHERE 
	`last_name`LIKE '%a%' OR
	`last_name`LIKE '%k%';

#7.显示出表 `employees` 中 `first_name`以 ‘e’结尾的员工信息
SELECT
	*
FROM
	`employees`
WHERE
	`first_name`LIKE'%e';

#8.显示出表`employees`部门标号在80——100之间的姓名和工种
SELECT
	`last_name`,
	`job_id`
FROM
	`employees`
WHERE 
	`department_id` BETWEEN 80 AND 100;

#9.显示出表`employees`的`manager_id`是100,101,110 的员工姓名,工资和管理者id
SELECT
	`last_name`,
	`salary`,
	`manager_id`
FROM
	`manager_id` IN (100,101,110);
	

2.排序和分页

#1.查询员工的姓名和部门号和年薪,按年薪降序,按照姓名增序
SELECT
	`last_name`,
	`department_id`,
	salary*(1+IFNULL(`commission_pct`,0)) AS sumsalary
FROM
	`employees`
ORDER BY
	sumsalary DESC,
	`last_name`
	
#2.选择工资不在8000和17000的员工的姓名和工资,按照工资降序,显示第21到40位置的数据
SELECT
	`last_name`,
	`salary`
FROM
	`employees`
WHERE 
	`salary` NOT BETWEEN 8000 AND 17000
ORDER BY
	`salary` DESC
LIMIT 
	20,20;
	
#3.查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按照部门号升序
SELECT
	*
FROM
	`employees`
WHERE 
	`email`LIKE'%e%'
ORDER BY
	LENGTH(`email`) DESC,
	`department_id` ;

3.多表查询

#1.显示所有员工的姓名,部门号和部门名称
SELECT
	e.`last_name`,
	e.`department_id`,
	d.`department_name`
FROM
	`employees` e
#可能存在有员工没分配`department_id`,所有使用左外连接,outer可以省略
LEFT OUTER JOIN 
	`departments` d
ON
	e.`department_id`=d.`department_id`;

#2.查询90号部门员工的job_id和90号部门的location_id
SELECT
	e.`job_id`,
	d.`location_id`
FROM
	`departments` d
INNER JOIN
	`employees` e
ON
	d.`department_id`=e.`department_id`
WHERE 
	d.`department_id`=90;

#3.选择所有有奖金的员工的`last_name`,`department_name`,`location_id`,`city`
SELECT
	e.`last_name`,
	e.`department_id`,
	l.`location_id`,
	l.`city`
FROM
	`departments` d
INNER JOIN
	`locations` l
ON
	d.`location_id`=l.`location_id`
#可能存在有员工没分配`department_id`,`employees`表较大,所有使用右外连接,outer可以省略
RIGHT JOIN 	
	`employees` e
ON
	d.`department_id`=e.`department_id`
WHERE 
	e.`commission_pct`IS NOT NULL; #也应该是35条记录
#验证结果	
SELECT
	COUNT(*)
FROM
	`employees`
WHERE 
	`commission_pct` IS NOT NULL; #35条记录
	
#4.选择`city`在Toronto工作的员工的`last_name`,`job_id`,`department_id`,`department_name`
SELECT
	e.`last_name`,
	e.`job_id`,
	e.`department_id`,
	d.`department_name`
FROM
	`departments` d
INNER JOIN
	`locations` l
ON
	d.`location_id`=l.`location_id`
INNER JOIN
	`employees` e
ON
	d.`department_id`=e.`department_id`
WHERE
	l.`city`='Toronto';
	
	
#5.查询员工所在部门名称,部门地址,姓名,工作,工资,其中员工所在部门的部门名称为‘Executive’
SELECT
	d.`department_name`,
	l.`street_address`,
	e.`last_name`,
	e.`job_id`,
	e.`salary`
FROM
	`employees` e
RIGHT JOIN
	`departments` d
ON
	d.`department_id`=e.`department_id`
INNER JOIN
	`locations` l
ON
	d.`location_id`=l.`location_id`
WHERE
	d.`department_name`='Executive';
	
#6.选择指定员工的姓名,员工号,以及他的管理者的姓名和员工号
SELECT	
	e.`last_name` AS '员工id',
	e.`employee_id`,
	b.`last_name` AS '老板id',
	b.`employee_id`
FROM
	`employees` e
#存在有员工没有老板,所有left join
LEFT JOIN
	`employees` b
ON
	e.`manager_id`=b.`employee_id`;
	
#7.查询哪些部门没有员工
SELECT
	d.`department_id`
FROM
	`departments` d
LEFT JOIN
	`employees` e
ON
	d.`department_id`=e.`department_id`
WHERE 
	e.`department_id` IS NULL;

#8.查询哪些城市没有部门
SELECT
	l.`city`
FROM
	`locations` l
LEFT JOIN
	`departments` d
ON
	l.`location_id`=d.`location_id`
WHERE 
	d.`location_id`IS NULL;
	
#9.查询部门名为Sales或者IT的员工信息
SELECT	
	e.*
FROM
	`employees` e
JOIN
	`departments` d
ON
	e.`department_id`=d.`department_id`
WHERE 
	`department_name`IN('Sales','IT');
	

4.单行函数

#1.显示系统时间
SELECT NOW();

#2.查询员工号,姓名工资,以及工资提高20%之后的结果
SELECT
	`employee_id`,
	`salary`,
	`salary`*(1+0.2) AS newsalary
FROM
	`employees`;
	
#3.将员工的姓名按首字母排序,并写出姓名的长度
SELECT
	`last_name`,
	LENGTH(`last_name`)
FROM
	`employees`
ORDER BY
	 SUBSTR(`last_name`,1,1) ASC;
	 
#4.查询员工id,`last_name`,`salary`并作为一个列输出,别名为OUT_PUT
SELECT
	CONCAT(`employee_id`,`last_name`,`salary`) AS OUT_PUT
FROM
	`employees`;

#5.查询公司员工的工作的年数,工作的天数,并按工作年数的降序排序
#DATEDIFF(date1,date2) date1-date2
#curdate 计算当前时间
SELECT
	`employee_id`,
	DATEDIFF(CURDATE(),`hire_date`) AS workday,
	DATEDIFF(CURDATE(),`hire_date`)/365 AS workyear
FROM
	`employees`
ORDER BY
	workyear DESC;
	
#6.查询员工姓名,`hire_date`,`department_id`,满足:`hire_date`在1997年之和,`department_id`为80或90或100,`commission_pct`不空
SELECT
	`last_name`,
	`hire_date`,
	`manager_id`
FROM
	`employees`
WHERE 
	#`hire_date`>='1997-01-01' #存在隐式转换
	DATE_FORMAT(`hire_date`,'%Y-%m-%d')>='1997-01-01'
	AND
	`department_id`IN(80,90,100)
	AND
	`commission_pct` IS NOT NULL;

#7.查询公司中入职时间超过10000天的员工姓名、入职时间
SELECT
	`last_name`,
	`hire_date`
FROM
	`employees`
WHERE 
	DATEDIFF(CURDATE(),`hire_date`)>=10000;

5. 聚合函数

#2.查询公司员工工资的最大值,最小值,平均值和总和
SELECT
	MAX(salary),
	MIN(salary),
	AVG(salary),
	SUM(salary)
FROM
	`employees`
	
#3.查询各`job_id`的员工工资的最大值,最小值,平均值和总和
SELECT
	MAX(salary),
	MIN(salary),
	AVG(salary),
	SUM(salary)
FROM
	`employees`
GROUP BY
	`job_id`
	
#4.选择具有各个`job_id`的员工个数
SELECT
	`job_id`,
	COUNT(*)
FROM
	`employees`
GROUP BY
	`job_id`;
	
#5.查询员工最高工资和最低工资的差距
SELECT
	MAX(salary)-MIN(salary)
FROM
	`employees`;
	
#6.查询各个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
SELECT
	MIN(salary)
FROM
	`employees`
WHERE 
	`manager_id` IS NOT NULL
GROUP BY
	`manager_id`
HAVING
	MIN(salary)>=6000;

#7.查询所有部门的名字,`location_id`,员工数量和平均工资,并且按照平均工资降序
SELECT
	`department_name`,
	d.`location_id`,
	COUNT(*),
	AVG(`salary`) AS avgsalary
FROM
	`employees` e
RIGHT JOIN
	`departments` d
ON
	e.`department_id`=d.`department_id`
GROUP BY
	d.`department_id`
ORDER BY
	avgsalary DESC;

#8.查询每个(所有)工种、每个部门的部门名工种名和最低工资
SELECT	
	`job_id`,
	`department_name`,
	MIN(salary)
FROM
	`departments` d
LEFT JOIN
	`employees` e
ON
	d.`department_id`=e.`department_id`
GROUP BY
	`department_name`,
	`job_id`

6.子查询

#1、查询和Zlotkey相同部门的员工姓名和工资
SELECT
	`last_name`,
	`salary`
FROM
	`employees` 
WHERE 
	`department_id`=
	(
	SELECT
		`department_id`
	FROM
		`employees`
	WHERE 
		`last_name`='Zlotkey'
	);



#2、查询工资比公司平均工资高的员工的员工号,姓名和工资
SELECT
	`employee_id`,
	`last_name`,
	`salary`
FROM
	`employees`
WHERE 
	`salary`>=(
	SELECT
		AVG(salary)
	FROM
		`employees`
	);

#3、查询工资大于所有`job_id`=‘SA_MAN’的员工的工资的员工的`last_name`,`job_id`,`salary`
SELECT
	`last_name`,
	`job_id`,
	`salary`
FROM
	`employees`
WHERE 
	`salary`>ALL(
	SELECT
		`salary`
	FROM
		`employees`
	WHERE 
		`job_id`='SA_MAN'
	);

#4、查询姓名中包含字母u的员工在相同部门的员工的员工号和姓名
SELECT
	`employee_id`,
	`last_name`
FROM
	`employees`
WHERE 
	`department_id`IN(
	SELECT
		DISTINCT `department_id`
	FROM
		`employees`
	WHERE 
		`last_name` LIKE'%u%'
	);

#5、查询在部门的`location_id`为1700的部门工作的员工的员工号
SELECT
	`employee_id`
FROM
	`employees` e
WHERE 
	e.`department_id` IN(
	SELECT
		d.`department_id`
	FROM
		`departments` d
	WHERE 
		d.`location_id`='1700'
	);
	
#6、查询管理者是King的员工的姓名和工资
SELECT
	e.`last_name`,
	e.`salary`
FROM
	`employees` e
WHERE 
	e.`manager_id`IN
	(
		SELECT
			`employee_id`
		FROM
			`employees`
		WHERE 	
			`last_name`='King'
	);


#7、查询工资最低的员工信息:`last_name`,`salary`
SELECT
	`last_name`,
	`salary`
FROM
	`employees`
WHERE 
	`salary`=
	(
	SELECT
		MIN(salary)
	FROM
		`employees`
	);
	
#8、查询平均工资最低的部门信息
SELECT
	*
FROM
	`departments` d
WHERE 
	d.`department_id`=(
	SELECT
		e.`department_id`
	FROM
		`employees` e
	GROUP BY
		e.`department_id`
	ORDER BY
		AVG(salary) ASC
	LIMIT 1
	)
	
#方法2
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
					FROM
						`employees`
					GROUP BY
						`department_id`)avgsalary
			)
	)
	

#9、查询平均工资最低的部门信息和该部门的平均工资(相关子查询)
SELECT
	d.*,
	(
	SELECT
		AVG(salary)
	FROM
		`employees`
	GROUP BY
		`department_id`
	HAVING
		`department_id`=d.`department_id`
	) min_avg
FROM
	`departments` d
WHERE 
	d.`department_id`=(
		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`)avgsalary
			)
	)
#方式2
SELECT
	d.*,
	(
	SELECT
		AVG(`salary`)
	FROM
		`employees`
	GROUP BY
		`department_id`
	HAVING
		`department_id`=d.`department_id`
	
	
	)min_avg
FROM
	`departments` d
WHERE 
	d.`department_id`=(
	SELECT
		e.`department_id`
	FROM
		`employees` e
	GROUP BY
		e.`department_id`
	ORDER BY
		AVG(salary) ASC
	LIMIT 1
	)

#10、查询平均工资最高的job信息
SELECT
	*
FROM
	`jobs` j
WHERE 
	j.`job_id`=(
	SELECT
		e.`job_id`
	FROM
		`employees` e
	GROUP BY
		e.`job_id`
	ORDER BY
		AVG(salary) DESC
	LIMIT 1
	);

#11、查询平均工资高于公司平均工资的部门有哪些
SELECT
	`department_id`
FROM
	`employees`
WHERE 
	`department_id` IS NOT NULL
GROUP BY
	`department_id`
HAVING AVG(salary)>(
	SELECT
		AVG(salary)
	FROM
		`employees`
	);
	
#12、查询公司中所有manager的详细信息
SELECT
	e1.`employee_id`,
	e1.`first_name`,
	e1.`last_name`
FROM
	`employees` e1
WHERE EXISTS(
	SELECT
		*
	FROM
		`employees` e2
	WHERE 
		e1.`employee_id`=e2.`manager_id`
	)

#13、各个部门中最高工资中最低的那个部门的最低工资是多少
SELECT
	MIN(salary)
FROM
	(
	SELECT
		e1.`salary`
	FROM
		`employees` e1
	WHERE 
		e1.`department_id`=(
		SELECT
			e2.`department_id`
		FROM
			`employees` e2
		GROUP BY
			e2.`department_id`
		ORDER BY
			MAX(e2.salary) ASC
		LIMIT 1
		)
	)e
	
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_sala
		)
	);

#14、查询平均工资最好的部门的manager的详细信息:`last_name`,`department_id`,`email`,`salary`
SELECT
	DISTINCT b.`last_name`,
	b.`department_id`,
	b.`salary`
FROM
	`employees` b
JOIN
	`employees` e
ON
	b.`employee_id`=e.`manager_id`
	AND
	e.`department_id`=
	(
	SELECT
		e1.`department_id`
	FROM
		`employees` e1
	GROUP BY
		e1.`department_id`
	HAVING 
		AVG(e1.salary)=(
		SELECT
			AVG(e2.salary)
		FROM
			`employees` e2
		GROUP BY
			e2.`department_id`
		ORDER BY
			AVG(e2.salary) DESC
		LIMIT 1
	)
	)
#方式2
SELECT
	DISTINCT `last_name`,
	`department_id`,
	`salary`
FROM
	`employees`
WHERE 
	`employee_id`IN(
	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、查询部门的部门号,其中不包括`job_id`是‘ST_CLERK’的部门号
SELECT
	DISTINCT`department_id`
FROM
	`employees`
WHERE 
	`department_id` NOT IN(
	SELECT
		DISTINCT`department_id`
	FROM
		`employees`
	WHERE 
		`job_id`=('ST_CLERK')
	)

	
#16、选择所有没有管理者的员工的`last_name`
SELECT
	e1.`last_name`
FROM
	`employees` e1
WHERE NOT EXISTS(
	SELECT
		*
	FROM
		`employees` e2
	WHERE 
		e1.`manager_id`=e2.`employee_id`
);

#17、查询员工号、姓名、雇佣时间、工资、其中员工的管理者为‘De Haan’
SELECT
	e.`employee_id`,
	e.`last_name`,
	e.`hire_date`,
	e.`salary`
FROM
	`employees` e
WHERE 
	e.`manager_id`IN(
	SELECT
		b.`employee_id`
	FROM
		`employees` b
	WHERE 
		b.`last_name`='De Haan'
	);
#方式2
SELECT
	e.`employee_id`,
	e.`last_name`,
	e.`hire_date`,
	e.`salary`
FROM
	`employees` e
WHERE EXISTS(
	SELECT
		*
	FROM
		`employees` b
	WHERE 
		e.`manager_id`=b.`employee_id`
	AND
		b.`last_name`='De Haan'
	);
	
#18、查询各部门中工资比本部门平均工资高的员工的员工号,姓名和工资
SELECT
	e1.`employee_id`,
	e1.`last_name`,
	e1.`salary`
FROM
	`employees` e1
WHERE 
	e1.salary>(
	SELECT
		AVG(salary)
	FROM
		`employees` e2
	GROUP BY
		e2.`department_id`
	HAVING
		e1.`department_id`=e2.`department_id`
	)
#19、查询每个部门下的部门人数大于5的部门名称
SELECT
	d.`department_name`
FROM
	`departments` d
WHERE 
	5<(
	SELECT
		COUNT(*)
	FROM
		`employees` e
	WHERE
		e.`department_id`=d.`department_id`
	)
#20、查询每个国家下的部门个数大于2的国家编码
SELECT
	l.`country_id`
FROM
	`locations` l
WHERE 
	2<(
	SELECT
		COUNT(*)
	FROM
		`departments` d
	WHERE 
		d.`location_id`=l.`location_id`
	)
  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2022-02-09 20:46:17  更:2022-02-09 20:46:52 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/24 12:51:19-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码