Skip to content
Advertisement

SQL JOIN with WHERE condition when two rows’ values are the same and one row matches to two different rows

I have these tables:

And I have to return all student names that do either Tennis or Football. However, there was a test case which I could not pass and it was stated like that:

Students with the same name.

I do not know the exact implementation of the test cases, but I suspect it was the situation where student A named Carl does Tennis and student B also named Carl does Football and Carl is showed two times. How could I query that database to get the result like that? I’ve created the demo base to try:

SQL Fiddle And let’s suppose that the passing set would be:

I’ve attempted with these two queries, but none gives the correct answer.

Advertisement

Answer

Join the tables, filter only the rows with the activities that you want and return distinct rows:

See the demo.

Results:

If you want only the names of the students without the ids:

See the demo.

Results:

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