0% found this document useful (0 votes)
6 views18 pages

DBMS Lab 6 N005

This document outlines Experiment No. 06, focusing on the application of the ORDER BY clause and various types of SQL joins. It includes explanations of the ORDER BY clause syntax, examples of sorting data, and detailed descriptions of INNER JOIN and LEFT OUTER JOIN operations. The document aims to enhance students' understanding of relational databases and SQL query management.

Uploaded by

koshangi0204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

DBMS Lab 6 N005

This document outlines Experiment No. 06, focusing on the application of the ORDER BY clause and various types of SQL joins. It includes explanations of the ORDER BY clause syntax, examples of sorting data, and detailed descriptions of INNER JOIN and LEFT OUTER JOIN operations. The document aims to enhance students' understanding of relational databases and SQL query management.

Uploaded by

koshangi0204
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

Experiment No.

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:

DML commands of SQL

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.

Syntax for Order By clause:

ORDER BY { column-Name | ColumnPosition | Expression }


[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
[ , column-Name | ColumnPosition | Expression
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]

Consider Emp table as below:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT - 11/17/1981 5000 - 10


7698 BLAKE MANAGER 7839 05/01/1981 2850 - 30
7782 CLARK MANAGER 7839 06/09/1981 2450 - 10
7566 JONES MANAGER 7839 04/02/1981 2975 - 20
7788 SCOTT ANALYST 7566 12/09/1982 3000 - 20
7902 FORD ANALYST 7566 12/03/1981 3000 - 20
7369 SMITH CLERK 7902 12/17/1980 800 - 20
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7876 ADAMS CLERK 7788 01/12/1983 1100 - 20
7900 JAMES CLERK 7698 12/03/1981 950 - 30
7934 MILLER CLERK 7782 01/23/1982 1300 - 10
101 A b 7839 04/01/2004 1000 10 20
102 B - - - - - 30

Example:

Sorting above table using order by clause using salary (SAL) column in ascending order.

Query:

select * from emp order by SAL asc;

Result:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 12/17/1980 800 - 20


7900 JAMES CLERK 7698 12/03/1981 950 - 30
101 A b 7839 04/01/2004 1000 10 20
7876 ADAMS CLERK 7788 01/12/1983 1100 - 20
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7934 MILLER CLERK 7782 01/23/1982 1300 - 10
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7782 CLARK MANAGER 7839 06/09/1981 2450 - 10
7698 BLAKE MANAGER 7839 05/01/1981 2850 - 30
7566 JONES MANAGER 7839 04/02/1981 2975 - 20
7902 FORD ANALYST 7566 12/03/1981 3000 - 20
7788 SCOTT ANALYST 7566 12/09/1982 3000 - 20
7839 KING PRESIDENT - 11/17/1981 5000 - 10
102 B - - - - - 30

Sorting above table using order by clause using salary (SAL) column in ascending order.

Query:
select * from emp order by SAL desc;

Result:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

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:

select * from emp order by 6;

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7369 SMITH CLERK 7902 12/17/1980 800 - 20


7900 JAMES CLERK 7698 12/03/1981 950 - 30
101 a b 7839 04/01/2004 1000 10 20
7876 ADAMS CLERK 7788 01/12/1983 1100 - 20
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7934 MILLER CLERK 7782 01/23/1982 1300 - 10
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7782 CLARK MANAGER 7839 06/09/1981 2450 - 10
7698 BLAKE MANAGER 7839 05/01/1981 2850 - 30
7566 JONES MANAGER 7839 04/02/1981 2975 - 20
7902 FORD ANALYST 7566 12/03/1981 3000 - 20
7788 SCOTT ANALYST 7566 12/09/1982 3000 - 20
7839 KING PRESIDENT - 11/17/1981 5000 - 10
102 b - - - - - 30

Example:

select * from emp order by comm nulls first;

Result:

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO

7839 KING PRESIDENT - 11/17/1981 5000 - 10


7698 BLAKE MANAGER 7839 05/01/1981 2850 - 30
7782 CLARK MANAGER 7839 06/09/1981 2450 - 10
7566 JONES MANAGER 7839 04/02/1981 2975 - 20
7788 SCOTT ANALYST 7566 12/09/1982 3000 - 20
7876 ADAMS CLERK 7788 01/12/1983 1100 - 20
7369 SMITH CLERK 7902 12/17/1980 800 - 20
102 b - - - - - 30
7934 MILLER CLERK 7782 01/23/1982 1300 - 10
7900 JAMES CLERK 7698 12/03/1981 950 - 30
7902 FORD ANALYST 7566 12/03/1981 3000 - 20
7844 TURNER SALESMAN 7698 09/08/1981 1500 0 30
101 a b 7839 04/01/2004 1000 10 20
7499 ALLEN SALESMAN 7698 02/20/1981 1600 300 30
7521 WARD SALESMAN 7698 02/22/1981 1250 500 30
7654 MARTIN SALESMAN 7698 09/28/1981 1250 1400 30

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:

Oracle INNER JOIN (or sometimes called simple join)


Oracle LEFT OUTER JOIN (or sometimes called LEFT JOIN)
Oracle RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
Oracle FULL OUTER JOIN (or sometimes called FULL JOIN)

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

The syntax for the INNER JOIN in Oracle/PLSQL is:

SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column;

Example:

Consider supplier and Orders table as below:

Supplier:

supplier_id supplier_name

10000 IBM

10001 Hewlett Packard

10002 Microsoft

10003 NVIDIA
Orders:

order_id supplier_id order_date

500125 10000 2003/05/12

500126 10001 2003/05/13

500127 10004 2003/05/14

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:

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers INNER JOIN orders


ON suppliers.supplier_id = orders.supplier_id;

Result:

supplier_id Name order_date

10000 IBM 2003/05/12

10001 Hewlett Packard 2003/05/13

Old Syntax: In earlier version of Oracle inner joins were specified as below:

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers, orders


WHERE suppliers.supplier_id = orders.supplier_id;

Joining more than one table:

Example: Consider tables as below:


To join n tables together, you need a minimum of n-1 join conditions. For example to join three
tables, a minimum of two joins is required.

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.

select employees.last_name, departments.department_id, locations.city from employees, departments, locations


where employees.department_id=departments.department_id and departments.location_id=locations.location_id;

Left Outer Join:

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

The syntax for the Oracle LEFT OUTER JOIN is:

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

SELECT suppliers.supplier_id, suppliers.supplier_name, orders.order_date FROM suppliers LEFT OUTER


JOIN orders ON suppliers.supplier_id = orders.supplier_id;

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:

supplier_id supplier_name order_date

10000 IBM 2003/05/12

10001 Hewlett Packard 2003/05/13

10002 Microsoft <null>

10003 NVIDIA <null>

Right Outer Join:


Another type of join is called an Oracle RIGHT OUTER JOIN. This type of join returns all rows
from the RIGHT-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

The syntax for the Oracle RIGHT OUTER JOIN is:

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

Here is an example of an Oracle RIGHT OUTER JOIN:

SELECT orders.order_id, orders.order_date, suppliers.supplier_name FROM suppliers RIGHT OUTER JOIN


orders ON suppliers.supplier_id = orders.supplier_id;

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.

Full Outer Join:


Another type of join is called an Oracle FULL OUTER JOIN. This type of join returns all rows
from the LEFT-hand table and RIGHT-hand table with nulls in place where the join condition is
not met.

Syntax

The syntax for the Oracle FULL OUTER JOIN is:

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:

SELECT * FROM table1 NATURAL JOIN table2;

Consider below tables: foods and company


ITEM_ID | ITEM_NAME | ITEM_UNIT | COMPANY_ID |
+--------- +-------------- +----------- +------------ +
| 1 | Chex Mix | Pcs | 16 |
| 6 | Cheez-It | Pcs | 15 |
| 2 | BN Biscuit | Pcs | 15 |
| 3 | Mighty Munch | Pcs | 17 |
| 4 | Pot Rice | Pcs | 15 |
| 5 | Jaffa Cakes | Pcs | 18 |
| 7 | Salt n Shake | Pcs | |
+--------- + --------------+----------- +------------ +

COMPANY_ID | COMPANY_NAME | COMPANY_CITY |


+------------ +--------------- +-------------- +
| 18 | Order All | Boston |
| 15 | Jack Hill Ltd | London |
| 16 | Akas Foods | Delhi |
| 17 | Foodies. | London |
| 19 | sip-n-Bite. | New York |
+------------ +--------------- +-------------- +

Query:

select * from foods natural join company;

Result:

COMPANY_ID ITEM_ID ITEM_NAME ITEM_UNIT COMPANY_NAME COMPANY_CITY

---------- ---------- ------------------------- ---------- ------------------------- -


-------------

16 1 Chex Mix Pcs Akas Foods Delhi

15 6 Cheez-It Pcs Jack Hill Ltd London

15 2 BN Biscuit Pcs Jack Hill Ltd London

17 3 Mighty Munch Pcs Foodies. London

15 4 Pot Rice Pcs Jack Hill Ltd London

18 5 Jaffa Cakes Pcs Order All Boston

A.5 Task: For given tables solve below queries:

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)

Roll No.: N005 Name: KOSHANGI


Program : MBA(Tech) CE Division: D
Batch: D1 Date of Experiment:06/03/2024
Date of Submission: 22/03/24 Grade :

B.1 Commands and Output:


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.

B.2 Curiosity Questions:


1. Justify the statement, “Joins are must for better manipulation of the given table”
Joins are essential for better manipulation of tables because they enable:
 Integration of data from multiple tables.
 Flexibility in querying with complex conditions.
 Maintenance of data normalization and integrity.
 Efficient retrieval and aggregation of data.
 Handling of complex relationships in real-world data scenarios.

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

3. Solve below queries for given tables:


Distributor (Dno, Dname, Daddress, Dphone)
Item (Itemno, Itemname, Colour, Weight)
Dist_Item(Dno, Itemno, Qty)

a. Find out distributors, who have supplied item number 01.


select * from distributor inner join dist_item on distributor.dno=dist_item.dno
where item_no=01;
b. Find out details of item supplied by distributor 01.
select * from item inner join dist_item on item.item_no=dist_item.item_no where
dno=01;

B.3 Conclusion:
Learnt how to apply order by clause and concept of different types of Joins for solving
queries.

You might also like