0

This was asked to me interview and i am beginner in sql so was unable to solve it.
we are given a table in which somehow some of the rows are duplicated but now we only want distinct rows

Name id orderNo
----------------
Mark 1 order1
Mark 1 order2
Mark 1 order2
Allen 3 order 2
Allen 3 order 3
Allen 3 order 3
Dennis 5 order 7
Dennis 5 order 9

Result

Mark 1 order1
Mark 1 order2
Allen 3 order 2
Allen 3 order 3
Dennis 5 order 7
Dennis 5 order 9

I answered

SELECT DISTINCT * from TableName;

but he wanted some other approach

MT0
  • 143,790
  • 11
  • 59
  • 117
  • 1
    As the [guidelines](https://stackoverflow.com/help/how-to-ask) suggest, you naturally researched before posting a question here... What did you find, and why didn't it answer your question? – HoneyBadger Aug 27 '22 at 11:17
  • anything else except SELECT DISTINCT * from TableName; – Aditya Patel Aug 27 '22 at 11:33
  • "he wanted some other approach". I think you should have started asking why another approach than this would be needed. The only thing worth changing is change `*` to the list of field names. – Luuk Aug 27 '22 at 12:09
  • in interview after wondering for a while i asked for hint he said you can use group by – Aditya Patel Aug 30 '22 at 10:52

3 Answers3

2

DISTINCT keyword used to select unique records. Following query will help you.

SELECT DISTINCT Name, Id, OrderNo from TableName

1

You could also use Group by clause to achieve this. Group the record by all of these column.

SELECT Name,id,orderNo
    FROM TableName
    GROUP BY Name,id,orderNo
Alpha
  • 1,413
  • 3
  • 9
  • 23
-1
SELECT DISTINCT * FROM TableName;

The above query would work.

Karthik Bhandary
  • 1,305
  • 2
  • 7
  • 16