views:

57

answers:

4

Hi

        I have a table with data

                   ID     Name

                    1      John
                    2      Robert
                    3      John
                    4      Sam
                    5      Jack
                    6      Sam

      Now i want ony the the duplicate names ony through query

         ie..,

                  Name

                  John
                  Sam

Thanks in advance

+11  A: 
SELECT Name
FROM YourTable
GROUP BY Name
HAVING COUNT(*) > 1
AdaTheDev
+1 Yeah ... this is better than mine. I wonder why I can't think simple :)
Rashmi Pandit
+3  A: 
CREATE TABLE MyTable (
    ID int
    , Name nvarchar(50)
)

INSERT MyTable VALUES ( 1, 'John' )
INSERT MyTable VALUES ( 2, 'Robert' )
INSERT MyTable VALUES ( 3, 'John' )
INSERT MyTable VALUES ( 4, 'Sam' )
INSERT MyTable VALUES ( 5, 'Jack' )
INSERT MyTable VALUES ( 6, 'Sam' )

SELECT
    Name
FROM
    MyTable
GROUP BY
    Name
HAVING
    COUNT(*) > 1

DROP TABLE MyTable

Results:

Name
--------------------------------------------------
John
Sam
AakashM
A: 
with temp as  (
    select Name, count(Name) as countOfNames
    from myTable
    group by Name
    )
select Name from temp 
where countOfNames > 1
Rashmi Pandit
A: 

Thinking in SQL by Joe Celko

MadH