Reputation:
I am learning SQL and can not figure out what I am doing wrong in my code.
Select robot, city, country, count(sponsor.sponsor)
From robot join sponsor on robot.robotid = sponsor.robotid
Group by robotid
Where country not in ('United States');
I am supposed to find all robots not from the US and how many sponsors they have. I have figured out how to get only the international robots from the robot table and the total number of sponsors each robotID has in the sponsor table but can not figure out how to join them. If it helps the pk for both tables is robotID but that value should not be returned in the query.
Upvotes: 0
Views: 71
Reputation: 151
Check this out.
Select robot, city, country, count(sponsor.sponsor)
From robot join sponsor on robot.robotid = sponsor.robotid
Where country not in ('United States')
Group by robotid;
Upvotes: -1
Reputation: 1270993
There are multiple issues with the query. Three that are obvious:
select
columns are not compatible with the group by
columns.where
clause is misplaced.robotid
is ambiguousOne could guess that you intend:
Select r.robot, city, country, count(*)
From robot r join
sponsor s
on r.robotid = s.robotid
where country not in ('United States')
group by r.robot, city, country;
This is just a guess. It should fix the syntax problems, but it may not be what you want. Why not? You have not explained your problem, provided sample data, or desired results.
Upvotes: 1