0% found this document useful (0 votes)
5 views

RDBMS unit 4

The document provides an overview of key concepts in Relational Database Management Systems, focusing on sorting, grouping data, and joining tables in Oracle SQL. It explains the use of clauses like ORDER BY, GROUP BY, and HAVING, as well as different join styles including EQUIJOIN, NATURAL JOIN, and OUTER JOIN. Additionally, it covers the syntax and examples for performing these operations to manipulate and retrieve data from multiple tables.

Uploaded by

balajikamble4321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

RDBMS unit 4

The document provides an overview of key concepts in Relational Database Management Systems, focusing on sorting, grouping data, and joining tables in Oracle SQL. It explains the use of clauses like ORDER BY, GROUP BY, and HAVING, as well as different join styles including EQUIJOIN, NATURAL JOIN, and OUTER JOIN. Additionally, it covers the syntax and examples for performing these operations to manipulate and retrieve data from multiple tables.

Uploaded by

balajikamble4321
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Subject : [BCS-403] Relational Database Management Systems

Class : SYBSc [CS] - Sem IV By : Ms. Archana Ojha


Unit IV : Sorting & Grouping Data, Joining Tables, Subqueries in ORACLE
2.1 What is Sorting , ORDER BY & ORDER BY DESC Clause
Sorting is a process of arranging or rearranging data records either in Ascending or
Descending order.
Sorting may be based on numeric or alphabetical values.
To perform sorting while displaying records, “ORDER BY” clause can be used with
Select statement.
If order sequence is not specified, by default sorting done is in Ascending order.
Syntax :
SQL> SELECT * / COL_LIST FROM <TABLE_NAME> [WHERE EXP] ORDER BY COL_NAME
[ASC / DESC] ;
E.g. :
SQL > SELECT * FROM STUDENT ORDER BY RNO DESC ;
SQL > SELECT * FROM STUDENT WHERE COURSE=’ABC’ ORDER BY RNO DESC ;

2.2 GROUP BY & GROUP BY HAVING Clause


A database table is used to store the data in record format. The sequence in which
records are inserted, in the same sequence, they are stored and accessed.
In many cases, data records are to be accessed in grouping form.
Data Grouping is a technique by which data records are clubbed / grouped based on
given expression.
To perform data grouping, “GROUP BY” clause is used with select statement as :
Syntax :
SQL> SELECT ---- FROM <TABLE_NAME> GROUP BY <EXP> ;
E.g. :
SELECT * FROM SALESDEPARTMENT GROUP BY ITEM ;
Data grouping is also useful while performing group / aggregate functions as :
SQL> SELECT ITEM, SUM(SALE) AS "TOTAL SALES" FROM SALESDEPARTMENT GROUP BY
ITEM ;

Using HAVING Clause :


By default, select statement with GROUP BY Clause performs data grouping on all
records. In some cases, data grouping is to be performed on selected records based on
given criteria.
To specify selection criteria of records at the time of grouping data, ‘HAVING’
clause is used as :
SQL > SELECT --- FROM <TABLE_NAME> GROUP BY <EXP> HAVING <EXP> ;
E.g. :
SQL > SELECT * FROM SALESDEPARTMENT GROUP BY ITEM HAVING ITEM='SHOES' ;
‘HAVING’ clause can also be used with the group by clause while performing group
/ aggregate functions as :
SQL > SELECT ITEM, MIN(SALE) AS "TOTAL SALES" FROM SALESDEPARTMENT GROUP BY
ITEM HAVING ITEM='SHOES' ;

2.3 What is Join , Join Styles : THETA, ANSI Style


Join is a method used to combine two or more tables, views based on a common
condition.
Sometimes it is necessary to work with multiple tables as though they were a
single entity. Then a single SQL sentence can manipulate data from all the tables. Joins
are used to achieve this.
Join is a query that is used to combine rows from two or more tables, views. It
retrieves data from multiple tables and creates a new table.
The goal of creating a join condition is that it helps you to combine the data from
two or more DBMS tables. The tables in DBMS are associated using the primary key and
foreign keys.
 Join Condition :
1) The SELECT and FROM clauses are both required SQL statement elements; the WHERE
clause is a required element of an SQL statement when joining tables.

2) The tables being joined are listed in the FROM clause.

3) The join is performed in the WHERE clause.

4) Several operators can be used to join tables, such as =, <, >, <>, <=, >=, !=, BETWEEN,
LIKE, and NOT; they can all be used to join tables.

However, the most common operator is the equal (=) symbol.

 Join Styles :
Joins can be done using any of the following two styles :

i) ANSI – style:

SQL > SELECT <ColumnName1>, <ColumnName2>, <ColumnName N>


FROM <TableName1> INNER JOIN <TableName2>
ON <TableName1>.<ColumnName1>=<TableName2>.<ColumnName2>
WHERE <Condition>ORDER BY <Col_Name1>, <Col_Name2>, <Col_Name N> ;

ii) THETA – style:

SQL > SELECT <ColumnName1>, <ColumnName2>, <ColumnName N>


FROM <TableName1>, <TableName2>
WHERE <TableName1>.<ColumnName1>=<TableName2>.<ColumnName2> ORDER
BY <ColumnName1>, <ColumnName2>, <ColumnName N> ;

2.4 Types of Joins :


When you join tables, the type of join that you create determines the rows that
appear in the result set. The most common types of joins are :

1) EQUIJOINS / INNER JOINS

2) NATURAL JOINS

3) NON-EQUIJOINS

4) OUTER JOINS
a) Left Outer Join

b) Right Outer Join

c) Full Outer Join

5) SELF JOINS

6) CROSS JOINS/ CARTESIAN PRODUCT

1) EQUI JOIN / INNER JOIN


Perhaps the most used and important of the joins is the EQUIJOIN, also referred
to as an INNER JOIN. The EQUIJOIN joins two tables with a common column in which
each is usually the primary key. This join returns all the rows from the both the tables
where there is a match.

 THETA STYLE :
SQL> select * from Student, Sdetails where Student.Rno = Sdetails.RNO ;

 ANSI STYLE :
SQL>select * from Student inner join Sdetails on Student.Rno = Sdetails.RNO ;

For E.g. :

STUDENT Table :

RNO NAME CLASS COUNTRY

1 ABC BCSSY INDIA

2 LMN BCSSY INDIA

3 XYZ BCASY INDIA

4 PQR BCASY INDIA

5 JKL BCSSY INDIA


SDETAILS Table :

RNO SUBJECT T1 T2 TOTAL AVERAGE

3 RDBMS 18 19 37 18.5

1 RDBMS 20 19 39 19.5

4 RDBMS 19 19 38 19

2 RDBMS 20 19 39 19.5

5 RDBMS 20 17 37 18.5

OUTPUT :

RNO NAME CLASS COUNTRY RNO SUBJECT T1 T2 TOTAL AVERAGE

3 XYZ BCASY INDIA 3 RDBMS 18 19 37 18.5

1 ABC BCSSY INDIA 1 RDBMS 20 19 39 19.5

2 LMN BCSSY INDIA 2 RDBMS 19 19 38 19

4 PQR BCASY INDIA 4 RDBMS 20 19 39 19.5

5 JKL BCSSY INDIA 5 RDBMS 20 17 37 18.5

2) NATURAL JOIN
This is something similar to Inner join. Natural join and inner join gives the same
output. The only difference is when “SELECT * FROM” is given in query Natural join
display the common column (here RNO) of both the tables (STUDENT and SDETAILS)
only once. While inner join display the common column two times in the output.

SQL> select * from Student natural join Sdetails ;


OUTPUT :

RNO NAME CLASS COUNTRY SUBJECT T1 T2 TOTAL AVERAGE

3 XYZ BCASY INDIA RDBMS 18 19 37 18.5

1 ABC BCSSY INDIA RDBMS 20 19 39 19.5

2 LMN BCSSY INDIA RDBMS 19 19 38 19

4 PQR BCASY INDIA RDBMS 20 19 39 19.5

5 JKL BCSSY INDIA RDBMS 20 17 37 18.5

3) NON EQUI JOIN


While inner join display the common column two Equi Join is a kind of join where
condition uses equal (=) sign. And Non Equi Join uses all comparison operators except
the equal (=) operator like !=, >=, <=, <, >.

SQL> select * from STUDENT ,SDETAILS where STUDENT.RNO <> SDETAILS.RNO ;

STUDENT Table :

RNO NAME CLASS COUNTRY

1 ABC BCSSY INDIA

2 LMN BCSSY INDIA

3 XYZ BCASY INDIA

4 PQR BCASY INDIA

5 JKL BCSSY INDIA

SDETAILS Table :

RNO SUBJECT T1 T2 TOTAL AVERAGE

3 RDBMS 18 19 37 18.5

1 RDBMS 20 19 39 19.5

2 RDBMS 19 19 38 19
OUTPUT :

RNO NAME CLASS COUNTRY RNO SUBJECT T1 T2 TOTAL AVERAGE

1 ABC BCSSY INDIA 3 RDBMS 18 19 37 18.5


2 LMN BCSSY INDIA 3 RDBMS 18 19 37 18.5
4 PQR BCASY INDIA 3 RDBMS 18 19 37 18.5
5 JKL BCSSY INDIA 3 RDBMS 18 19 37 18.5
2 LMN BCSSY INDIA 1 RDBMS 20 19 39 19.5
3 XYZ BCASY INDIA 1 RDBMS 20 19 39 19.5
4 PQR BCASY INDIA 1 RDBMS 20 19 39 19.5
5 JKL BCSSY INDIA 1 RDBMS 20 19 39 19.5
1 ABC BCSSY INDIA 2 RDBMS 19 19 38 19
3 XYZ BCASY INDIA 2 RDBMS 19 19 38 19
4 PQR BCASY INDIA 2 RDBMS 19 19 38 19
5 JKL BCSSY INDIA 2 RDBMS 19 19 38 19

4) OUTER JOIN
This join returns all the rows from one table and only those rows from second
table which meets the condition. They can be further Classified as :

a. Left Outer Join


b. Right Outer Join
c. Full Outer Join

a. Left Outer Join


Returns all the rows from left table (ie, first table) and rows that meet the
condition from second table. All the rows from left table and only matching rows
from the right table.
For E.g. :
STUDENT Table :

RNO NAME CLASS COUNTRY

1 ABC BCSSY INDIA

2 LMN BCSSY INDIA

3 XYZ BCASY INDIA

4 PQR BCASY INDIA

5 JKL BCSSY INDIA

SDETAILS Table :

RNO SUBJECT T1 T2 TOTAL AVERAGE

3 RDBMS 18 19 37 18.5

1 RDBMS 20 19 39 19.5

2 RDBMS 19 19 38 19

SQL> SELECT * FROM STUDENT LEFT OUTER JOIN SDETAILS ON STUDENT.RNO =


SDETAILS.RNO ;
OUTPUT :

RNO NAME CLASS COUNTRY RNO SUBJECT T1 T2 TOTAL AVERAGE

3 XYZ BCASY INDIA 3 RDBMS 18 19 37 18.5


1 ABC BCSSY INDIA 1 RDBMS 20 19 39 19.5
2 LMN BCSSY INDIA 2 RDBMS 19 19 38 19
5 JKL BCSSY INDIA - - - - - -
4 PQR BCASY INDIA - - - - - -
b. Right Outer Join
Returns all the rows from right table (ie, second table) and rows that meet the
condition from first table. All rows from right table and only matching rows from the
left table.

For E.g. :

SQL> SELECT * FROM STUDENT RIGHT OUTER JOIN SDETAILS ON STUDENT.RNO =


SDETAILS .RNO ;

OUTPUT :

RNO NAME CLASS COUNTRY RNO SUBJECT T1 T2 TOTAL AVERAGE

1 ABC BCSSY INDIA 1 RDBMS 20 19 39 19.5


2 LMN BCSSY INDIA 2 RDBMS 19 19 38 19
3 XYZ BCASY INDIA 3 RDBMS 18 19 37 18.5

c. Full Outer Join


Combines the results of both left and right outer joins. Non-matching records
from both the tables will be left blank.

SQL> SELECT * FROM STUDENT FULL OUTER JOIN SDETAILS ON STUDENT.RNO =


SDETAILS.RNO ;

OUTPUT :

RNO NAME CLASS COUNTRY RNO SUBJECT T1 T2 TOTAL AVERAGE

3 XYZ BCASY INDIA 3 RDBMS 18 19 37 18.5


1 ABC BCSSY INDIA 1 RDBMS 20 19 39 19.5
2 LMN BCSSY INDIA 2 RDBMS 19 19 38 19
5 JKL BCSSY INDIA - - - - - -
4 PQR BCASY INDIA - - - - - -
2.5 Self Join, Cross Join, Joining Three Tables

CROSS JOIN

The CROSS JOIN specifies that all rows from first table join with all of the rows of
second table. If there are "x" rows in table1 and "y" rows in table2 then the cross join
result set have x*y rows. It normally happens when no matching join columns are
specified.

In simple words you can say that if two tables in a join query have no join
condition, then the Oracle returns their Cartesian product.

Syntax :

SQL> SELECT * FROM table1 CROSS JOIN table2 ;


OR
SQL> SELECT * FROM table1, table2 ;

Both the above syntax are same and used for Cartesian product. They provide
similar result after execution.

Image representation of cross join

Oracle Cross Join Example

Let's take two tables "customer" and "supplier".

Customer Table

CUSTOMER_ID FIRST_NAME LAST_NAME

1 ABCD PATIL
2 RAMAN KAPOOR
3 AKSHAY SINGH
Supplier Table

SUPPLIER_ID FIRST_NAME LAST_NAME


1 KARAN ARORA
2 KAVYA NANDA
3 RAKESH KHANNA

Cross Join Query :

SQL> SELECT * FROM customer,supplier ;

OUTPUT :

CUSTOMER_ID FIRST_NAME LAST_NAME SUPPLIER_ID FIRST_NAME LAST_NAME


1 ABCD PATIL 1 KARAN ARORA
1 ABCD PATIL 2 KAVYA NANDA
1 ABCD PATIL 3 RAKESH KHANNA
2 RAMAN KAPOOR 1 KARAN ARORA
2 RAMAN KAPOOR 2 KAVYA NANDA
2 RAMAN KAPOOR 3 RAKESH KHANNA
3 AKSHAY SINGH 1 KARAN ARORA
3 AKSHAY SINGH 2 KAVYA NANDA
3 AKSHAY SINGH 3 RAKESH KHANNA

JOINING THREE TABLES


There may occur some situations sometimes where data needs to be fetched
from three or more tables.
In a three-table join, Oracle joins two of the tables and joins the result with the
third table.
When the query in the following listing is executed, the EMP, DEPT, and ORDERS
tables are joined together, as illustrated in Table 1.
Example :

Student Table

S_ID S_NAME
4 Praveen
5 Bisa
6 Suraj
1 Jack
2 Rithvik
3 Jaspreet

Marks Table

SCHOOL_ID S_ID SCORE STATUS

1004 1 23 fail
1008 6 95 pass
1012 2 97 pass
1016 7 67 pass
1020 3 100 pass
1025 8 73 pass
1030 4 88 pass
1035 9 13 fail
1040 5 16 fail
1050 10 53 pass

Details Table

ADDRESS_CITY EMAIL_ID SCHOOL_ID ACCOMPLISHMENTS


Banglore [email protected] 1020 ACM ICPC selected
Hyderabad [email protected] 1030 Geek of the month
Delhi [email protected] 1012 IOI finalist
Chennai [email protected] 1111 Geek of the year
Banglore [email protected] 1008 IMO finalist
Mumbai [email protected] 2211 Made a robot
Ahmedabad [email protected] 1172 Code Jam finalist
Jaipur [email protected] 1972 KVPY finalist
Joining Three Tables Query :

SQL> SELECT S_NAME, SCORE, STATUS, ADDRESS_CITY, EMAIL_ID, ACCOMPLISHMENTS


FROM STUDENT S, MARKS M, DETAILS D WHERE S.S_ID = M.S_ID AND M.SCHOOL_ID =
D.SCHOOL_ID ;

OUTPUT :

S_NAME SCORE STATUS ADDRESS_CITY EMAIL_ID ACCOMPLISHMENTS


Praveen 88 pass Hyderabad [email protected] Geek of the month
Suraj 95 pass Banglore [email protected] IMO finalist
Rithvik 97 pass Delhi [email protected] IOI finalist
Jaspreet 100 pass Banglore [email protected] ACM ICPC selected

2.6 Subqueries and its Types

As programming language statements can be used in nested form, similarly SQL as


database programming also allows nesting of statements.
When one SQL statement is enclosed within other, it becomes nested statement
or Sub-Query.
SQL Statement within another SQL Statement is called Sub-Query, which creates
one outer (Parent) query and one inner (Child) query.
Oracle subquery is a SELECT statement that is written inside another statement or
query (We can say it a nested query or a query which is present within a query) which
can be SELECT, INSERT, UPDATE or even DELETE statement and generally, these sub-
queries reside inside the WHERE clause, the FROM clause or the SELECT clause so that
the user can better construct and create more complex queries with the help of
subqueries without even using joins or unions to write complex queries.
In a Sub-query, the inner query executes first and its result is used to execute
the outer query as :
SQL > SQL Stmt. ( SQL Stmt. ) ;

Parent Query Child Query


This nesting of queries can be done at any level :
SQL > SQL Stmt_1 ( SQL Stmt_2 ( SQL Stmt_3 ) ) ;
Sub-Query can be used for the following :
1. Read or Retrieve Data
2. Create Copy of Table
3. Copy Data Records
4. Create View

Types of Subqueries

1.) Single Row Sub Query :


Sub query which returns single row output. They mark the usage of single row
comparison operators, when used in WHERE conditions.
A single-row subquery is used when the outer query's results are based on a
single, unknown value. Although this query type is formally called "single-row," the
name implies that the query returns multiple columns-but only one row of results.
In the below SELECT query, inner SQL returns only one row i.e. the minimum
salary for the company. It in turn uses this value to compare salary of all the employees
and displays only those, whose salary is equal to minimum salary.

EMPLOYEES Table :
FNAME SALARY DEPT_ID AGE

ABC 15000 101 36


DEF 5000 102 30
XYZ 25000 101 32
LMN 12000 103 35
JKL 18000 102 35
WER 5000 102 40
Single Row Sub Query :
SQL > SELECT FNAME, SALARY, DEPT_ID FROM EMPLOYEES WHERE SALARY = (SELECT
MIN (SALARY) FROM EMPLOYEES);
Output :
FNAME SALARY DEPT_ID

DEF 5000 102


WER 5000 102

2.) Multiple row sub query :


Sub query returning multiple row output. They make use of multiple row
comparison operators like IN, ANY, ALL. There can be sub queries returning multiple
columns also.
Multiple-row subqueries are nested queries that can return more than one row of
results to the parent query. Multiple-row subqueries are used most commonly in WHERE
and HAVING clauses. Since it returns multiple rows, it must be handled by set
comparison operators (IN, ALL, ANY).
EMPLOYEES Table :
FNAME SALARY DEPT_ID AGE

ABC 15000 101 36


DEF 5000 102 30
XYZ 25000 101 32
LMN 12000 103 35
JKL 18000 102 35
WER 5000 102 40
DEPARTMENTS Table :
DEPT_ID LOCATION

101 100
102 100
103 103
Multiple Row Sub Query :
SQL > SELECT FNAME, DEPT_ID FROM employees WHERE DEPT_ID IN (SELECT DEPT_ID
FROM DEPARTMENTS WHERE LOCATION = 100) ;
Output :
FNAME DEPT_ID

XYZ 101
ABC 101
WER 102
JKL 102

DEF 102

You might also like