0
点赞
收藏
分享

微信扫一扫

SQL for Data Science - w2(University of California, Davis)

kbd 2022-04-24 阅读 54


2.1 - 2.2 Basics of Filtering with SQL

2.1用 where 子句(clause)

SELECT column1, column2, columnN 
FROM table_name
WHERE [condition]‍;

运算符

OperatorDescription
=Equal
>Greater than
<Less than
>=Greater than or equal
<=Less than or equal
<>Not equal. Note: In some versions of SQL this operator may be written as !=
BETWEENBetween a certain range
LIKESearch for a pattern

IS NULL

Null value

查找不匹配项,筛选出除了Alice mutton以外的东西


2.2 用IN OR NOT 三个operator

2.2.1 用IN()

要用()将需要筛选的值框起来生成list。list里面只能用str,都需要用到"

SELECT * FROM Customers
WHERE City IN ('Paris','London');‍

2.2.2 用 OR

如果满足第一个条件,就不会评估第二个条件(如果想要两个条件都满足需要用到 AND )

SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...; ‍

对比OR 优先用IN:1不用考虑顺序 2运行速度更快 3可以在子查询中用另一个选择语句(后面有)

2.2.3 用 AND

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...; ‍

!!sql会先运行AND 然后运行 OR ,如果想将OR单独运行要用到()

2.2.4 用 NOT 

SELECT column1, column2, ...
FROM table_name
WHERE NOT condition; ‍


2.3 通配符wildcards

SymbolDescriptionExample
%Represents zero or more charactersbl% finds bl, black, blue, and blob
_Represents a single characterh_t finds hot, hat, and hit
[]Represents any single character within the bracketsh[oa]t finds hot and hat, but not hit
^Represents any character not in the bracketsh[^oa]t finds hit, but not hot and hat
-Represents any single character within the specified rangec[a-b]t finds cat and cbt

%wildcard不能匹配null

2.3.2 LIKE 运算符

SELECT * FROM Customers
 WHERE City LIKE 'ber%';  ‍


2.4 ORDER BY 排序

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;‍
ORDER BY 2,3
--直接按照2、3列顺序排序

ASC ascending升序

DESC descending降序

2.5 数字运算 + - * /


2.6 聚合函数 aggregate functions : AVG() COUNT() MIN() MAX() SUM()

计算平均值 AVG()

了解表中有多少记录 COUNT()

最大值最小值


2.7 分组GROUP BY

NULL 会被分组成null类别,这样就不会丢失部分数据

having count ,确保只选择≥2的订单。

select, from, where, group by, having



















举报

相关推荐

0 条评论