SQL Clauses
GROUP BY
- SQL GROUP BY statement is used to arrange identical data into groups.
- The GROUP BY statement is used with the SQL SELECT statement.
- The GROUP BY statement follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause.
- The GROUP BY statement is used with aggregation function.
Syntax
SELECT column
FROM table_name
WHERE conditions
GROUP BY column
ORDER BY column
Example
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING
- HAVING clause is used to specify a search condition for a group or an aggregate.
- Having is used in a GROUP BY clause. If you are not using GROUP BY clause then you can use HAVING function like a WHERE clause
Syntax
SELECT column1, column2 FRO
M table_name
WHERE conditions
GROUP BY column1, column2
AVING conditions
ORDER BY column1, column2;
Example
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GOUP BY COMPANY
HAVING COUNT(*)>2;
ORDER BY
- The ORDER BY clause sorts the result-set in ascending or descending order.
- It sorts the records in ascending order by default. DESC keyword is used to sort the records in descending order.
Syntax
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1, column2... AS
C|DESC;
Example
SELECT *
FROM CUSTOMER
ORDER BY NAME;
OR
SELECT *
FROM CUSTOMER
ER BY NAME DESC;

Comments
Post a Comment