title: SQL query to find the average number of employees per company in a database
body: <p>The goal of this query is to find the average number of employees per company in my database. Here is what I currently have for my query, along with my result:</p>
  
  <pre><code>create view Totals as
  ((select distinct count(company_name) as TotalCompanies, company_name
    from company
    group by company_name)
   union
  (select distinct count(Lastname) as TotalEmps, company_name
     from Works
     group by company_name));
  
  
  select avg(TotalCompanies) from Totals;
  
  --RESULT:
  AVG(TOTALCOMPANIES)
  2.777777777778
  </code></pre>
  
  <p>While I did get a result (so I do not believe there are any syntax errors), based on the actual data I've inputted into my tables, I do not believe this value is correct.</p>
  
  <p>Is what I'm doing in my view creation even getting me to an appropriate point where I can just call the average function of the TotalCompanies field of that view? My intent was to count all the employees per company name in the view, and then average those values...</p>
  
  <p>For an FYI, I am using SQL for Oracle 11g R2 and here is my initial schema I'm creating queries for:</p>
  
  <pre><code>create table Employee(
      Lastname    varchar(10),
      FirstName   varchar(10),
      MidInitial  char(1),
      gender      char(1),
      street      varchar(10),
      city        varchar(10),
      primary key(Lastname, FirstName, MidInitial));
  
  create table company(
      company_name    varchar(20),
      city    varchar(10),
      primary key(company_name));
  
  create table Works(
      Lastname    varchar(10),
      FirstName   varchar(10),
      MidInitial  char(1),
      company_name    varchar(20),
      salary      numeric(8,2),
      primary key(Lastname, FirstName, MidInitial, company_name),
      foreign key(Lastname, FirstName, MidInitial) references Employee,
      foreign key(company_name) references company);
  </code></pre>
  
  <p>Thanks for the help!</p>