You are on page 1of 15

Department of Computer Science and Engineering (CSE)

Group by, Having and Order


by Clause in DBMS

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

What is Group by?

 The GROUP BY Statement in SQL is used to arrange identical data into


groups with the help of some functions.

 i.e if a particular column has same values in different rows then it will
arrange these rows in a group.

Important Points:

 GROUP BY clause is used with the SELECT statement.


 In the query, GROUP BY clause is placed after the WHERE clause.
 In the query, GROUP BY clause is placed before ORDER BY clause if
used any.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Syntax

SELECT column1, function_name(column2)


FROM table_name
WHERE condition GROUP BY column1, column2
ORDER BY column1, column2;

function_name: Name of the function used for example,


SUM() , AVG().
table_name: Name of the table.
condition: Condition used.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples

SQL> SELECT NAME,


SUM(SALARY) FROM CUSTOMERS
GROUP BY NAME;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Example 2:
SQL> SELECT NAME, SUM(SALARY) FROM CUSTOMERS
GROUP BY NAME;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Order By:
 The SQL ORDER BY clause is used to sort the data in ascending or
descending order, based on one or more columns.
 Some databases sort the query results in an ascending order by
default.

Syntax:

SELECT column-list
FROM table_name
[WHERE condition]
[ORDER BY column1, column2, .. columnN] [ASC | DESC];

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Examples:

SQL> SELECT * FROM CUSTOMERS ORDER BY NAME,


SALARY;

SQL> SELECT * FROM CUSTOMERS ORDER BY NAME , SALARY


DESC;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

SQL> SELECT * FROM CUSTOMERS ORDER


BY NAME DESC;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Having Clause:

The HAVING Clause enables you to specify conditions


that filter which group results appear in the results.

The WHERE clause places conditions on the selected


columns, whereas the HAVING clause places conditions
on groups created by the GROUP BY clause.

HAVING Clause restricts the data on the group records


rather than individual records.

WHERE and HAVING can be used in a single query.

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Syntax

The following code block shows the position of the


HAVING Clause in a query.
SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY

SELECT column1, column2


FROM table1, table2
WHERE [ conditions ]
GROUP BY column1, column2
HAVING [ conditions ]
ORDER BY column1, column2
University Institute of Engineering (UIE)
Department of Computer Science and Engineering (CSE)

Examples:

SQL > SELECT ID, NAME, AGE, ADDRESS, SALARY


FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 2;

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

University Institute of Engineering (UIE)


Department of Computer Science and Engineering (CSE)

Any questions?

University Institute of Engineering (UIE)

You might also like