Finding rows with same value

By Pradyumna Chippigiri

January 22, 2026


Found this wonderful interview question asked to someone on X, thought it was easy at first, but it was nice to learn about the solution. Question goes like this :


You have a table named Students with columns StudentID, FirstName, LastName, and Score. Write an SQL query to find the names of students who have the same scores.


The solution goes like this: First find which scores are duplicated and that can be done like this

SELECT Score
FROM Students
GROUP BY Score
HAVING COUNT(*) > 1;

This returns only the score values that appear more than once. (not the entire student rows, because we are selecting only scores) Now lets return the name of the students

SELECT s.FirstName, s.LastName, s.Score
FROM Students s
WHERE s.Score IN (
  SELECT Score
  FROM Students
  GROUP BY Score
  HAVING COUNT(*) > 1
)
ORDER BY s.Score, s.LastName, s.FirstName;