inner join on 加条件和where加条件_SQL学习笔记 - GROUP BY / JOIN / UNION
最近在DataCamp上學習SQL(基于PostgreSQL)的課程,本文主要記錄自己易記混的點,以便日后參考學習,不做原理講解。
2. WHERE/ GROUP BY/ HAVING/ ORDER BY 執行順序
SELECT首先WHERE將最原始記錄中不滿足條件的記錄刪除(所以應該在where語句中盡量將不符合條件的記錄篩選掉,這樣可以減少分組的次數),WHERE語句不能用聚合函數;
然后通過GROUP BY關鍵字對數據進行分組 ;
接著根據HAVING關鍵字后面指定的篩選條件,將分組后不滿足條件的記錄篩選掉,(HAVING可以用聚合函數,如 HAVING AVG(col) > 10;
最后按照ORDER BY語句進行排序。
WHER子句在聚合前先篩選記錄,也就是說作用在GROUP BY和 HAVING子句前;而HAVING子句在聚合后對組記錄進行篩選。
3. JOIN
INNER JOIN / JOIN : only includes records in which the key is is both tables.
LEFT JOIN:keeps all of the records in the left table while bringing in missing values for those key field values that don't appear in the right table.
RIGHT JOIN:keeps all of the records in the right table while bringing in missing values for those key field values that don't appear in the left table.
FULL JOIN:combines a LEFT JOIN and a RIGHT JOIN, it will bring in all records from both the left and the right table and keep all of the missing values accordingly.
當用于聯結兩個表的字段相同時,USING等價于JOIN操作中的ON,如以下2個實例等價:
SELECT a.name, b.age FROM test AS a JOIN test2 AS b ON a.id = b.id;等價于SELECT a.name, b.age FROM test AS a JOIN test2 AS b USING(id);注:細微區別在與,USING(id) 在結果集中只會有一個id列。
4. UNION
UNION:includes every record in both tables but DOES NOT double count those that are in both tables.(包含兩個表中的每個記錄,但重復的行,最終只會出現一次)
UNION ALL:includes every record in both tables and DOES replicate those are in bot tables.(包括兩個表中的每個記錄,并且保留重復行)
INTERSECT:results in only those records found in both of the tow tables.(交集,兩個集中共同的部分)
EXCEPT:results in only those records in one table BUT NOT the other.(差異,兩個集中不重復的部分)
你的點贊是我持續更新的動力~ 謝謝 Thanks?(・ω・)ノ
其他SQL學習筆記 友情鏈接:
JessieY:SQL學習筆記 - 窗口函數OVER?zhuanlan.zhihu.comJessieY:SQL學習筆記 - CTE通用表表達式和WITH用法?zhuanlan.zhihu.comJessieY:SQL學習筆記 - CASE WHEN THEN?zhuanlan.zhihu.com總結
以上是生活随笔為你收集整理的inner join on 加条件和where加条件_SQL学习笔记 - GROUP BY / JOIN / UNION的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么一刻值千金 春宵一刻值千金
- 下一篇: date转timestamp格式_技术分