SQL Group By Clause | What is Group By in SQL | Group By Statement in SQL | Intellipaat


Hey guys welcome back to the SQL tutorial series and in this session we’ll be working with a group by statement. The
group by statement is used to get aggregate result with respect to a group. For
example let’s say we wanted to find out the average salary with respect to
gender, that is average salary of male employee separately and average salary
of female employees separately. This is where we can use group by to divide the
data into two groups, male and female and find out the average salary of each
group separately. Now let’s have a look at the syntax. So here you’d have to be
careful of the sequence so the group by clause follows the where clause and
precedes the order by clause. So if you mix up the sequence then you may not get
the right result and if you’re not using the where Clause and the order by clause
you can directly follow up the group by statement after the table name.
So I would want the average salary with respect to the gender. So this would be
the syntax for that select and since I want average salary I will use the AVG
aggregate function and then given the name of the column which would be e_salary and since I also want to see the gender column I will give in e_gender
over here and this is from the employee table and
since I have already given the name of the table I can use the group by
statement over here and after the group by statement I need to give in the
column with respect to which I have to group the data and I want to group the
data with respect to the gender column so this would be my total command, select
average of e_salary and e _gender from employee group by e_gender so let me hit
execute right so this is the average salary with respect to the female
employees and this is the average salary with respect to the male employees. So
the average salary of female employees is around ninety nine thousand dollars
per annum and the average salary of male employees is around one hundred and
eleven thousand dollars per annum. Now I want the average age with respect to the
department and I’ll also use the order by keyword to sort the data in
descending order with respect to the average age. So this is the syntax
select since I want average age I will use the average aggregate function and then
pass in the column which would be e_age and I would also want to have a glance
at the department so e_dept i will given the name of the table which should
be employed after which I will give in the group by statement and I want to
group this with respect to the department. So group by e_dept and after
the group by statement I need to give the order by statement so order by and I
want to order the result with respect to the average age so again this would be
average age and I want the sorting to be in descending order. So desc so this is
a total command, select average e_agent e_ department. So I will be getting the
average age and the Department column from the employee table and I’m grouping
with respect to the department column and I’m sorting it in descending order
of the age. So this would be e_age over here so let me hit execute so we have
the average age with respect to each department ordered in descending order.
So operations department has the maximum average age of 36 then sales has an
average age of 33, Analytics department has an average age of 27 and support has
the youngest staff of age 21 and this is how we can use a group by clause. So
thanks for attending the session. Let’s meet in the next class

One Reply to “SQL Group By Clause | What is Group By in SQL | Group By Statement in SQL | Intellipaat”

  1. Got a question on SQL group by clause? Do write it in the comment and you will get a response immediately by our expert. For MS SQL Server training & certification, call us at US: 1800-216-8930 (Toll Free) or India: +917022374614. You can also write us at [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *