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. linux环境下的Oracle部署

    一.  环境及相关软件 虚拟机:VMwore Workstation Linux系统:CentOS ORACLE:ORACLE_112030_Linux-x86-64 Xmanger软件 二.  安装 ...

  2. [PHP] 新版本PHP7.4与新版本MySQL8认证问题

    mysql8的默认密码加密方式是caching_sha2_password,PHP7.4连接mysql的加密方式也为caching_sha2_password,这个地方要注意. 当为了兼容旧版的客户端 ...

  3. 新版本的node,全局配置

    配置环境变量: 1.在你安装node环境目录下 即是node_modules同级目录中. 创建两个文件夹[node_global]及[node_cache] node_cache:缓存目录 node_ ...

  4. day8_类的装饰器和反射

    """ 类的装饰器: @property 当类的函数属性声明 @property后, 函数属性不需要加括号 即可调用 @staticmethod 当类的函数属性声明 @s ...

  5. Mybatis的Java API(八)

    使用mybatis的主要Java接口就是SqlSession.可以通过这个接口来执行命令,获取映射器和事务管理. SqlSession是由SqlSessionFactory实例创建,SqlSessio ...

  6. LG5104 红包发红包 概率与期望

    问题描述 LG5104 题解 观察发现,对于 \(w\) ,期望得钱是 \(\frac{w}{2}\) . 然后答案就是 \(\frac{w}{2^k}\) . 然后快速幂求个逆元就好了. \(\ma ...

  7. JAVA多线程间隔时间段执行方法

    import java.util.Date; import java.util.Timer; import java.util.TimerTask; public class ManyProject ...

  8. pip 源切换至国内镜像

    pip 源切换至国内镜像 使用 pip 安装软件时,使用国内镜像可以大大提高下载速度 常用国内镜像 https://pypi.tuna.tsinghua.edu.cn/simple/ # 清华大学 h ...

  9. nmap中文手册

    译注该Nmap参考指南中文版由Fei Yang <fyang1024@gmail.com>和Lei Li<lilei_721@6611.org> 从英文版本翻译而来. 我们希望 ...

  10. JS分类选择插件

    需要做一个选择分类工具,大致要求如下: 点击按钮,显示一级分类,指向某个一级分类显示对应二级分类,分类有几层不定. 只能选择最后一个分类,然后把分类的ID 传值给按钮的value 我的思路: 1.后台 ...