0

in my table having data like this

+-----+----------+  
| sno | name     |  
+-----+----------+  
| 101 | Raju     |  
| 102 | Raju     |  
| 103 | Santhosh |  
| 104 | Santhosh |  
| 105 | madhavi  |  
| 106 | suheel   |  
+-----+----------+  

in that i want find dupliacte records and display sno(number) only
for example output should be like this

+-----+  
| sno |  
+-----+  
| 101 |  
| 102 |  
| 103 |  
| 104 |  
+-----+  
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
venkat
  • 465
  • 3
  • 9
  • 2
    Have you tried a query yet? – Tim Biegeleisen Nov 26 '18 at 07:20
  • Duplicate of https://stackoverflow.com/q/2594829/3404097 – philipxy Nov 27 '18 at 06:04
  • This is a faq. Please always google error messages & many clear, concise & specific versions/phrasings of your question/problem/goal with & without your particular strings/names & 'site:stackoverflow.com' & tags & read many answers. Add relevant keywords you discover to your searches. If you don't find an answer then post, using 1 variant search as title & keywords for tags. See the downvote arrow mouseover text. When you do have a non-duplicate code question to post please read & act on [mcve]. – philipxy Nov 27 '18 at 06:05

3 Answers3

1

In a Derived table, get all the name values which have duplicates. To do that, we can GROUP BY on name and use HAVING to consider only those names, where COUNT(*) (total number of rows for that name) is more than 1.

Now, we can join back to the main table to get their respective sno values

SELECT
 t.sno
FROM your_table t
JOIN (SELECT name 
      FROM your_table 
      GROUP BY name
      HAVING COUNT(*) > 1) dt
  ON dt.name = t.name 
Madhur Bhaiya
  • 28,155
  • 10
  • 49
  • 57
0

Here is a MySQL 8+ way of doing this:

SELECT sno
FROM
(
    SELECT t.*, COUNT(*) OVER (PARTITION BY name) cnt
    FROM yourTable t
) t
WHERE cnt > 1;
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

You can try using correlated subquery

select sno from tablename a 
where name in (select 1 from tablename b where a.name=b.name having count(name)>1 )
Fahmi
  • 37,315
  • 5
  • 22
  • 31