DataBase -- Count & Group by

Posted 江湖小妞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DataBase -- Count & Group by相关的知识,希望对你有一定的参考价值。

SQL Count()函数:

  • SQL COUNT(column_name)语法:COUNT(column_name)函数返回指定列的值得数目(NULL不计入)
select count(column_name) from table_name
  • SQL COUNT(DISTINCT column_name)语法:COUNT(DISTINCT column_name)函数返回指定列的不同值得数目
select count(distinct column_name) from table_name
  • SQL COUNT(*)语法:COUNT(*)函数返回表中的记录数
select count(*) from table_name

 

 

SQL GROUP BY 语句:

合计函数如SUM函数,常常需要添加GROUP BY语句。

语法:

SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name

 

 

实例:

Question:

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

+----+---------+
| Id | Email   |
+----+---------+
| 1  | [email protected] |
| 2  | [email protected] |
| 3  | [email protected] |
+----+---------+

For example, your query should return the following for the above table:

+---------+
| Email   |
+---------+
| [email protected] |
+---------+

Note: All emails are in lowercase.

 

Analysis:

写一个SQL语句,找出Person表中所有重复的email

 

Answer:

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

 

以上是关于DataBase -- Count & Group by的主要内容,如果未能解决你的问题,请参考以下文章

Oracle 11g 快速搭建脚本

net.sqlcipher.database.SQLiteException: 文件不是数据库: , 编译时: select count(*) from sqlite_master

LeetCode:Database 84.每次访问的交易次数

(g_jvm)->AttachCurrentThread(&env, NULL) 后使用 (g_jvm)->DetachCurrentThread();程序报错

SELECT COUNT(*);

oracle database 9i/10g/11g 编程艺术 源代码下载