SQL学习笔记 - GROUP BY / JOIN / UNIO
最近在DataCamp上学习SQL(基于PostgreSQL)的课程,本文主要记录自己易记混的点,以便日后参考学习,不做原理讲解。
- GROUP BY(分组)一般和聚合函数一起使用,包括COUNT(),AVG(),MAX(),MIN(),SUM();一般跟在FROM后面;SELECT语句中未出现在聚合函数里的列都要出现在GROUP BY。
2. WHERE/ GROUP BY/ HAVING/ ORDER BY 执行顺序
SELECT __ FROM __ WHERE __ GROUP BY __ (HAVING __) (ORDER BY _ ASC/DESC)
首先,WHERE将最原始记录中不满足条件的记录删除(所以应该在where语句中尽量将不符合条件的记录筛选掉,这样可以减少分组的次数),WHERE语句不能用聚合函数;
然后,通过GROUP BY关键字对数据进行分组 ;
接着,根据HAVING关键字后面指定的筛选条件,将分组后不满足条件的记录筛选掉,(HAVING可以用聚合函数,如 HAVING AVG(col) > 10;
最后,按照ORDER BY语句进行排序。
WHER子句在聚合前先筛选记录,也就是说作用在GROUP BY和 HAVING子句前;而HAVING子句在聚合后对组记录进行筛选。
3. JOIN
imageINNER 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
imageUNION: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.(差异,两个集中不重复的部分)
本文首发于知乎「JessieY」