You are on page 1of 6

Vidyashilp University

Databases Management
Semester-IV
2023-24
Lab-Evaluation 2
4th March
2024
Marks: 10 Time: 2 Hour 11:05 AM to 1:10 PM
Name: Abhishek G UEN: 2022UG000045

1. Write a query to display all policy name which has Raksha as a substring.
Solution:

SELECT PName
FROM Policy
WHERE PName LIKE '%Raksha%';

2. Write a query to display all the female agents name who are junior and still working.
Solution:

SELECT Aname
FROM AGENT
WHERE Position = 'Junior'
AND Gender = 'F'
AND EmpStatus = '1';
3. Write a query to display the Total Insurance Sale amount of business done by the agents whose
name has Gowda as a substring.
Example: The Total Insurance Sale of Agent Gowry Gowda is 500000+ 250000 + 300000 =
1050000.
Solution:

SELECT A.Aname, (
SELECT SUM(S.Insurance_Amount)
FROM SALES S
WHERE S.AID = A.AID
) AS Total_Insurance_Sale
FROM AGENT A
WHERE A.Aname LIKE '%Gowda%';

4. Write a query to display the policy names insured by Femle agents.


Solution:
SELECT DISTINCT P.PName
FROM AGENT A, SALES S, Policy P
WHERE A.AID = S.AID
AND S.P_Num = P.P_Num
AND A.Gender = 'F';

5. Write a function to display the name of the Agent and Award.


Award is computed based on total sales done by the Agent. Where the Total sale is sum of all
the Insurance Amount in the business done by the Agent.
Example: Total Sale of Agent Gowry Gowda is 500000+ 250000 + 300000 = 1050000.
If total Sale is more than 250000 the award is Silver. If the total sale is more than 500000, the
award is Gold. If the total sale is more than 750000, the award is Platinum and “No Award” will
be displayed if the above criteria are not satisfied.

The output from the given tables shall be.


Agent Award
Anand Nair Silver
Gowry Gowda Platinum
Vijaya Reddy Silver
Manjunath Gowda No Award
Manoj Chaturvedi Gold
Rishab Gowda No Award
Niranjan No Award
Dhanu No Award
Solution:

CREATE DEFINER=`root`@`localhost` FUNCTION `GetAgentAward`(AIDVal INT) RETURNS


varchar(255) CHARSET utf8mb4
DETERMINISTIC
BEGIN
DECLARE total_sale DECIMAL(10,2);
DECLARE award VARCHAR(255);

SELECT SUM(S.Insurance_Amount) INTO total_sale


FROM SALES S
WHERE S.AID = AIDVal;

IF total_sale > 750000 THEN


SET award = 'Platinum';
ELSEIF total_sale > 500000 THEN
SET award = 'Gold';
ELSEIF total_sale > 250000 THEN
SET award = 'Silver';
ELSE
SET award = 'No Award';
END IF;

RETURN award;
END

Query to execute: SELECT A.Aname, GetAgentAward(A.AID) AS Award


FROM AGENT A;
5. OR

Write a procedure to compute the Premium, Commission and Total Commission for
each business done by the Agent.

Premium= Insurance_Amoutn/Term
Commission = 5% of premium
Total Commission= Commission* Term.
Solution:
CREATE DEFINER=`root`@`localhost` PROCEDURE `ComputeCommission`()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE AID_val INT;
DECLARE P_Num_val INT;
DECLARE Insurance_Amount_val DECIMAL(10, 2);
DECLARE Term_val INT;
DECLARE premium_val DECIMAL(10, 2);
DECLARE commission_val DECIMAL(10, 2);
DECLARE total_commission_val DECIMAL(10, 2);

DECLARE cur CURSOR FOR


SELECT AID, P_Num, Insurance_Amount, Term
FROM SALES;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN cur;
read_loop: LOOP
FETCH cur INTO AID_val, P_Num_val, Insurance_Amount_val, Term_val;
IF done THEN
LEAVE read_loop;
END IF;

SET premium_val = Insurance_Amount_val / Term_val;


SET commission_val = 0.05 * premium_val;
SET total_commission_val = commission_val * Term_val;

INSERT INTO Commission (AID, P_Num, Premium, Commission,


Total_Commission)
VALUES (AID_val, P_Num_val, premium_val, commission_val,
total_commission_val);
END LOOP;
CLOSE cur;
END

CREATE TABLE Commission (


AID INT,
P_Num INT,
Premium DECIMAL(10, 2),
Commission DECIMAL(10, 2),
Total_Commission DECIMAL(10, 2)
);

CALL ComputeCommission();

select * from commission;

Or

CREATE DEFINER=`root`@`localhost` PROCEDURE `ComputeCommission`(


IN AID_val INT,
OUT Premium_val DECIMAL(10, 2),
OUT Commission_val DECIMAL(10, 2),
OUT Total_Commission_val DECIMAL(10, 2)
)
BEGIN
DECLARE Insurance_Amount_val DECIMAL(10, 2);
DECLARE Term_val INT;

SELECT Insurance_Amount, Term INTO Insurance_Amount_val, Term_val


FROM SALES
WHERE AID = AID_val
LIMIT 1;

SET Premium_val = Insurance_Amount_val / Term_val;


SET Commission_val = 0.05 * Premium_val;
SET Total_Commission_val = Commission_val * Term_val;
END

Query to select:

SET @AID_val = 3;
CALL ComputeCommission(@AID_val, @Premium_val, @Commission_val,
@Total_Commission_val);
SELECT @Premium_val AS Premium, @Commission_val AS Commission,
@Total_Commission_val AS Total_Commission;

You might also like