【sql】sql-Learn,Basic grammar(持续更新)

https://www.bilibili.com/video/bv12b411k7zu?p=12&spm_id_from=p
agedriver

-h localhost -p 3306 -u root -p
主机 端口 用户 密码

登录: mysql -h 主机名 -p 端口号 -u 用户名 -p密码

p12
mysql常见命令:

show databases;

use test; 使用test库

show tables; 在test库里面找库;
show tables from mysql; 在mysql里面找表

select databases(); 选择数据库。

create table stuinfo(
-> id int,
-> name varchar(20)); 创建表

desc stuinfo; 查看表格属性

select * from stuinfo; 查看数据。

insert into stuinfo(id,name) values(1,“john”); 插入数据。

update stuinfo set name=“lilei” where id = 1; 修改数据库

delete from studio where id = 1; 删除数据库

select version(); 查看mysql的版本。

mysql 不区分大小写。

注释方法: --(空格)注释
#注释
/*
注释
*/

dql语言学习:

#启用指定库
use myemployees;

基础查询:select 查询列表 from 表名;
{
1.查单个字段
select last_name from employees;

2.查多个字段
select last_name,salary,email from employees;

3.查所有
select * from employees;

4.查询常亮值
select 100;
select 'john';

5.查询表达式
select 100*98;
select 100%98;

6.查询函数
select version();

7.起别名——》 防止链接查询重名
select 100%98 as 结果;
select last_name as 姓,first_name as 名 from employees;
select last_name 姓,first_name 名 from employees;

查询salary并起别名
select salary as 'out put' from employees

8.去重 distinct
select distinct department_id from employees;

9.连接两个字段为一个 concat
select concat(last_name,first_name) as 姓名;

null进行拼接将会变为null	

}
2.条件查询
{
select 查询列表 from 表名 where 筛选条件;

1.条件运算符
	>,<,=,!=,<>(不等于),>=,<=

	select * from employees where salary>12000;
	select last_name,department_id from employees where 

department_id!=90;

2.逻辑运算符
	&& || !
	
	
	
	
3.模糊查询
	like 
	between ... and 
	in
	is null
	
	通配符:
	% 任意多个字符
	_ 任意单个字符
	\ 转义符号  匹配特殊字符
		(或者 like _$_% escape $)
	
	
	
	#筛选员工名中包含字符a的员工信息
	#% -> 任意,通配符
	select * from employees where last_name like '%a%';  
	
	#筛选员工编号为100到200之间的(包含边界)
	select * from employees where employee_id between 

100 and 200;

	#查询员工工种编号是 it_prog、ad_vp、ad_pres
	select * from employees where employees where job_id 

in(‘it_prog’、‘ad_vp’、‘ad_pres’)

	#查询没有奖金的员工名和奖金率
	#null 不能用条件判断符判断
	select last_name,commission_pct from employees where 

commission_pct is null;

	#安全等于  <=>  可判断null
	select last_name,salary from employees where salary 

<=> 12000

}

https://www.bilibili.com/video/BV12b411K7Zu?p=12&spm_id_from=p
ageDriver

-h localhost -P 3306 -u root -p
主机 端口 用户 密码

登录: mysql -h 主机名 -P 端口号 -u 用户名 -p密码

p12
mysql常见命令:

show databases;

use test; 使用test库

show tables; 在test库里面找库;
show tables from mysql; 在mysql里面找表

select databases(); 选择数据库。

create table stuinfo(
-> id int,
-> name varchar(20)); 创建表

desc stuinfo; 查看表格属性

select * from stuinfo; 查看数据。

insert into stuinfo(id,name) values(1,“john”); 插入数据。

update stuinfo set name=“lilei” where id = 1; 修改数据库

delete from studio where id = 1; 删除数据库

select version(); 查看mysql的版本。

mysql 不区分大小写。

注释方法: --(空格)注释
#注释
/*
注释
*/

DQL语言学习:

#启用指定库
USE myemployees;

基础查询:select 查询列表 from 表名;
{
1.查单个字段
select last_name FROM employees;

2.查多个字段
SELECT last_name,salary,email FROM employees;

3.查所有
SELECT * FROM employees;

4.查询常亮值
SELECT 100;
SELECT 'john';

5.查询表达式
SELECT 100*98;
SELECT 100%98;

6.查询函数
SELECT VERSION();

7.起别名——》 防止链接查询重名
SELECT 100%98 AS 结果;
SELECT last_name AS 姓,first_name AS 名 FROM employees;
SELECT last_name 姓,first_name 名 FROM employees;

查询salary并起别名
SELECT salary AS 'out put' FROM employees

8.去重 DISTINCT
SELECT DISTINCT department_id FROM employees;

9.连接两个字段为一个 CONCAT
SELECT CONCAT(last_name,first_name) as 姓名;

null进行拼接将会变为null	

}
2.条件查询
{
select 查询列表 from 表名 where 筛选条件;

1.条件运算符
	>,<,=,!=,<>(不等于),>=,<=

	select * from employees where salary>12000;
	select last_name,department_id from employees where 

department_id!=90;

2.逻辑运算符
	&& || !

}
3.模糊查询
{
like
between … and
in
is null

	通配符:
	% 任意多个字符
	_ 任意单个字符
	\ 转义符号  匹配特殊字符
		(或者 like _$_% escape $)
	
	
	
	#筛选员工名中包含字符a的员工信息
	#% -> 任意,通配符
	select * from employees where last_name like '%a%';  
	
	#筛选员工编号为100到200之间的(包含边界)
	select * from employees where employee_id between 

100 and 200;

	#查询员工工种编号是 IT_PROG、AD_VP、AD_PRES
	select * from employees where employees where job_id 

IN(‘IT_PROG’、‘AD_VP’、‘AD_PRES’)

	#查询没有奖金的员工名和奖金率
	#NULL 不能用条件判断符判断
	select last_name,commission_pct from employees where 

commission_pct IS NULL;

	#安全等于  <=>  可判断NULL
	select last_name,salary from employees where salary 

<=> 12000

}
4.排序查询 P39
{
order by asc\desc (升序\降序)

select * from employees order by salary desc;

#支持别名排序
	select *,salary*12*(1+ifnull(commission_pct,0)) as 年薪
	from employees
	order by 年薪 desc;
	
#按员工名字长度排序
	select length(last_name) as 字节长度,last_name,salary
	from employees
	order by 字节长度 asc;
	
#多重排序 
#工资升序,员工编号降序
	select * from employees
	order by salary asc,employee_id	desc;

}

5.常见函数
{
select 函数名(实参) 【from 表】;

1.字符函数
	(1) length 字符长度
	select length('123');
	
	(2) concat 拼接函数
	select concat(last_name,'_',first_name) as 姓名 from employees;
	
	(3) upper、lower 大小写
	select concat(upper(last_name),lower(first_name)) from employees;
	
	(4) substr,substring  截取字符串,sql中索引从1开始
		
	
	select SUBSTR('今天天气真不错',4);
	#结果--> 气真不错
	
	select SUBSTR('今天天气真不错',1,3);
	#结果--> 今天天
	
	#实现首字符大写
	select concat(upper(substr(last_name,1,1)),'_',lower(substr(last_name,2))) as output;
	
	(5) instr 得到子串在大串里面的索引位置。
	select instr("今天天气真好",'天气') as output;
	#结果--> 3
	
	(6) trim 修剪
	select length(trim("          123    ")) as output;
	select trim("a" from "aaaaaaa1aaaa2aaa3aaa") as output;
	
	(7) lpad 左填充指定长度
		rpad 右填充指定长度
	select lpad('你好',10,'*') as output
	
	(8) replace 替换
	select replace("猪猪今天天气真不错",'猪猪','你') as output;


2. 数学函数
(1)round 四舍五入
select round(1.55);
select round(1.567,2);

(2)ceil 向上取整
select ceil(0.22);--->   1

(3)floor 向下取整
select floor(9.99);

(4)truncate 截断
select truncate(1.69999,1);----->1.6

(5)mod 取余
select mod(10,3);  ---->1

3.日期函数
(1)now  当前时间
select now();

(2)curdate 返回当前日期,不包含时间
select curdate();

(3)curtime 返回时间,不包含日期
select curtime();

(4)指定特定部分年月日。。。
select year(now());  --> 2021
select mouth(now()); --> 12
date
hour
minute
second

(5)str_to_date 字符转换成指定格式日期
select str_to_date('9-13-1999','%m-%d-%Y');  

(6)date_format 将日期转换成字符
select date_format(now(),'%Y_%m_%d')

4.其它函数
(1)version();

(2)databases();

(3)user();

5.流程控制函数
(1) if(判断条件,ture输出值,false输出值)
select if(10<5,"大","小");

(2)case 

#常量判别
   case 参数
   when 常量1 then ...
   when 常量2 then ...
   else ...
   end
   
   select salary 原始工资,department_id,
	case department_id
	when 30 then salary*1.2
	when 40 then salary*1.3
	when 50 then salary*1.4
	else salary
	end 新工资
	from employees;
	
#多重if
	case 
	when 条件1 then ...;
	when 条件2 then ...;
	...
	else ....;
	end
	
语句加;  值不加

}

二 分组函数
{
用作统计使用,又称为聚合函数或统计函数或组函数
(1)sum,avg,min,max,count
select SUM(salary) from employees;

select sum(salary) as 和 ,avg(salary) as 平均,
min(salary) as 最低, max(salary) as 最高, count(salary) as 数量
from employees;


统计行数-->   count(*)\  select count(1) from employees;



(2)分组查询  group by
	select 分组函数,列
	from 表
	【where 筛选条件】
	group by 分组的列表
	【order by 子句】
	【having 条件】  #分组后再筛选
	
	#查询每个工种的最大工资,不统计一人的job_id
	select max(salary),job_id from employees
	group by job_id
	having count(*)>=2;
	
	分组后条件
	select min(salary),manager_id from employees
	where manager_id > 103
	group by manager_id
	having min(salary) > 5000;
	
	优先使用分组前筛选-->性能好
	
	#筛选员工姓名长度分组,查询每一组员工个数,筛选员工个数>5的
	select count(*),length(last_name) from employees group by length(last_name) having count(*)>5;
	
	
	#按多个字段分组
	select avg(salary),department,job_id from employees
	group by department,job_id;
	
	#添加排序
	select avg(salary),department,job_id from employees
	group by department,job_id
	having avg(salary) > 10000;
	order by avg(salary) desc;
	
	#查每个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
	select manager_id,min(salary) as low from employees
	where salary >= 6000
	group by manager_id
	having not isnull(manager_id)
	order by low;

(3)连接查询  	
	select NAME,boyName from beauty,boys
	where beauty.boyfriend_id = boys.id;
	
	#为表起别名
	select e.last_name,e.job_id,j.job_id
	from employees e,jobs j
	where e.job_id = j.job_id;
	
	#查询城市名中第二个字符为o的部门名和城市名
	select department_name,city
	from departments d,locations l
	where d.location_id = l.location_id
	and city like "_o%";
	
	
	
	#等值连接
	a.多表连接的结果为交集
	
	
	#非等值连接
	select salary,grade_level
	from employees as e,job_grades as g
	where salary between g.lowest_sal and g.highest_sal;
	
	#自连接  表格自身的关联关系
	
	#查员工名和上级名称
	select e.employee_id,e.last_name,m.employee_id leader_id,m.last_name leaderName
	from employees as e,employees as m 
	where e.manager_id = m.employee_id;

}

https://www.bilibili.com/video/bv12b411k7zu?p=12&spm_id_from=p
agedriver

-h localhost -p 3306 -u root -p
主机 端口 用户 密码

登录: mysql -h 主机名 -p 端口号 -u 用户名 -p密码

p12
mysql常见命令:

show databases;

use test; 使用test库

show tables; 在test库里面找库;
show tables from mysql; 在mysql里面找表

select databases(); 选择数据库。

create table stuinfo(
-> id int,
-> name varchar(20)); 创建表

desc stuinfo; 查看表格属性

select * from stuinfo; 查看数据。

insert into stuinfo(id,name) values(1,“john”); 插入数据。

update stuinfo set name=“lilei” where id = 1; 修改数据库

delete from studio where id = 1; 删除数据库

select version(); 查看mysql的版本。

mysql 不区分大小写。

注释方法: --(空格)注释
#注释
/*
注释
*/

dql语言学习:

#启用指定库
use myemployees;

基础查询:select 查询列表 from 表名;
{
1.查单个字段
select last_name from employees;

2.查多个字段
select last_name,salary,email from employees;

3.查所有
select * from employees;

4.查询常亮值
select 100;
select 'john';

5.查询表达式
select 100*98;
select 100%98;

6.查询函数
select version();

7.起别名——》 防止链接查询重名
select 100%98 as 结果;
select last_name as 姓,first_name as 名 from employees;
select last_name 姓,first_name 名 from employees;

查询salary并起别名
select salary as 'out put' from employees

8.去重 distinct
select distinct department_id from employees;

9.连接两个字段为一个 concat
select concat(last_name,first_name) as 姓名;

null进行拼接将会变为null	

}
2.条件查询
{
select 查询列表 from 表名 where 筛选条件;

1.条件运算符
	>,<,=,!=,<>(不等于),>=,<=

	select * from employees where salary>12000;
	select last_name,department_id from employees where 

department_id!=90;

2.逻辑运算符
	&& || !
	
	
	
	
3.模糊查询
	like 
	between ... and 
	in
	is null
	
	通配符:
	% 任意多个字符
	_ 任意单个字符
	\ 转义符号  匹配特殊字符
		(或者 like _$_% escape $)
	
	
	
	#筛选员工名中包含字符a的员工信息
	#% -> 任意,通配符
	select * from employees where last_name like '%a%';  
	
	#筛选员工编号为100到200之间的(包含边界)
	select * from employees where employee_id between 

100 and 200;

	#查询员工工种编号是 it_prog、ad_vp、ad_pres
	select * from employees where employees where job_id 

in(‘it_prog’、‘ad_vp’、‘ad_pres’)

	#查询没有奖金的员工名和奖金率
	#null 不能用条件判断符判断
	select last_name,commission_pct from employees where 

commission_pct is null;

	#安全等于  <=>  可判断null
	select last_name,salary from employees where salary 

<=> 12000

}

https://www.bilibili.com/video/BV12b411K7Zu?p=12&spm_id_from=p
ageDriver

-h localhost -P 3306 -u root -p
主机 端口 用户 密码

登录: mysql -h 主机名 -P 端口号 -u 用户名 -p密码

p12
mysql常见命令:

show databases;

use test; 使用test库

show tables; 在test库里面找库;
show tables from mysql; 在mysql里面找表

select databases(); 选择数据库。

create table stuinfo(
-> id int,
-> name varchar(20)); 创建表

desc stuinfo; 查看表格属性

select * from stuinfo; 查看数据。

insert into stuinfo(id,name) values(1,“john”); 插入数据。

update stuinfo set name=“lilei” where id = 1; 修改数据库

delete from studio where id = 1; 删除数据库

select version(); 查看mysql的版本。

mysql 不区分大小写。

注释方法: --(空格)注释
#注释
/*
注释
*/

DQL语言学习:

#启用指定库
USE myemployees;

基础查询:select 查询列表 from 表名;
{
1.查单个字段
select last_name FROM employees;

2.查多个字段
SELECT last_name,salary,email FROM employees;

3.查所有
SELECT * FROM employees;

4.查询常亮值
SELECT 100;
SELECT 'john';

5.查询表达式
SELECT 100*98;
SELECT 100%98;

6.查询函数
SELECT VERSION();

7.起别名——》 防止链接查询重名
SELECT 100%98 AS 结果;
SELECT last_name AS 姓,first_name AS 名 FROM employees;
SELECT last_name 姓,first_name 名 FROM employees;

查询salary并起别名
SELECT salary AS 'out put' FROM employees

8.去重 DISTINCT
SELECT DISTINCT department_id FROM employees;

9.连接两个字段为一个 CONCAT
SELECT CONCAT(last_name,first_name) as 姓名;

null进行拼接将会变为null	

}
2.条件查询
{
select 查询列表 from 表名 where 筛选条件;

1.条件运算符
	>,<,=,!=,<>(不等于),>=,<=

	select * from employees where salary>12000;
	select last_name,department_id from employees where 

department_id!=90;

2.逻辑运算符
	&& || !

}
3.模糊查询
{
like
between … and
in
is null

	通配符:
	% 任意多个字符
	_ 任意单个字符
	\ 转义符号  匹配特殊字符
		(或者 like _$_% escape $)
	
	
	
	#筛选员工名中包含字符a的员工信息
	#% -> 任意,通配符
	select * from employees where last_name like '%a%';  
	
	#筛选员工编号为100到200之间的(包含边界)
	select * from employees where employee_id between 

100 and 200;

	#查询员工工种编号是 IT_PROG、AD_VP、AD_PRES
	select * from employees where employees where job_id 

IN(‘IT_PROG’、‘AD_VP’、‘AD_PRES’)

	#查询没有奖金的员工名和奖金率
	#NULL 不能用条件判断符判断
	select last_name,commission_pct from employees where 

commission_pct IS NULL;

	#安全等于  <=>  可判断NULL
	select last_name,salary from employees where salary 

<=> 12000

}
4.排序查询 P39
{
order by asc\desc (升序\降序)

select * from employees order by salary desc;

#支持别名排序
	select *,salary*12*(1+ifnull(commission_pct,0)) as 年薪
	from employees
	order by 年薪 desc;
	
#按员工名字长度排序
	select length(last_name) as 字节长度,last_name,salary
	from employees
	order by 字节长度 asc;
	
#多重排序 
#工资升序,员工编号降序
	select * from employees
	order by salary asc,employee_id	desc;

}

5.常见函数
{
select 函数名(实参) 【from 表】;

1.字符函数
	(1) length 字符长度
	select length('123');
	
	(2) concat 拼接函数
	select concat(last_name,'_',first_name) as 姓名 from employees;
	
	(3) upper、lower 大小写
	select concat(upper(last_name),lower(first_name)) from employees;
	
	(4) substr,substring  截取字符串,sql中索引从1开始
		
	
	select SUBSTR('今天天气真不错',4);
	#结果--> 气真不错
	
	select SUBSTR('今天天气真不错',1,3);
	#结果--> 今天天
	
	#实现首字符大写
	select concat(upper(substr(last_name,1,1)),'_',lower(substr(last_name,2))) as output;
	
	(5) instr 得到子串在大串里面的索引位置。
	select instr("今天天气真好",'天气') as output;
	#结果--> 3
	
	(6) trim 修剪
	select length(trim("          123    ")) as output;
	select trim("a" from "aaaaaaa1aaaa2aaa3aaa") as output;
	
	(7) lpad 左填充指定长度
		rpad 右填充指定长度
	select lpad('你好',10,'*') as output
	
	(8) replace 替换
	select replace("猪猪今天天气真不错",'猪猪','你') as output;


2. 数学函数
(1)round 四舍五入
select round(1.55);
select round(1.567,2);

(2)ceil 向上取整
select ceil(0.22);--->   1

(3)floor 向下取整
select floor(9.99);

(4)truncate 截断
select truncate(1.69999,1);----->1.6

(5)mod 取余
select mod(10,3);  ---->1

(6)rand() 获取随机数、返回0-1

3.日期函数
(1)now  当前时间
select now();

(2)curdate 返回当前日期,不包含时间
select curdate();

(3)curtime 返回时间,不包含日期
select curtime();

(4)指定特定部分年月日。。。
select year(now());  --> 2021
select mouth(now()); --> 12
date
hour
minute
second

(5)str_to_date 字符转换成指定格式日期
select str_to_date('9-13-1999','%m-%d-%Y');  

(6)date_format 将日期转换成字符
select date_format(now(),'%Y_%m_%d')

4.其它函数
(1)version();

(2)databases();

(3)user();

(4)md5("字符")   得到md5的加密形式。

5.流程控制函数
(1) if(判断条件,ture输出值,false输出值)
select if(10<5,"大","小");

(2)case 

#常量判别
   case 参数
   when 常量1 then ...
   when 常量2 then ...
   else ...
   end
   
   select salary 原始工资,department_id,
	case department_id
	when 30 then salary*1.2
	when 40 then salary*1.3
	when 50 then salary*1.4
	else salary
	end 新工资
	from employees;
	
#多重if
	case 
	when 条件1 then ...;
	when 条件2 then ...;
	...
	else ....;
	end
	
语句加;  值不加

}

二 分组函数
{
用作统计使用,又称为聚合函数或统计函数或组函数
(1)sum,avg,min,max,count
select SUM(salary) from employees;

select sum(salary) as 和 ,avg(salary) as 平均,
min(salary) as 最低, max(salary) as 最高, count(salary) as 数量
from employees;


统计行数-->   count(*)\  select count(1) from employees;



(2)分组查询  group by
	select 分组函数,列
	from 表
	【where 筛选条件】
	group by 分组的列表
	【order by 子句】
	【having 条件】  #分组后再筛选
	
	#查询每个工种的最大工资,不统计一人的job_id
	select max(salary),job_id from employees
	group by job_id
	having count(*)>=2;
	
	分组后条件
	select min(salary),manager_id from employees
	where manager_id > 103
	group by manager_id
	having min(salary) > 5000;
	
	优先使用分组前筛选-->性能好
	
	#筛选员工姓名长度分组,查询每一组员工个数,筛选员工个数>5的
	select count(*),length(last_name) from employees group by length(last_name) having count(*)>5;
	
	
	#按多个字段分组
	select avg(salary),department,job_id from employees
	group by department,job_id;
	
	#添加排序
	select avg(salary),department,job_id from employees
	group by department,job_id
	having avg(salary) > 10000;
	order by avg(salary) desc;
	
	#查每个管理者手下员工的最低工资,其中最低工资不能低于6000,没有管理者的员工不计算在内
	select manager_id,min(salary) as low from employees
	where salary >= 6000
	group by manager_id
	having not isnull(manager_id)
	order by low;

(3)连接查询  	
	select NAME,boyName from beauty,boys
	where beauty.boyfriend_id = boys.id;
	
	#为表起别名
	select e.last_name,e.job_id,j.job_id
	from employees e,jobs j
	where e.job_id = j.job_id;
	
	#查询城市名中第二个字符为o的部门名和城市名
	select department_name,city
	from departments d,locations l
	where d.location_id = l.location_id
	and city like "_o%";
	
	
	
	#等值连接
	a.多表连接的结果为交集
	
	
	#非等值连接
	select salary,grade_level
	from employees as e,job_grades as g
	where salary between g.lowest_sal and g.highest_sal;
	
	
	#sql 99 语法
		select 查询列表
		from 表1 as name1 【连接类型】
		join 表2 as 别名
		on 连接条件
		【where 条件】..;
		
	#内连接  inner
	#只输出两个表交集的地方
		select 查询列表
		from 表1 as name1 inner
		join 表2 as 别名
		on 连接条件;
		
		select last_name,department_name
		from employees e,department d
		on e.department_id = d.department_id;
	
		#连接三表并做排序
		select e.last_name,d.department_name,j.job_title from employees as e
		inner join departments as d on e.department_id = d.department_id
		inner join jobs as j on e.job_id = j.job_id
		order by d.department_name desc;
		
		#自连接
		select e.last_name,m.last_name
		from employees as e
		join employees as m
		on e.manager_id = m.manager_id;
		
	#外连接
	#输出连接的地方和 主表中不满足连接的地方
		外连接 = 内连接结果+ 主表有而从表没有的记录

		左外   left 【outer】 left 左边是主表
		右外   right 【outer】右边是主表
		全外   full 【outer】 
		
		#左外
		select g.name 
		from boys bo 
		left outer join beauty g
		on g.boyfriend_id = bo.id;
		
		#右外
		select g.name
		from boys bo
		right outer join beauty g
		on g.boyfriend_id = bo.boyfriend_id;
		
		#全外连接
		求并集
		
		
	#交叉连接
	
		#求笛卡尔乘积
		select g.*,bo.*
		from beauty g
		cross join boys bo;
		
		
	#子查询 : 出现在其他语句中的select语句。
	标量子查询  一行一列   select
	列子查询  一列多行  where,having
	行子查询  一行多列	where,having
	表子查询  多行多列  from
			
	select first_name from employees where
	department_id in (
		select department_id from departments
		where location_id = 1700
	)
	
	#标量子查询
	#谁的工资比Abel高
	#子编码
	select salary
	from employees
	where last_name = 'Abel'
	
	select * from employees
	where salary > (
		select salary
		from employees
		where last_name = 'Abel'
	);
	
	# 工资大于 143 号,job_id = 141号
	select last_name from employees
	where salary > (
		select salary from employees
		where employee_id = 143
	)
	and job_id = (
		select job_id from employees
        where employee_id = 141
    );
	
	#最低工资大于50号部门最低工资的部门id和其最低工资
	select MIN(salary),department_id from employees
	group by department_id
	having MIN(salary) > (
		select MIN(salary) from employees
		where department_id = 50
	);
	
	#多行子查询
	IN NOT IN    a IN(10,20,30)
	
	any/some    a > any(10,20,30)  ==>  a > min(10,20,30)
	
	all         a > all(10,20,30)  ==> a > 10 && a>20 && a >30

	#返回location_id 是1400 或 1700 的部门的所有员工姓名
	#子查询
	select department_id from employees
	where location_id IN (1400,1700)
	
	
	select last_name from employees
	where department_id  = any(
		select department_id from departments
		where location_id IN (1400,1700)
	);
	
	#一行多列
	查员工编号最小,并且工资最高的员工信息
		
	select * from employees
	where (employee_id,salary) = (
			select MIN(employee_id),MAX(salary) from employees
	);
	
	
	#表
	#查询每个部门的平均工资的工资等级
	#①子查询
	(select avg(salary) from employees 
	group by department_id) as avg_Dep;
	#②
	select grade_level from grade_level as grade;
	
	select avg_dep.*,grade.grade_level from (
		select avg(salary) as avg_sal, department_id from employees 
		group by department_id
	) as avg_dep
	inner join job_grades as grade
	on avg_dep.avg_sal between lowest_sal and highest_sal;
	
	
	#exist后面 (相关子查询)
	exist(完整查询语句) ---> 返回  1或0
	
	
	
	#查询各部门平均工资 比本部门平均工资高的员工的员工号,姓名和工资
	#
	select avg(salary) from employees
	group by department_id;
	

	
	select employee_id,last_name,salary from employees as e 
	inner join (
		select avg(salary) avg_sal,department_id from employees
		group by department_id
	) as new_mat
    on e.department_id = new_mat.department_id
    where salary > new_mat.avg_sal;
	
	
	#分页查询
	放在最后
	limit offset,size --> 起始位置(从0开始),大小
	
	#查询前5个员工信息
	select * from employees
	limit 0,5;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值