GROUP BY vs. ORDER BY ----- WHERE vs. HAVING

 

GROUP BY vs. ORDER BY Comparison Chart

The following comparison chart explains their main differences in a quick manner:

SNGROUP BYORDER BY
1.It is used to group the rows that have the same values.It sorts the result set either in ascending or descending order.
2.It may be allowed in CREATE VIEW statement.It is not allowed in CREATE VIEW statement
3.It controls the presentation of rows.It controls the presentation of columns.
4.The attribute cannot be under aggregate function under GROUP BY statement.The attribute can be under aggregate function under ORDER BY statement.
5.It is always used before the ORDER BY clause in the SELECT statement.It is always used after the GROUP BY clause in the SELECT statement.
6.It is mandatory to use aggregate functions in the GROUP BY.It's not mandatory to use aggregate functions in the ORDER BY.
7.Here, the grouping is done based on the similarity among the row's attribute values.Here, the result-set is sorted based on the column's attribute values, either ascending or descending order.




WHERE vs. HAVING Comparison Chart


Comparison BasisWHERE ClauseHAVING Clause
DefinitionIt is used to perform filtration on individual rows.It is used to perform filtration on groups.
BasicIt is implemented in row operations.It is implemented in column operations.
Data fetchingThe WHERE clause fetches the specific data from particular rows based on the specified conditionThe HAVING clause first fetches the complete data. It then separates them according to the given condition.
Aggregate FunctionsThe WHERE clause does not allow to work with aggregate functions.The HAVING clause can work with aggregate functions.
Act asThe WHERE clause acts as a pre-filter.The HAVING clause acts as a post-filter.
Used withWe can use the WHERE clause with the SELECT, UPDATE, and DELETE statements.The HAVING clause can only use with the SELECT statement.
GROUP BYThe GROUP BY clause comes after the WHERE clause.The GROUP BY clause comes before the HAVING clause.