DBMS Lab 6 N005
DBMS Lab 6 N005
06
PART A
(PART A: TO BE REFFERED BY STUDENTS)
A.1 Aim: To apply order by clause and concept of different types of Joins for solving queries.
A.2 Prerequisite:
A.3 Outcome:
After successful completion of this experiment students will be able to
7. Apply knowledge of relational algebra and structural query language to retrieve and
manage data in relational databases.
A.4 Theory:
Order By Clause:
An ORDER BY clause allows you to specify the order in which rows appear in the result set.
The SQL ORDER BY clause is used to sort the records in the result set for a SELECT statement.
Example:
Sorting above table using order by clause using salary (SAL) column in ascending order.
Query:
Result:
Sorting above table using order by clause using salary (SAL) column in ascending order.
Query:
select * from emp order by SAL desc;
Result:
102 b - - - - - 30
7839 KING PRESIDENT - 11/17/1981 5000 - 10
7788 SCOTT ANALYST 7566 12/09/1982 3000 - 20
7902 FORD ANALYST 7566 12/03/1981 3000 - 20
7566 JONES MANAGER 7839 04/02/1981 2975 - 20
7698 BLAKE MANAGER 7839 05/01/1981 2850 - 30
7782 CLARK MANAGER 7839 06/09/1981 2450 - 10
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7934 MILLER CLERK 7782 01/23/1982 1300 - 10
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7876 ADAMS CLERK 7788 01/12/1983 1100 - 20
101 a b 7839 04/01/2004 1000 10 20
7900 JAMES CLERK 7698 12/03/1981 950 - 30
7369 SMITH CLERK 7902 12/17/1980 800 - 20
The table can also be sorted using column number or column position. While sorting we can specify
whether to display null values first or last using “NULL FIRST” or “NULL LAST” clause.
For example in EMP table column position of SALARY column is 6. Using column position
table will be sorted as below:
Example:
Result:
Joins:
JOINS are used to retrieve data from multiple tables. A JOIN is performed whenever two or
more tables are joined in a SQL statement.
There are 4 different types of Oracle joins:
INNER JOIN:
It is the most common type of join. Oracle INNER JOINS return all rows from multiple
tables where the join condition is met.
Syntax
Example:
Supplier:
supplier_id supplier_name
10000 IBM
10002 Microsoft
10003 NVIDIA
Orders:
Applying inner join between supplier and orders table in order to fetch supplier id,
supplier name and order date from both tables supplier and orders.
Syntax:
Result:
Old Syntax: In earlier version of Oracle inner joins were specified as below:
In order to join above three tables to fetch employee last name, department in which employee is
working and the city where department is located below query will be used.
Another type of join is called an Oracle LEFT OUTER JOIN. This type of join returns all rows
from the LEFT-hand table specified in the ON condition and only those rows from the other
table where the joined fields are equal (join condition is met).
Syntax
SELECT columns FROM table1 LEFT [OUTER] JOIN table2 ON table1.column = table2.column;
Visual Illustration
In this visual diagram, the Oracle LEFT OUTER JOIN returns the shaded area:
The Oracle LEFT OUTER JOIN would return the all records from table1 and only those
records from table2 that intersect withtable1.
Example
This LEFT OUTER JOIN example would return all rows from the suppliers table and only those
rows from the orders table where the joined fields are equal.
If a supplier_id value in the suppliers table does not exist in the orders table, all fields in the
orders table will display as <null> in the result set.
Result:
Syntax
SELECT columns FROM table1 RIGHT [OUTER] JOIN table2 ON table1.column = table2.column;
In some databases, the RIGHT OUTER JOIN keywords are replaced with RIGHT JOIN.
Visual Illustration
In this visual diagram, the Oracle RIGHT OUTER JOIN returns the shaded area:
The Oracle RIGHT OUTER JOIN would return the all records from table2 and only
those records from table1 that intersect with table2.
Example
This RIGHT OUTER JOIN example would return all rows from the orders table and only those
rows from the suppliers table where the joined fields are equal.
If a supplier_id value in the orders table does not exist in the suppliers table, all fields in the
suppliers table will display as <null>in the result set.
Syntax
SELECT columns FROM table1 FULL [OUTER] JOIN table2 ON table1.column = table2.column;
In some databases, the FULL OUTER JOIN keywords are replaced with FULL JOIN.
Visual Illustration
In this visual diagram, the Oracle FULL OUTER JOIN returns the shaded area:
The Oracle FULL OUTER JOIN would return the all records from both table1 and table2.
Natural Join:
The natural join clause is based on all columns in the two tables that have the same name. It
selects rows from the two tables that have equal values in all matched columns.
Syntax:
Query:
Result:
category_header
route_Header
Place Header:
Fleet Header:
Ticket Header:
Ticket Detail:
Route Detail:
Queries:
1. Display all routes sorted using distance (descending order).
2. Display details of those routes for which category description is “Delux”.
3. Find out details of ticket issued to passenger “Charu”.
4. Find out places for which routes are non stop.
5. Display all rows from route header and only matching rows from route detail.
6. Find out common route id’s those are present in route_header and route_detail.
7. Find out names of passengers who travel through routes with origin as ‘Madurai’
and destination as ‘Madras’.
8. Find out fleets which passes through routes which are not non stop.
9. Insert into category_header table new row with below details:
Cat code: 05 cat description: fast
10. Display cat_code, cat_description and all route details for all the categories, whether
route available for category or not.
11. Find out details of those tickets for which origin of one ticket is same as destination
of another ticket.
PART B
(PART B: TO BE COMPLETED BY STUDENTS)
(Students must submit the soft copy as per following segments within two hours of the
practical. The soft copy must be uploaded on the student portal or emailed to the
concerned lab in charge faculties at the end of the practical in case the there is no student
portal access available)
6. Find out common route id’s those are present in route_header and route_detail.
7. Find out names of passengers who travel through routes with origin as ‘Madurai’
and destination as ‘Madras’.
8. Find out fleets which passes through routes which are not non stop.
11. Find out details of those tickets for which origin of one ticket is same as
destination of another ticket.
2. Differentiate between ‘inner join’ and ‘outer join’ operators used for Databases.
Matching Returns rows with matching Returns rows with matching
Rows values in both tables on join values in both tables based on
condition. join condition
Unmatched Excludes unmatched rows from Includes unmatched rows from
Rows both tables. one or both tables.
Types Only one type Three types: Left, Right, Full
Result Set Smaller result set Larger result set
Usage Typically used when only Used when you want to retain
matched rows are needed unmatched rows from one or both
tables
B.3 Conclusion:
Learnt how to apply order by clause and concept of different types of Joins for solving
queries.