0% found this document useful (0 votes)
8 views11 pages

Exp 3,4

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

Exp 3,4

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

Student Name: Khushbu

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

SUBMITTED BY: SUBMITTED T O:


KHUSHBU Ms. SWETA
EXPERIMENT NO. - 03
Aim: Amber's conglomerate corporation just acquired some new companies. Each of the companies follows
this hierarchy:
Given the table schemas below, write a query to print the company_code, founder name, total number of
lead managers, total number of senior managers, total number of managers, and total number of employees.
Order your output by ascending company_code.
Note:The tables may contain duplicate records.The company_code is string, so the sorting should not be
numeric. For example, if the company_codes are C_1, C_2, and C_10, then the ascending company_codes
will be C_1, C_10, and C_2.

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:

• Company: Stores the company_code and founder details.


• Lead_Manager: Stores the lead_manager_id and the company_code for which they work.
• Senior_Manager: Stores the senior_manager_id and the lead_manager_id they report to.
• Manager: Stores the manager_id and the senior_manager_id they report to.
• Employee: Stores the employee_id and the manager_id they report to.

2.Insert Sample Data:

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.

3.Write a SELECT Query: You are tasked to write a query that:

• Retrieves the company_code and founder.


• Counts the total number of lead managers, senior managers, managers, and employees for each
company.
• Orders the result by the company_code (string order, not numeric).

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:

Step 1: Create Tables

Table to store company details:


CREATE TABLE Company (
company_code VARCHAR(10),
founder VARCHAR(50)
);

Table to store lead managers:


CREATE TABLE Lead_Manager (
lead_manager_id INT,
company_code VARCHAR(10)
);

Table to store senior managers:


CREATE TABLE Senior_Manager (
senior_manager_id INT,
lead_manager_id INT
);

Table to store managers:


CREATE TABLE Manager (
manager_id INT,
senior_manager_id INT
);

Table to store employees:


CREATE TABLE Employee (
employee_id INT,
manager_id INT
);

Step 2: Insert Tables

Inserting into Company:

INSERT INTO Company (company_code, founder)


VALUES
('C_1', 'KHUSHBU'), ('C_2', 'KHUSHI'),('C_10', 'SHAGUN');

Inserting into Lead_Manager:

INSERT INTO Lead_Manager (lead_manager_id, company_code)


VALUES
(1, 'C_1'), (2, 'C_1'), (3, 'C_2'), (4, 'C_10');
Inserting into Senior_Manager:

INSERT INTO Senior_Manager (senior_manager_id, lead_manager_id)


VALUES
(1, 1),(2, 2),(3, 3), (4, 4);

Inserting into Manager

INSERT INTO Manager (manager_id, senior_manager_id)


VALUES
(1, 1), (2, 2), (3, 3), (4, 4);

Inserting into Employee

INSERT INTO Employee (employee_id, manager_id)


VALUES
(1, 1), (2, 2), (3, 3), (4, 4);

Step 3: Write the Select Query

SELECT
C.company_code,
C.founder,

COUNT(DISTINCT LM.lead_manager_id) AS total_lead_managers,


COUNT(DISTINCT SM.senior_manager_id) AS total_senior_managers,
COUNT(DISTINCT M.manager_id) AS total_managers,
COUNT(DISTINCT E.employee_id) AS total_employees

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:

Understand Table Structure:

• Students Table: Contains information about students (ID and Name).


• Friends Table: Contains the mapping between a student (ID) and their best friend (Friend_ID).
• Packages Table: Contains the salary (in $ thousands per month) offered to each student, where ID
represents the student.

Identify the Task:

• 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:

Step 1: Create Tables

Creating the Students table:


CREATE TABLE Students (
ID INT PRIMARY KEY,
Name VARCHAR(50)
);

Creating the Friends table:


CREATE TABLE Friends (
ID INT,
Friend_ID INT,
PRIMARY KEY (ID),
FOREIGN KEY (ID) REFERENCES Students(ID),
FOREIGN KEY (Friend_ID) REFERENCES Students(ID)
);

Creating the Packages table:


CREATE TABLE Packages (
ID INT,
Salary INT, -- Salary in $ thousands per month
PRIMARY KEY (ID),
FOREIGN KEY (ID) REFERENCES Students(ID)
);
Step 2: Insert Tables

Inserting data into the Students table:


INSERT INTO Students (ID, Name) VALUES
(1, KHUSHBU),
(2, 'KHUSHI');

Inserting data into the Friends table:


INSERT INTO Friends (ID, Friend_ID) VALUES
(1, 2),
(2, 1);

Inserting data into the Packages table:


INSERT INTO Packages (ID, Salary) VALUES
(1, 4000),
(2, 30000);

Step 3: Query to Output Student Names


Query to find students whose best friends got offered a higher salary:

SELECT S.Name AS Student_Name


FROM Students S
JOIN Friends F ON S.ID = F.ID
JOIN Packages P1 ON S.ID = P1.ID
JOIN Packages P2 ON F.Friend_ID = P2.ID
WHERE P2.Salary > P1.Salary
ORDER BY P2.Salary ASC;
Output:
Learning outcomes:

Understanding and Performing Table Joins:

• 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.

SQL Query Design and Logical Thinking:

• 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).

Handling Relational Data:

• Develop skills in dealing with relational data by using foreign keys (i.e., Friend_ID referring to ID in
the Friends and Students tables).

Using WHERE Clause for Conditions:

• 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).

You might also like