Home DBMS SQL PL/SQL SQLite MongoDB Cassandra MySQL Oracle CouchDB Neo4j DB2 C Java Projects Interview Q
⇧ 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.
The GROUP BY statement is used with aggregation function.
Syntax
ADVERTISEMENT
ADVERTISEMENT
SELECT column
FROM table_name
WHERE conditions
GROUP BY column
ORDER BY column
Sample table:
PRODUCT_MAST
PRODUCT COMPANY QTY RATE COST
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
Item7 Com1 5 30 150
Item8 Com1 3 10 30
Item9 Com2 2 25 50
Item10 Com3 4 30 120
Example:
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY;
Output:
Com1 5
Com2 3
Com3 2
2. 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
FROM table_name
WHERE conditions
GROUP BY column1, column2
HAVING conditions
ORDER BY column1, column2;
Example:
SELECT COMPANY, COUNT(*)
FROM PRODUCT_MAST
GROUP BY COMPANY
HAVING COUNT(*)>2;
Output:
Com1 5
Com2 3
3. 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... ASC|DESC;
Where
ASC: It is used to sort the result set in ascending order by expression.
DESC: It sorts the result set in descending order by expression.
Example: Sorting Results in Ascending Order
Table:
CUSTOMER
CUSTOMER_ID NAME ADDRESS
12 Kathrin US
23 David Bangkok
34 Alina Dubai
45 John UK
56 Harry US
Enter the following SQL statement:
SELECT *
FROM CUSTOMER
ORDER BY NAME;
Output:
CUSTOMER_ID NAME ADDRESS
34 Alina Dubai
23 David Bangkok
56 Harry US
45 John UK
12 Kathrin US
Example: Sorting Results in Descending Order
Using the above CUSTOMER table
SELECT *
FROM CUSTOMER
ORDER BY NAME DESC;
Output:
CUSTOMER_ID NAME ADDRESS
12 Kathrin US
45 John UK
56 Harry US
23 David Bangkok
34 Alina Dubai
← Prev Next →
Youtube For Videos Join Our Youtube Channel: Join Now
Feedback
Help Others, Please Share
Learn Latest Tutorials
Splunk SPSS tutorial Swagger T-SQL Tumblr
tutorial SPSS
tutorial tutorial tutorial
Splunk Swagger Transact-SQL Tumblr
React tutorial Regex R RxJS tutorial
tutorial Reinforcement Programming
ReactJS RxJS
learning tutorial
Regex
tutorial
R Programming
Reinforcement
Learning
React Native Python Python Python Keras
tutorial Design Patterns Pillow tutorial Turtle tutorial tutorial
React Native Python Design Python Pillow Python Turtle Keras
Patterns
Preparation
Aptitude Logical Verbal Interview Company
Aptitude
Reasoning Ability Questions Interview
Reasoning Verbal Ability Interview
Questions
Questions Company
Questions
Trending Technologies
Artificial AWS Tutorial Selenium Cloud Hadoop
Intelligence AWS
tutorial Computing tutorial
Artificial Selenium Cloud Computing Hadoop
Intelligence
ReactJS Data Science Angular 7 Blockchain Git Tutorial
Tutorial Tutorial Tutorial Tutorial Git
ReactJS Data Science Angular 7 Blockchain
Machine DevOps
Learning Tutorial
Tutorial
DevOps
Machine
Learning
B.Tech / MCA
DBMS Data DAA tutorial Operating Computer
tutorial Structures DAA
System Network
DBMS
tutorial Operating
tutorial
Data Structures System Computer
Network
Compiler Computer Discrete Ethical Computer
Design tutorial Organization Mathematics Hacking Graphics
and Tutorial Tutorial
Compiler Design Ethical Hacking
Architecture Discrete Computer
Computer Mathematics Graphics
Organization
Software html tutorial Cyber Automata C Language
Engineering Web Technology
Security Tutorial tutorial
Software
tutorial Automata C Programming
Engineering Cyber Security
C++ tutorial Java tutorial .Net Python List of
C++ Java
Framework tutorial Programs
tutorial Python Programs
.Net
Control Data Mining Data
Systems Tutorial Warehouse
tutorial Data Mining
Tutorial
Control System Data Warehouse