The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the records in ascending order by default.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
SELECT * FROM Customers
ORDER BY Country;
Columns of type ntext, text, image, geography, geometry, and xml cannot be used in an ORDER BY clause.
The GROUP BY statement group rows that have the same values into summary rows
It is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
GROUP BY ROLLUP
Creates a group for each combination of column expressions. In addition, it "rolls up" the results into subtotals and grand totals.
For example, GROUP BY ROLLUP (col1, col2, col3, col4) creates groups for each combination of column expressions in the following lists.
NULL, NULL, NULL, NULL --This is the grand total
GROUP BY CUBE creates groups for all possible combinations of columns.
For GROUP BY CUBE (a, b) the results has groups for unique values of (a, b), (NULL, b), (a, NULL), and (NULL, NULL).
Specifies a search condition for a group or an aggregate.
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
Returns the sum of all the values, or only the DISTINCT values, in the expression.
SUM can be used with numeric columns only. Null values are ignored.
This function returns the number of items found in a group. This includes NULL values and duplicates.
COUNT is a deterministic function when used without the OVER and ORDER BY clauses.
This function returns the average of the values in a group. It ignores null values.