
SQL中包含的知識(shí)點(diǎn)很多,不同職業(yè)可能使用的知識(shí)側(cè)重點(diǎn)不同,比如數(shù)據(jù)挖掘,數(shù)據(jù)開(kāi)發(fā),ETL等因?yàn)樾枰獦?gòu)建和維護(hù)數(shù)據(jù)庫(kù),對(duì)于數(shù)據(jù)庫(kù)的創(chuàng)建,插入和更新的操作比較多,但對(duì)于偏向于業(yè)務(wù)的數(shù)據(jù)分析師或者商業(yè)分析師來(lái)講,查詢是我們使用到SQL中最常用的操作。
SQL知識(shí)點(diǎn)有很多,但是二八法則告訴我們,你只用聚焦于那核心的20%的知識(shí)點(diǎn)就可以應(yīng)對(duì)業(yè)務(wù)場(chǎng)景中80%的需求。本文重點(diǎn)歸納SQL操作中常用的20%知識(shí)點(diǎn)。
核心構(gòu)成
對(duì)數(shù)據(jù)庫(kù)和表進(jìn)行操作
創(chuàng)建數(shù)據(jù)庫(kù)
create database database_name
創(chuàng)建表
create table table_name (
column_name data_type [constrains],
)
刪除表或者數(shù)據(jù)庫(kù)
drop [table|datebase] name
添加,刪除,更新表中的列或者index
alert table table_name
[add | drop | alert ] [column | index] column_name
對(duì)表中的記錄進(jìn)行操作
向表格中插入一條語(yǔ)句
insert into table_name values(x,x)
更新表中的記錄
update table_name set column_name = x where condition
刪除某一行記錄
delete from table_name where condition
查詢操作
select 子句:語(yǔ)法 select column 。選擇用于輸出的列。
from子句:語(yǔ)法 from table 。從指定的表中選擇。
select * from table
where 子句:語(yǔ)法 where condition 。用于行級(jí)過(guò)濾。
select * from table where column>2?
order by 子句:語(yǔ)法 order by [ASC|DESC] 。按照升序或者降序?qū)Σ樵兘Y(jié)果集進(jìn)行排序,默認(rèn)升序。
select * from table order by desc
having 子句:語(yǔ)法having condition。對(duì)分組的數(shù)據(jù)進(jìn)行篩選,往往配合著group by字句一起
select * from table order by desc
子句的一般順序:select from where group by having limit ,這里除了select , from 是必須的,其他都是可選的,這要看實(shí)際的業(yè)務(wù)場(chǎng)景。
注意:SQL 不區(qū)分大小寫(xiě)
基礎(chǔ)關(guān)鍵字
like: 用于where字句中的模糊匹配。
# 如查詢姓名字段中以張開(kāi)頭的名字
where name like '張%'
As: 用于給字段,表等起一個(gè)別名,方便簡(jiǎn)化
NULL: 不包含值,多與is配合使用
Limit: 用于從結(jié)果集的基礎(chǔ)上選擇N條數(shù)據(jù)
基本用法:limit 2:選舉兩條記錄輸出
select * from table limit 2
高級(jí)用法 limit 2,1:從第2條數(shù)據(jù)之后,輸出一條記錄
select * from table limit 2,1
注:有些數(shù)據(jù)庫(kù)支持Top關(guān)鍵字
Distinct : 去除重復(fù)數(shù)據(jù)
基本用法:
select distinct column from table
邏輯操作符
| 操作符 | and | or | in | not |
|---|---|---|---|---|
| 例子 | A and B | A and B | A in (,) | A not in (,) |
| 說(shuō)明 | 同時(shí)滿足AB | AB有一個(gè)滿足 | 判斷A是在結(jié)果集中 | 判斷A是不在結(jié)果集中 |
算術(shù)操作符
| = 等于 | <> 不等于 | !=不等于 |
|---|---|---|
| <=?。ǖ龋┯?/td> | >=大(等)于 | between and 兩者之間 |
| 單元格 | 單元格 | 表頭 |
聚合函數(shù)
| sum求和 | avg 求平均 | count 計(jì)算個(gè)數(shù) |
|---|---|---|
| max求最大值 | min 求最小值 | 你想讓這里放置哪個(gè)函數(shù)呢 |
事例:
select sum(column) from table
注:當(dāng)聚合函數(shù)和普通列同時(shí)出現(xiàn)在select后面時(shí),普通列必須在group by字句中指定。
extract 函數(shù):主要用于提取時(shí)間戳中的單獨(dú)部分如年,月等。如下orderData列對(duì)應(yīng)的值是2020-12-12 16:25:46.635,則使用函數(shù)后的值為2020.
select extract(year from orderData) as order_year,
from table
where condition
高階
case when
主要包括case函數(shù)和case搜索函數(shù)
# case函數(shù)
case gender
when 'm' THEN '男'
when 'f' THEN '女'
else '其他'
end
# case搜索函數(shù)
case
when gender = 'm' then '男'
when gender = 'f' then '女'
else '其他'
end
子查詢
子查詢返回的結(jié)果用于另一條 select 語(yǔ)句的 where 子句。
select customer_id
from table01
where order_num in (
select order_num
from table02
where prod_id = 'kk');
連接查詢
| 連接類型 | 說(shuō)明 | |
|---|---|---|
| 內(nèi)連接 join | A join B 返回AB的交集 | |
| 左連接 left join | A left join B 返回 A+AB的交集 | |
| 右連接 right join | A right join B 返回 B+AB的交集 | |
| 右連接 full join | A full join B 返回 AB的全集 |
注:并不是所有的數(shù)據(jù)庫(kù)都支持四種連接,但大部分都會(huì)支持內(nèi)連接和左連接。
SQL查詢所涉及的知識(shí)點(diǎn)雖然簡(jiǎn)單,但是要完全掌握還需要大量的練習(xí)。以上就是主要內(nèi)容了。關(guān)于本文如果有什么疑問(wèn),歡迎回復(fù)交流。
在線練習(xí)推薦
相關(guān)參考
知識(shí)點(diǎn)、SQL語(yǔ)句學(xué)習(xí)及詳細(xì)總結(jié)
SQL教程