DBMS SQL Clauses - Javatpoint
DBMS SQL Clauses - Javatpoint
⇧ SCROLL TO TOP
SQL Clauses
The following are the various SQL clauses:
1. 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.
Syntax
ADVERTISEMENT
ADVERTISEMENT
SELECT column
FROM table_name
WHERE conditions
GROUP BY column
ORDER BY column
Sample table:
PRODUCT_MAST
Item1 Com1 2 10 20
Item2 Com2 3 25 75
Item3 Com1 2 30 60
Item4 Com3 5 10 50
Item5 Com2 2 20 40
Item6 Cpm1 3 25 75
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Output:
Com1 5
Com2 3
Com3 2
2. HAVING
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:
Example:
Output:
Com1 5
Com2 3
3. ORDER BY
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... ASC|DESC;
Where
Table:
CUSTOMER
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
SELECT *
FROM CUSTOMER
ORDER BY NAME;
Output:
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
SELECT *
FROM CUSTOMER
ORDER BY NAME DESC;
Output:
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
← Prev Next →
Feedback
Preparation
Trending Technologies
Machine DevOps
Learning Tutorial
Tutorial
DevOps
Machine
Learning
B.Tech / MCA