Group by and Having ,Order by
Group By - In SQL Server we have got lot of aggregate functions. Examples 1. Count() 2. Sum() 3. avg() 4. Min() 5. Max() Group by clause is used to group a selected set of rows into a set of summary rows by the values of one or more columns or expressions. It is always used in conjunction with one or more aggregate functions. I want an sql query, which gives total salaries paid by City. The output should be as shown below. Query for retrieving total salaries by city : We are applying SUM() aggregate function on Salary column, and grouping by city column. This effectively adds, all salaries of employees with in the same city. Select City, SUM (Salary) as TotalSalary from tblEmployee Group by City Note: If you omit, the group by clause and try to execute the query, you get an error - Column 'tblEmployee.City' is invalid in the select list because it is not ...
Comments