Results 1 to 4 of 4
  1. #1
    Join Date
    May 2007
    Posts
    18

    How can I use AVG() for small groups?

    How do you use AVG in sql to manage grouped output? For example:
    Given a SQL Table “Employee”



    Name Salary City

    John Doe 15000 Seattle
    Jane Doe 30000 Redmond
    Tim Wayne 25000 Seattle


    Write a SQL query that would return the average salary per city in the format shown below:



    City Salary

    Seattle 20000
    Redmond 30000



    Select City, AVG(Salary) from Employee where ... I don't know the rest

    Where city == city or something like that?

  2. #2
    Join Date
    Aug 2012
    Posts
    11
    I don't think you need a where clause. I would use the group by clause.

    group by city.

  3. #3
    Join Date
    May 2007
    Posts
    18
    do you mean something like this

    select city
    ,avg(salary) as avgsalary
    from employee
    group by city

  4. #4
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    Quote Originally Posted by xarzu View Post
    do you mean something like this
    what happened when you tested that?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •