1

I have data of students' result. Each student has appeared in 5 subjects. Few students have more than 5 records in database. Student having 2 records in "Science" and 1 for each remaining subjects. total 6 records. example is given here.

sample data:

RollNo          Subject   TOTAL_MARKS
-------------------------------------
11-119-302        ENGL      34
11-119-302        MATH      56
11-119-302        SCIE      44
11-119-302        SCIE      24
11-119-302        URDU      76

I want to get all the fields of those students having duplicate subjects.

Required output:

RollNo          Subject   TOTAL_MARKS
-------------------------------------    
11-119-302        ENGL      34
11-119-302        ENGL      56
11-119-333        SCIE      44
11-119-333        SCIE      24
Jithin Shaji
  • 5,893
  • 5
  • 25
  • 47
Shah Nawaz
  • 31
  • 5
  • possible duplicate of [Finding duplicate values in a SQL table](http://stackoverflow.com/questions/2594829/finding-duplicate-values-in-a-sql-table) – Bud Damyanov Aug 13 '14 at 08:54

2 Answers2

1

try this :

select * from results where
         num in ( select num from results group by num having count(num)>2)
Youness
  • 1,468
  • 1
  • 9
  • 19
1

See this

DECLARE @MARKS TABLE (ROLLNO VARCHAR(20),SUBJECT VARCHAR(20), TOTAL_MARKS INT)
INSERT INTO @MARKS VALUES 
('11-119-302','ENGL',34),
('11-119-302','MATH',56),
('11-119-302','SCIE',44),
('11-119-302','SCIE',24),
('11-119-302','URDU',76)

SELECT  *
FROM    @MARKS M JOIN (
SELECT  ROLLNO,SUBJECT,COUNT(*) C FROM @MARKS GROUP BY ROLLNO,SUBJECT HAVING COUNT(*) > 1) LU
ON      M.ROLLNO = LU.ROLLNO AND M.SUBJECT = LU.SUBJECT

--Result

enter image description here

Jithin Shaji
  • 5,893
  • 5
  • 25
  • 47