注册一亩三分地论坛,查看更多干货!
您需要 登录 才可以下载或查看附件。没有帐号?注册账号 
x
自己总结了过去上课+刷题+面试遇到的常见知识点,梳理了一个框架出来感觉舒服多了哈哈哈,希望大家帮忙看看,超级超级欢迎大家的建议和补充!
..
SQL思路+syntax总结
. 1point 3acres
思路
单一表
一. 条件输出: case when
二. 只需要返回aggregation data: group by
三. 需要保留原data以及aggregation data: window function. 1point3acres.com
四. 需要使用order筛选表:window function
多表操作
一. 连接多表: join
二. 多层aggregation: with /create table
一些概念 + explore data
一. Get to know database/table (MySQL)
.--
1. 显示所有数据库:show database.1point3acres
2. 改变数据库:use database1
3. 显示所有tables名称:show tables
4. 显示table的columns名称(显示为field)
a. show columns from table1
b. describe table1. From 1point 3acres bbs
.--
二. data type:. 1point 3 acres
1. string
a. char(n) 必须n位.--
b. varchar(n) 最多n位
2. number
a. int
b. dec(5,2)=999.99. 1point 3acres
c. real,double
3. datetime
a. date日期.google и
b. time 时间
c. timestamp日期+时间
三. NULL rules
1. NULL in where
a. NULL in comparison is unknown
b. U and T=U, U and F = F. 1point3acres.com
c. U or T=T, U or F=U
d. SQL 只返回 TRUE
2. NULL in aggregation: 一般忽略null,除了count(*)
.--
单一表. ----
一. 输出列: (先要知道使用哪个table (from))
1. 条件输出(case when… then… else …end)
2. 改变格式cast(column AS INT/DEC/VARCHAR/CHAR…..)
3. numeric
a. 绝对值:abs(). Waral dи,
b. 求余数 mod(x,y)
c. 次方power()
d. 开方sqrt()
e. 四舍五入round(column, 位数)
4. text
a. 文本合并:concat(a,b)
b. 大写upper()
c. 小写lower()
d. 返回文本长度length()
e. 提取字符mid(column, start, length)
..
5. 去重:distinct. .и
6. aggregation function
a. avg()
b. sum()
c. count() (常用count(distinct A))
d. max()
. e. min()
7. windows function
a. 格式: function() over (partition by x order by y) as a
b. function:
i. aggregation function (sum/avg/max/min). From 1point 3acres bbs
ii. row_number()
iii. rank()/dense_rank()
iv. lead()/lag()
v. first_value()
8. 日期:(syntax各database不统一 )
a. datediff()
b. 改变格式format(column,””)
c. dayname(“”) 周几?需要引号
d. day()日子数字
e. year()年份
f. month()月份
g. ><:日期前后日期要加引号
h. curdate()
二. 筛选行:where (先要知道使用哪个table (from))
key idea: 原理:对每一行判断TRUE/FALSE, 返回TRUE的rows
1. numeric variable :
a. > / < / = / <> / between (连续)-baidu 1point3acres
b. 某几个数字: in (10, 20) /not in (间断). From 1point 3acres bbs
c. is null/ is not null. ----
2. categorical variables:
a. in (a, b, c) / not in ()
b. =
c. is null/ is not null
d. like “”
i. “%”表示任何字符任何次数. 1point3acres
ii. “_”表示一个字符
3. 多个条件:AND/OR. Waral dи,
三. 其他部分. Waral dи,
1. group by
2. having.1point3acres
3. order by
4. limit
.
四. 多表
1. 横向连接JOIN
a. A INNER JOIN/LEFT JOIN/RIGHT JOIN B ON A.id=B.id ..
2. 纵向连接UNION / UNION ALL . check 1point3acres for more.
. Waral dи,
3. temporary table
a. with a as ()
b. create table a as ()
.
. From 1point 3acres bbs
. Waral dи,
|