Sobes.tech
Senior

SQL task: select profiles with more than 10 posts.

sobes.tech AI

Answer from AI

To select profiles with more than 10 posts, you can use an SQL query with the aggregate function COUNT and the GROUP BY clause. Suppose there are tables profiles and posts, where posts contains a profile_id field to link to the profile.

Example query:

SELECT p.*
FROM profiles p
JOIN posts ps ON ps.profile_id = p.id
GROUP BY p.id
HAVING COUNT(ps.id) > 10;

Explanation:

  • JOIN links profiles with their posts.
  • GROUP BY p.id groups records by profile.
  • HAVING COUNT(ps.id) > 10 filters groups where the number of posts is greater than 10.

If you want to select only specific fields, you can specify them instead of p.*. It is important that all fields in SELECT that are not aggregated are either in GROUP BY or are functionally dependent on it (depends on the DBMS).