前言
数据库 (Database) 指的是硬盘中用于存储数据的那块空间;
数据库管理系统 (Database Management System, DBMS) 是用户直观操作数据库的软件。要用特定的编程语言(SQL 语句)操作 DBMS 与数据库进行沟通。
为了方便普通用户,数据库应用系统被开发出来,将 SQL 语句封装成易于理解和操作的按钮等。如浏览网页、逛淘宝、点外卖,都是在操作数据库应用系统,从数据库里取数据。
注1:本文是一枚 SQL 菜鸡为了应对秋招做的一点备忘笔记,不成系统。
注2:但 SQL 查询语句真的很 easy 啊,最起码应付秋招来说,下面的这些内容完全够了。我今天花半个小时复习了一下,刷了牛客上的题,感觉没什么问题了哈哈哈
常用函数
返回记录编号:
row_number() OVER (PARTITION BY COL1 ORDER BY COL2)
表示根据COL1分组,在分组内部根据 COL2排序,而此函数计算的值就表示每组内部排序后的顺序编号(组内连续的唯一的)
可以省略 PARTITION BY
参数(不分组),返回排序后的每一行编号:row_number() OVER (ORDER BY COL2)
。
经典题目“查询连续三天登录的用户”就会用到这个函数。cf: SQL经典题:连续3天登录用户
字符串相关函数:
concat
函数拼接字符串或字段;
select concat(last_name, first_name) as name from employees;
upper
, lower
函数转换大小写;
substr
从索引位置开始截取字符串(SQL 索引从 1 开始!)
left
截取字符串的前 n 个字符
select substr('friends', 1, 4) //从首位开始,截取 4 个字符
// 等价于
select left('friends', 4)
select substr('friends', 4) //从第四个字符开始,直到最后
常用数学函数:
round
, ceil
, floor
, truncate
(截断,不四舍五入)
select round(235.415, 2) AS RoundValue;
流程控制函数:
if
函数:类似三元运算符
select if(grades> 80, 'A', 'B') from students;
case
函数:分情况讨论。有两种用法
select salary as old_salary, department_id,
case department_id
when 30 then salary*1.1
when 40 then salary*1.2
else salary
end
as new_salary from employees;
也可以当作多重 if-else
select salary
case
when salary > 2000 then 'A'
when salary > 1500 then 'B'
else 'C'
end
as salary_level from employees;
分组函数:
sum
, avg
, min
, max
, count
(统计该字段非 null 个数)
和关键字 distinct
搭配:
select count(distinct salary) from employees;
条件查询
where
条件查询:
常用的条件函数:
- 条件运算符:
=
,<>
(不等于),<
,<=
- 逻辑运算符:
and
,or
,not
- 模糊查询:
like
,between and
,in
,is null
select concat(last_name, first_name) as name from employees
where employee_id between 100 and 200
and salary > 2000;
like
的用法:一般搭配通配符使用。
%
表示任意个字符(包括 0 个)
_
表示任意单个字符
select last_name, salary from employees
where first_name like '_e_a%'
排序查询
- 默认为升序
asc
,可以指定为降序desc
- 支持按照函数排序(如 length)
- 有多个排序标准时,优先级高的放在前面,用逗号隔开
select *, salary*12 as salary_annual from employees
order by salary_annual desc, length(last_name) asc
分组查询
用 Group by
将表中的数据分组
// 查询每个工种的最高工资
select max(salary), job_id from employees
group by job_id;
// 查询哪些部门人数大于 3
select count(*), department_id from employees
group by department_id
having count(*) > 3
注:分组前筛选用 where
,数据源为原始表;分组后筛选用 having
,数据源为分组后的结果集
连接查询
需要查询的字段来自多个表,添加有效的连接条件
连接类型:内连接、外连接(左外、右外、全外)
连接条件用关键字 on 声明
内连接:
select last_name, department_name from employees as e
inner join departments as d
on e.department_id = d.department_id
外连接:用于查询一个表有,另一个表没有的记录。
它有主表、从表之分。外连接查询结果为主表中的所有记录,如果从表中有和它匹配的,就显示匹配值;如果没有,显示 null
左外连接:left join 左边为主表;
右外连接:right join 右边为主表;
子查询
一个 select 语句嵌套了另一个完整的 select 语句。
单行子查询(单行单列):
// 查询谁的工资比 Abel 高
select * from employees
where salary > (
select salary from employees
where last_name = 'Abel'
);
多行子查询(多行单列):
select last_name from employees
where department_id > (
select distinct department_id from departments
where location_id in (1400, 1700)
);
注意:多行多列的子查询表必须有别名。
select order_num, sum(total_price) from
(
select order_num, (item_price * quantity) as total_price
from OrderItems
) as t
group by order_num
having sum(total_price) >= 1000
联合查询
用 union 关键字,将多条查询语句的结果合并成一个
select ...
union
select...