1. 程式人生 > >sql leetcode -Duplicate Emails

sql leetcode -Duplicate Emails

code etc 作用 region div 這樣的 技術 logs com

技術分享

第一種解法:

select distinct p1.Email as Email from Person p1, Person p2 where p1.Email=p2.Email and p1.Id>p2.id;

  第二種解法:

select Email from Person group by Email having count(Email)>1;

  sql 中有一系列 聚合函數: sum, count, max, avg, 這些函數作用域多條記錄上

select sum(population) from b_table;

而通過group by 子句, 可以讓sum和 count 這些函數對於屬於一組的數據起作用。 計算population,可以指定region

select region, SUM(population),SUM(area)
from b_table 
group by region

  having 子句可以篩選成組後的數據,where子句在聚合前篩選記錄, 也就是作用域group by ,having子句之前,而having 子句對聚合後的記錄進行篩選:

select region, sum(population),sum(area)
from b_table
group by region 
having sum(population)>100000

  不能使用where來篩選超過100000的地區,因為表匯總不存在這樣的記錄。。。

sql leetcode -Duplicate Emails