Search code examples
sqlcountaverage

How to find AVG of Count in SQL


This is what I have

select avg(visit_count) from ( SELECT count(user_id) as visit_count from table )group by user_id;

But I get the below error

ERROR 1248 (42000): Every derived table must have its own alias

if I add alias then I get avg for only one user_id

What I want is the avg of visit_count for all user ids

SEE the picture for reference

Example 3,2.5,1.5


Solution

  • It means that your subquery needs to have an alias.

    Like this:

    select avg(visit_count) from (
      select count(user_id) as visit_count from table
       group by user_id) a