Need help with DISCINT sql keyword. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Need help with DISCINT sql keyword.

Hello. I need your help with sql. Consider having following table named scores: date name score gender 2018-4-12 Liz 98 f 2017-8-2 Max 85 m 2018-5-18 Bob 82 m 2017-8-2 Max 73 m 2018-5-18 Bob 82 m I want to delete duplicate records that have exact same date & name & score (record 3 & 5). I don't care about gender column, but I want to display it too. So I want the table to look like this: date name score gender 2018-4-12 Liz 98 f 2017-8-2 Max 85 m 2018-5-18 Bob 82 m 2017-8-2 Max 73 m How can I do it?

7th Aug 2018, 9:40 AM
Jan Štěch
Jan Štěch - avatar
7 Answers
+ 1
Hi Jan, I would use GROUP BY: select name, date, score, gender from scores group by name, date, score, gender
7th Aug 2018, 1:17 PM
P.W.R.
P.W.R. - avatar
+ 1
GROUP BY as sql-clause has the effect, that every combination of the mentioned attributs appears exactly once. You can combine it with functions like e.g. sum(), count() etc.
7th Aug 2018, 7:06 PM
P.W.R.
P.W.R. - avatar
+ 1
You could save the unique result of the select into a new table with "select name, date, gender, score into table newTable group by ...", delete the old table and rename the new one.
8th Aug 2018, 6:49 PM
P.W.R.
P.W.R. - avatar
0
Thanks for your answer, but what does GROUP BY exactly do?
7th Aug 2018, 6:25 PM
Jan Štěch
Jan Štěch - avatar
0
Great. I got it now. But this will only select and show me what I want to keep in my table. Hkw can I delete the rest?
7th Aug 2018, 8:08 PM
Jan Štěch
Jan Štěch - avatar
0
Ok. Thanks for your help.
8th Aug 2018, 7:48 PM
Jan Štěch
Jan Štěch - avatar
0
select distinct * from scores
14th Aug 2018, 7:04 AM
Amin Golmahalle
Amin Golmahalle - avatar