Write a SQL query to find all duplicate emails in a table named Person.

+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
For example, your query should return the following for the above table: +---------+
| Email |
+---------+
| a@b.com |
+---------+
Note: All emails are in lowercase.

题意:查找表中重复的Email.

此题是很典型的对分组结果进行统计筛选例题,因此可以利用group by进行分组,然后使用having统计.

# Write your MySQL query statement below
SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) > 1;

此处,对wheregroup by进行比较(引用自:https://leetcode-cn.com/problems/duplicate-emails/solution/cha-zhao-zhong-fu-de-dian-zi-you-xiang-by-he-qing-/):

  • where后不能跟聚合函数,因为where执行顺序大于聚合函数。
  • where子句的作用是在对查询结果进行分组前,将不符合where条件的行去掉,即在分组之前过滤数据,条件中不能包含聚组函数,使用where条件显示特定的行。
  • having子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数,使用having条件显示特定的组,也可以使用多个分组标准进行分组。

LeetCode——Duplicate Emails(使用group by以及having解决分组统计结果)的更多相关文章

  1. [LeetCode] Duplicate Emails 重复的邮箱

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  2. LeetCode - Duplicate Emails

    Description:Write a SQL query to find all duplicate emails in a table named Person. 找出表中重复的Email. # ...

  3. sql group by hour 按小时分组统计

    Time字段以小时分组统计 select datepart(hour,time) hour,count(1) count from table where Similarity<75 group ...

  4. sql leetcode -Duplicate Emails

    第一种解法: select distinct p1.Email as Email from Person p1, Person p2 where p1.Email=p2.Email and p1.Id ...

  5. [LeetCode] Delete Duplicate Emails 删除重复邮箱

    Write a SQL query to delete all duplicate email entries in a table named Person, keeping only unique ...

  6. Leetcode 182. Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  7. LeetCode DB: Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  8. [SQL]LeetCode182. 查找重复的电子邮箱 | Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

  9. LeeCode(Database)-Duplicate Emails

    Write a SQL query to find all duplicate emails in a table named Person. +----+---------+ | Id | Emai ...

随机推荐

  1. 修改postgresql 密码

    sudo -u postgres psql -c "alter user postgres password '123456';"

  2. CodeForces 1236D(模拟)

    题意 https://vjudge.net/problem/CodeForces-1236D 最近,爱丽丝得到了一个新玩偶.它甚至可以走路! 爱丽丝为玩偶建造了一个迷宫,并想对其进行测试.迷宫具有n行 ...

  3. Ubuntu16.04下安装Cmake-3.8.2并为其配置环境变量

    下载安装包 首先我们到官网下载最新的cmake二进制安装包https://cmake.org/files/ 这里,我下载的是比较新的cmake-3.8.2-Linux-x86_64.tar.gz解压安 ...

  4. fis3 相关

    fis3 静态资源存放 windows: C:\Users\Administrator\AppData\Local\.fis3-tmp

  5. DRF--验证器

    前戏 在之前我们对前端妹子传来的数据进行校验,使用的是序列化类来进行校验的,但这里面往往满足不了我们的需求,更多的时候我们希望自己定义校验规则.这里介绍三种自定义校验的方式.分别是单一字段校验,多个字 ...

  6. Python apply函数

    Python apply函数 1.介绍 apply函数是pandas里面所有函数中自由度最高的函数.该函数如下: DataFrame.apply(func, axis=0, broadcast=Fal ...

  7. 常见算法合集[java源码+持续更新中...]

    一.引子 本文搜集从各种资源上搜集高频面试算法,慢慢填充...每个算法都亲测可运行,原理有注释.Talk is cheap,show me the code! 走你~ 二.常见算法 2.1 判断单向链 ...

  8. js -- 数组的操作(自己的常用备查)

    1.数组的定义 var arr = [],或者 var arr = [1,2,3,4,5] 2.数组的使用 >数组的合并 concat ,,]; ,]; a = a.concat(b); con ...

  9. Azure EA (2) 使用Postman访问国内Azure Billing API

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China 请读者先看一下之前的文档内容:Azure EA (1) 查看国内Az ...

  10. 详解 IaaS、PaaS和SaaS 以及他们各自的代表公司

    ——IaaS,PaaS和SaaS 是云计算领域的专业术语,也是云计算的三种服务模式.   (1)SaaS:Software as a Service,软件即服务(也称为云应用程序服务) . 云市场中企 ...