0

How do I query that I get a distinct clubName based on my query? Below is the website to demo the sql query.

http://www.sqlfiddle.com/#!2/54be8b/6

Here is the query used in the SQL fiddle:

SELECT DISTINCT c.ClubName, c.*, p.* 
from Club c inner join Persons p on p.clubName = c.clubName;

Output should be

ManUtd
Barcelona

with 4 rows

3
  • Can you share the output you're trying to get? Commented Oct 17, 2014 at 6:20
  • I've added your query from SQL fiddle above. The query gets you Club Name and ID plus the associated persons. Your Output is Club names only. So what do you actaully want? Club names only as you say? Then why join with the persons table at all? (And btw: what do you have a club id for, if you don't use it, but use the club name in the persons table instead?) Commented Oct 17, 2014 at 8:26
  • what do u mean "with 4rows"? Please post Exact output that u need.. Commented Oct 17, 2014 at 8:44

1 Answer 1

1

If you are only interested in getting distinct clubName's,

SELECT DISTINCT c.ClubName 
FROm Club c inner join Persons p on p.clubName = c.clubName;

This gives you

CLUBNAME
Man Utd
Barcelona

If, however, you include all other columns in the SELECT DISTINCT statement as you did in the OP,

SELECT DISTINCT c.ClubName, c.*, p.* 
from Club c inner join Persons p on p.clubName = c.clubName;

then, there may be multiple DISTINCT rows corresponding to the same clubName, and SQL is correct to give you:

CLUBNAME    ID  LASTNAME    FIRSTNAME
Man Utd     1   Maria   Di
Man Utd     1   Rooney  Wayne
... 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.