Friday, March 30, 2012

New to SQL

I'm learning SQL and am trying to solve this problem. I have a table of Teams with a TeamID and another table of people with a TeamID col. I am trying to count the number of people on a team and display the team with the most people.

I have used this to choose the number of people on the teams:

SELECT tname, COUNT(tid)
FROM teams t, people p
WHERE t.tid=p.tid
GROUP BY tname;

this displays the team name and the # of members

How can I choose the team with the most members?

Could I do something such as (the syntax may not be correct, but is the overall idea?):

SELECT tname, count(*)
FROM tname
GROUP BY tname
HAVING count(*) >= all
(SELECT tname, COUNT(tid)
FROM teams t, people p
WHERE t.tid=p.tid)Try:
SELECT t.tname, count(*)
FROM teams t, people p
GROUP BY t.tname
HAVING count(*) >= ALL
( SELECT COUNT(*)
FROM people p
GROUP BY p.tid
)|||no that printed out the team names and 23 for each team. 23 is the number of rows in people|||That's because I forgot the join:

SELECT t.tname, count(*)
FROM teams t, people p
WHERE p.tid = t.tid
GROUP BY t.tname
HAVING count(*) >= ALL
( SELECT COUNT(*)
FROM people p
GROUP BY p.tid
)|||thanks that worked... Maybe someone could explain how to think through a query in order to solve it...I have programmed C++ which is a procedural, and SQL is not. I'm having trouble relating a question given in english to how it should be written in SQL|||Yes, it does require a different frame of mind. I'll try to explain how I got to it...

The requirement is "display the team with the most people". To find out the number of people in each team we need to look at the people table:

SELECT p.tid, count(*)
FROM people p
GROUP BY p.tid;

But from those results we only want that group having (hint) the highest count:

SELECT p.tid, count(*)
FROM people p
GROUP BY p.tid
HAVING count(*) >= ALL (<counts by p.tid>;

Now this query serves for <counts by p.tid>:

SELECT COUNT(*)
FROM people p
GROUP BY p.tid;

It will return a list of counts like:
11
7
13
2

So we now have:

SELECT p.tid, count(*)
FROM people p
GROUP BY p.tid
HAVING count(*) >= ALL
( SELECT COUNT(*)
FROM people p
GROUP BY p.tid
);

The final part is more or less cosmetic: show the team name instead of the tid. We do that by joining to the team table in the main query, and then grouping by the team name instead of the tid (since, I assumed, both are unique within the teams table). That gives us the final query:
SELECT t.tname, count(*)
FROM teams t, people p
WHERE p.tid = t.tid
GROUP BY t.tname
HAVING count(*) >= ALL
( SELECT COUNT(*)
FROM people p
GROUP BY p.tid
);

There is more than one way to do it, and it is really a matter of experience and practice to become proficient at solving such problems.|||Thank you. That was explained well.|||Suppose I wanted to see which teams had 4 or more. I tried to change all to 4, which worked, but when I tried to output the names of people, nothing was displayed. I assume it because this column is removed some where in the having clause, but how do I retain that information...once again, thanks for taking the time to write the last reply!|||Hello,

Well you should have had :

SELECT t.tname, count(*)
FROM teams t, people p
WHERE p.tid = t.tid
GROUP BY t.tname
HAVING count(*) >=4;

So you display team names and their number of players for teams having at least 4 players.

Now, I don't understand why you speak of people name. You won't have them with this query.

What do you exactly want ?

Regards,

RBARAER|||Following on from RBARAER's query, you can get all the people's names like this:

SELECT t.tname, p.pname
FROM teams t, people p
WHERE p.tid = t.tid
AND p.tid IN
( SELECT p.tid, count(*)
FROM people p
GROUP BY p.tid
HAVING count(*) >=4
)
ORDER BY t.tname, p.pname;|||I'm sorry, when I said people name, it is actually fname and lname. when i tried the code it gave me an error

Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.

why does it not let me enter p.lname and p.fname in the outer select?|||NO WAIT...it wasnt the outer select, it was the inner...it does not allow p.tid in the inner select statement. I removed it and it did not produce the correct answer. It returned a group name with 4 members and a gname with 1 member|||Then:
SELECT t.tname, p.fname, p.lname
FROM teams t, people p
WHERE p.tid = t.tid
AND p.tid IN
( SELECT p.tid
FROM people p
GROUP BY p.tid
HAVING count(*) >=4
)
ORDER BY t.tname, p.pname;
If that doesn't work, please post the exact SQL and error code/message.

(I have removed the COUNT(*) that I accidently left in before.)|||perfect!! How does it count the number of members without actually using count?|||oops...sorry, didnt notice it in the having

No comments:

Post a Comment