What is difference b/w "group by" and "order by", SQL query?
What is difference b/w "group by" and "order by", SQL query?
Regards,
Brijesh Jain
---------------------------------------------------------
Connect with me on Skype: jainbrijesh
Google Plus : jainbrijeshji
GROUP BY colllects values in multiple records by the group by column.
ORDER BY simply sorts in ascending or descending order.
OREDER BY sort your result set into asc or desc order, asc in the default.
whereas The GROUP BY keyword is used when we are selecting multiple columns from a table (or tables) and at least one arithmetic operator appears in the SELECT statement. When that happens, we need to GROUP BY all the other selected columns, i.e., all columns except the one(s) operated on by the arithmetic operator.
thanks buddy.. thanks
'group by' is used to get the data from table by grouping one or more columns when it come to the order by used to sorting the column data either ascending or decending order....
fetched records can be sorted out by order by clause.
record set can be summarized by group by clause.
hi
here group by clause we use group by clause mainly when we want to use some agregate functions like sum which return agregate of all columns value every time they r called & with out the group by clause it was not possible to find the sum for each individual gruop of columns values .basically group by clause is use to work on group .
and order by clause is use to sort particular column alfabatically or numerically wht ever value columns has in decending or accending order.
hi
here group by clause we use group by clause mainly when we want to use some agregate functions like sum which return agregate of all columns value every time they r called & with out the group by clause it was not possible to find the sum for each individual gruop of columns values .basically group by clause is use to work on group .
and order by clause is use to sort particular column alfabatically or numerically wht ever value columns has in decending or accending order.
Group by is a way to sub-total your results, or perform some other "aggregate" function on them. Example: select department, sum(salary) from tblemployee group by department; will give you salary totals by department, whereas the sum statement by itself would just give you the grand total of all salaries in tblemployee. Order by is a way to sort your results - it does not affect what shows up in your resultset, only what order it is displayed. Example: select * from tblemployee order by lastname; will give you all tblemployee data, in order of last name. If you want the results in descending (reversed) order, simply add desc to the end of the clause: order by lastname desc; by default rsults are displayed in ascending order.
group by groups the records as per a single or list of fields.
this is mainly used for displaying aggregate data.
Order by is used for sorting the records.
it can sort either in ascending or descenging order. where ascending is the default sort.
order by only Sorts the resultset in a particular order.
I thas no effect on the actual data while used in a select statment.
wen we are using any aggregate function in query like MAX,AVG,MIN,COUNT.......... suppose if we are using a avg function then it will calculate avg of some group of vales (more than 1 value)for example we want calculate avg salary of an employee means so many employes are there then u need each employe's avg salary then u have to use group by function on employee name or ID if u are not provide group by then also it wil give avg of total salary amnt that present in colmn ,if u are using group by then it calculate avg salary according to what u have mention at group by clause
AND in order by which colmn name u have mention that wil display in ascending r descending order according to ur choice