Exp 3,4
Exp 3,4
UID: 24MCI10177
Branch: MCA(AI&ML)
Section/Group: 3B
Semester: 1st
Date of Performance: 18 Sep. 2024
Subject Name: PL/SQL lab
Subject Code: 24CAP-602
Task to be done:
1.Create Tables: You need to create the required tables for the database to store the hierarchical data of the
companies. The tables are:
After creating the tables, you need to insert sample data into these tables. This data should cover various
companies, lead managers, senior managers, managers, and employees.
4.Handle Duplicates:
Since the tables may contain duplicate records, ensure that duplicates are not counted multiple times in your
final result. This can be achieved using COUNT(DISTINCT ...) to count unique values.
5.Sort by company_code:
You need to ensure that the company_code is sorted lexicographically (string sorting). For example, C_1
should come before C_10 and C_2.
Code for experiment/practical:
SELECT
C.company_code,
C.founder,
FROM
Company C
LEFT JOIN
Lead_Manager LM ON C.company_code = LM.company_code
LEFT JOIN
Senior_Manager SM ON LM.lead_manager_id = SM.lead_manager_id
LEFT JOIN
Manager M ON SM.senior_manager_id = M.senior_manager_id
LEFT JOIN
Employee E ON M.manager_id = E.manager_id
GROUP BY
C.company_code, C.founder
ORDER BY
C.company_code ASC;
Output:
Learning outcomes:
Table Creation and Data Modelling:
• Learn how to design relational database tables to represent hierarchical relationships (e.g., lead
managers, senior managers, etc.) using primary and foreign keys.
Handling Duplicate Data:
• Understand how to manage and eliminate duplicates using COUNT(DISTINCT ...) to ensure accurate
counts in aggregated data.
String-based Sorting:
• Gain the ability to sort string-based data (e.g., company_code) lexicographically, ensuring proper
ordering for non-numeric codes like C_1, C_10, and C_2.
Joining Multiple Tables:
• Learn how to write efficient queries that join multiple tables and aggregate data from different
hierarchical levels, ensuring all necessary information is captured.
Aggregate Functions:
• Develop proficiency with aggregate functions (e.g., COUNT()) to calculate totals across different roles
(lead managers, senior managers, etc.) while grouping by company_code.
EXPERIMENT NO. - 04
Aim: You are given three tables: Students, Friends and Packages. Students contains two columns: ID
and Name. Friends contains two columns: ID and Friend_ID (ID of the only best friend). Packages
1) contains two columns: ID and Salary (offered salary in $ thousands per month).
Write a query to output the names of those students whose best friends got offered a higher salary than
them. Names must be ordered by the salary amount offered to the best friends. It is guaranteed that no
two students got same salary offer.
Task to be done:
• You need to find students whose best friends were offered a higher salary than them.
• You need to compare the salary of each student with the salary of their best friend.
Data Comparison:
• Join the three tables (Students, Friends, and Packages) to access both the salary of the student and the
salary of their best friend.
• Ensure that you compare the best friend’s salary with the student’s salary.
Sorting:
• After identifying the students whose best friends have a higher salary, the results need to be ordered
by the salary of the best friends in ascending order.
Final Output:
• The query should return the names of the students whose best friends have higher salaries, sorted by
the friend’s salary.
Code for experiment/practical:
• Learn how to join multiple tables based on relationships between columns, specifically using inner
joins to combine data from Students, Friends, and Packages tables.
Data Comparison:
• Understand how to compare data between related rows in different tables, such as comparing the
salary of a student and their best friend from the Packages table.
• Gain experience in designing an SQL query that logically solves the problem by filtering records based
on conditions (i.e., finding students whose best friends have higher salaries).
Sorting Results:
• Learn how to sort the output of an SQL query based on specific columns (in this case, the salary of
the best friends).
• Develop skills in dealing with relational data by using foreign keys (i.e., Friend_ID referring to ID in
the Friends and Students tables).
• Practice using the WHERE clause effectively to filter records based on complex conditions (i.e., when
a best friend’s salary is higher than the student’s salary).