Sobes.tech
Intern

SQL: write a query to count the number of subscribers for each blogger (table with bloggers and subscribers).

sobes.tech AI

Answer from AI

Suppose there are two tables: bloggers (with columns id, name) and subscribers (with columns id, blogger_id). To count the number of subscribers for each blogger, you can use the following SQL query:

SELECT b.id, b.name, COUNT(s.id) AS subscribers_count
FROM bloggers b
LEFT JOIN subscribers s ON b.id = s.blogger_id
GROUP BY b.id, b.name
ORDER BY subscribers_count DESC;

Here, we use LEFT JOIN to include all bloggers, even if they have no subscribers, and count the number of subscribers for each blogger.