DBMS 4
DBMS 4
Aim : Queries using Group By, Order By, and Having Clauses.
Description : SQL gives you options for retrieving, analyzing, and displaying the
information you need with the GROUP BY, HAVING, and ORDER BY clauses. Here are
some examples of how you can use them.
GROUP BY clauses:
Sometimes, rather than retrieving individual records, you want to know something about
a group of records. The GROUP BY clause is the tool you need. Suppose you’re the sales
manager of another location, and you want to look at the performance of your sales force.
SELECT expressions
FROM tables
[WHERE conditions]
ORDER BY expression [ ASC | DESC ];
HAVING clauses:
You can analyze the grouped data further by using the HAVING clause. The HAVING
clause is a filter that acts similar to a WHERE clause, but on groups of rows rather than
on individual rows. To illustrate the function of the HAVING clause, suppose the sales
manager considers Bennett to be in a class by himself.
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
ORDER BY clauses:
Use the ORDER BY clause to display the output table of a query in either ascending or
descending alphabetical order. Whereas the GROUP BY clause gathers rows into groups
and sorts the groups into alphabetical order, ORDER BY sorts individual rows. The
ORDER BY clause must be the last clause that you specify in a query.
Syntax:
COMMIT;
ROLLBACK Command
If any error occurs with any of the SQL grouped statements, all changes need to be
aborted. The process of reversing changes is called rollback. This command can only
be used to undo transactions since the last COMMIT or ROLLBACK command was
issued.