You are on page 1of 6

GROUPING DATA FROM TABLES IN SQL

GROUP BY:
The GROUP BY clause is another section of SELECT statement. This optional clause tells oracle
to group rows based on distinct values that exist for specified columns.
I.e. it creates a data set, containing several sets of records grouped together based on a
condition.
Example:
Retrieve the product numbers and the total quantity ordered for each product from the
sales_order_details table.

Detorder_no Product_No Qty_Ordered Qty_Disp


O19001 P00001 10 10
O19001 P00004 3 3
O19001 P00006 7 7
O19002 P00002 4 4
O19002 P00005 10 10
O19003 P00003 2 2
O19004 P00001 6 6
O19004 P00006 4 4
O19005 P00004 1 1
O19006 P00006 8 8

SELECT product_no, sum(qty_ordered) “Total Qty Ordered”


FROM sales_order_details
GROUP BY product_no;

Output:

Product No Total Qty Ordered


P00001 16
P00002 4
P00003 2
P00004 4
P00005 10
P00006 19
In the above example the common rows in the column product_no are grouped together and
the total quantity ordered for each product is displayed on the screen.

HAVING Clause:
The HAVING clause can be the used in conjunction with the GROUP BY clause. HAVING imposes
a condition on the group by clause, which further filters the groups created by the group by
clause.
Example:
Retrieve the product_no and the total quantity ordered for products ‘P00001’, ‘P00004’ from
the sales_order_details
SELECT product_no, sum(qty_ordered) “Total Qty ordered”
FROM sales_order_details
GROUP BY product_no
HAVING product_no=’P00001’ or product_no=’P00004’;

Output:

Product No Total Qty Ordered


P00001 16
P00004 4

In the above example, the common rows in the column product_no are grouped together and
the total quantity ordered for only the product numbers specified in the having clause are
displayed on the screen.
SUBQUERIES
 A subquery is a form of an SQL statement that appears inside another SQL statement.
 It is also termed as nested query.
 The statement containing a subquery is called a parent statement. The parent
statement uses the rows returned by the subquery.
Subqueries can be used by the following commands

 To insert records in a target table


 To create tables and insert records in the table created
 To update records in a target table.
 To create views
 To provide values for conditions in WHERE, HAVING, IN etc. used with SELECT, UPDATE
and DELETE statements.

Examples:
1. Retrieve all orders placed by a client named ‘Rahul Desai’ from the sales_order table.
Table name: sales_order

Order No ClientNo Order Date


O19001 C00006 12-Apr-97
O19002 C00002 25-Dec-97
O19003 C00007 3-Oct-97
O19004 C00005 18-Jun-97
O19005 C00002 20-Aug-97
O19006 C00007 12-Jan-97

Table name: Client_Master

Client NO Name Bal_Due


C00001 Ashok Mehra 500
C00002 Vishal 1000
C00003 Ajay Mehta 0
C00004 Rohit 0
C00005 Nalini 0
C00006 Prem 0
C00007 Rahul Desai 0
SELECT * FROM sales_order
WHERE client_no= (SELECT client_no FROM client_master WHERE name=’Rahul Desai’);

Output:

Order No ClientNo Order Date


O19003 C00007 3-Oct-97
O19006 C00007 12-Jan-97

The table sales_order contains client_no and all associated sales order information about this
client. However the sales_order does not contain a client’s name. In client_master table, each
client is identified by a unique client_no. This table also holds the client’s name.
If we wish to see all the orders placed by a client ‘Rahul Desai’, we have to retrieve Rahul
Desai’s client_no from the client_master table. Having done this, we can retrieve the orders
placed by Rahul Desai from the sales_order table.

Exercise:
Find out all the products that are not being sold from the product_master table, based on the
products actually sold as shown in the sales_order_details table.

Table name: sales_order_details

Detorder_no Product_No Qty_Ordered Qty_Disp


O19001 P00001 10 10
O19001 P00004 3 3
O19001 P00006 7 7
O19002 P00002 4 4
O19002 P00005 10 10
O19003 P00003 2 2
O19004 P00001 6 6
O19004 P00006 4 4
O19005 P00004 1 1
O19006 P00006 8 8
Table name: Product master

Product_No Description
P00001 1.44 Floppies
P00002 Monitors
P00003 Mouse
P00004 1.22 Floppies
P00005 keyboards
P00006 CD Drive
P00007 HDD
P00008 1.44 Drive
P00009 1.22 Drive

Example:
Retrieve the names of all persons who work in Pradeep’s department and have worked on an
inventory control system as well, from the tables emp and inv_sys
Table name: emp

Emp No Ename Dept No


1 Rahul D01
2 Joshi D02
3 Lenna D01
4 Ashwariya D01
5 Pradeep D01
6 Arjun D01
7 Preetam D01
8 Sangeeta D02
9 Prashant D02
10 Melba D02

Table name: inv_sys

Ename Performance
Rahul Good
Joshi Average
Lenna Excellent
Pradeep Excellent
Preetam Ok
Sangeeta Excellent
Melba Good
SELECT ename, dept_no
FROM emp
WHERE dept_no IN ( SELECT dept_no FROM emp WHERE ename=’Pradeep’)
AND ename IN (SELECT ename from inv_sys);

If a select statement is defined as a subquery, the innermost select statement gets executed
first. Thus in the above example, oracle executes
SELECT ename FROM inv_sys
The data retrieved by the above statement will be passed to the WHERE clause of the query as
in
ename in (‘Rahul’,’Joshi’,’Lenna’,’Pradeep’,’Preetam’,’Sangeeta’,’Melba’)
Similarly when oracle executes
SELECT dept_no FROM emp where ename=’Pradeep’;
the output will be passed to the WHERE clause as in
dept_no IN (‘D01’)
Thus the final query after replacing the inner queries with retrieved values will be
SELECT ename, dept_no
FROM emp
WHERE dept_no IN (‘D01’) AND
ename IN (‘Rahul’,’Joshi’,’Lenna’,’Pradeep’,’Preetam’,’Sangeeta’,’Melba’);

Output:

Ename Dept No
Lenna D01
Pradeep D01
Preetam D01
Rahul D01