Kodewick
Start learning →
Basic←→0 / 70

Pets per owner

#043BasicJOINGROUP BY5 min

The vet wants to know how many pets each owner has. Join owners with pets, group by the owner's name and count. Two columns: the owner's name, and how many pets. Owners with no pets can be left out.

The tables
owners
idnamecityphone
1LenaOslo555-0101
2MarcoRomeNULL
3PriyaOslo555-0103
4OmarCairoNULL
5SofiaRome555-0105
5 rows
pets
idnamekindageweightowner_id
1Biscuitdog3121
2Mochicat541
3Pepperdog8202
4Nemofish1NULL3
5Lunacat233
6Rexdog1130NULL
7Kiwibird4NULL5
8Tofucat75NULL
8 rows
query.sql
-- your code here: join, group by owners.name, COUNT(*)
SELECT owners.name, pets.name
FROM owners
JOIN pets ON pets.owner_id = owners.id;
Ctrl + Enter runs too
Result

Run shows what your query gives. When it looks right, Submit.