Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SELECT id WHERE COUNT(*) > X ? - How to get records from any user that has more than X records in table?

Obviously the query in the title does not work, but it might illustrate in a naive way, what I would like to do. I have a table that contains some users identified by an id column. This id is NOT unique within the database. It marks a user that may have multiple records in my table.

How can I show the whole record of all users (identified by id) that have more than 10 records in my table?

like image 565
Matt Bannert Avatar asked Feb 01 '11 15:02

Matt Bannert


People also ask

What does SELECT count (*) from table do?

SQL SELECT COUNT(*) function The COUNT(*) function represents the count of all rows present in the table (including the NULL and NON-NULL values).

What does count () over () do in SQL?

count(*) over() will count how many rows in your result set, in your case, because you did GROUP BY on [ID] column, which I assume it is a column with primary key (unique values and no null values), then in your case, count(*) returns same value as count(*) over () does.

What are the ways to get a count of the number of records in a table?

With the help of the SQL count statement, you can get the number of records stored in a table.


1 Answers

Use having instead of where:

SELECT id
  FROM (
        SELECT id, COUNT(*) as cnt
        FROM somewhere 
        GROUP BY id
        HAVING cnt > 1
   ) temp_table
like image 163
dev-null-dweller Avatar answered Sep 19 '22 22:09

dev-null-dweller