Professional Documents
Culture Documents
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.
Output:
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:
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
Examples:
1. Retrieve all orders placed by a client named ‘Rahul Desai’ from the sales_order table.
Table name: sales_order
Output:
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.
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
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