SQL Query To Obtain Value that Occurs more than once
The answers mentioned here is quite elegant https://stackoverflow.com/a/6095776/1869562 but upon testing, I realize it only returns the last name. What if you want to return the entire record itself ? Do this (For Mysql)
SELECT *
FROM `beneficiary`
WHERE `lastname`
IN (
SELECT `lastname`
FROM `beneficiary`
GROUP BY `lastname`
HAVING COUNT( `lastname` ) >1
)
From Oracle (but works in most SQL DBs):
SELECT LASTNAME, COUNT(*)
FROM STUDENTS
GROUP BY LASTNAME
HAVING COUNT(*) >= 3
P.S. it's faster one, because you have no Select withing Select methods here
For SQL Server 2005+
;WITH T AS
(
SELECT *,
COUNT(*) OVER (PARTITION BY Lastname) as Cnt
FROM Students
)
SELECT * /*TODO: Add column list. Don't use "*" */
FROM T
WHERE Cnt >= 3
For MySQL:
SELECT lastname AS ln
FROM
(SELECT lastname, count(*) as Counter
FROM `students`
GROUP BY `lastname`) AS tbl WHERE Counter > 2