Skip to content
Advertisement

Select everything from SQL database except duplicate

I have a database that looks like this:

It measures how many times participant1 and participant2 have shaked hands.
I want to select the amount of times total people have shaken hands (without the duplicates counted).
So in this example the output would be like this:

Can anyone help me with the SQL Statement to do so?

Advertisement

Answer

With NOT EXISTS:

This query uses the functions least() and greatest() which are supported by MySql and Postgresql.

See the demo.
Results:

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement