0% found this document useful (0 votes)
5 views4 pages

SQL Function Case Study[1]

This case study explores the use of user-defined functions in SQL, specifically focusing on a function that calculates employee bonuses based on salary and years of service. It covers the creation of a scalar-valued function, the benefits of using SQL functions, and provides a practical example with sample data and output. The study emphasizes the importance of code reusability, clarity, and maintainability in SQL programming.
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)
5 views4 pages

SQL Function Case Study[1]

This case study explores the use of user-defined functions in SQL, specifically focusing on a function that calculates employee bonuses based on salary and years of service. It covers the creation of a scalar-valued function, the benefits of using SQL functions, and provides a practical example with sample data and output. The study emphasizes the importance of code reusability, clarity, and maintainability in SQL programming.
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/ 4

Case Study on Function in SQL

Group Members: Harshal Ninawe (23010012)


Harsh Meshram (23010008)
Yash More (23010006)
Navinya Urkude (23010005)
Branch: Artificial Intelligence Sub: DBMS
Sem: IV Date:

Introduction:

In database systems, functions in SQL play a crucial role in simplifying and reusing logic. SQL functions
can accept parameters, perform complex operations, and return results that help in various data processing
tasks. This case study demonstrates the use of a user-defined function to calculate employee bonuses
based on salary and years of service.

Objective:

The objective of this case study is to:

- Understand the concept of user-defined functions in SQL.

- Create a scalar-valued function that calculates an employee's bonus.

- Demonstrate the use of the function within a SQL query.

Theory:

A function in SQL is a compiled set of SQL statements that can take input parameters, process
them, and return a single value (scalar function) or a table (table-valued function). SQL Server
supports the creation of user-defined functions (UDFs) using the CREATE FUNCTION statement.

Types of SQL Functions:

- System-defined functions: Pre-defined in SQL (e.g., GETDATE(), LEN())

- User-defined functions:

- Scalar functions: Return a single value.

- Table-valued functions: Return a table.

Benefits of using SQL functions:

- Code reusability

- Simplified and cleaner queries

- Better maintainability and modular design


Program:

Database Table:

CREATE TABLE Employees (

EmployeeID INT PRIMARY

KEY, FullName

VARCHAR(100), Salary

DECIMAL(10, 2),

YearsOfService INT

);

Insert Sample Data:

INSERT INTO Employees (EmployeeID, FullName, Salary, YearsOfService)

VALUES

(1, 'Alice Johnson', 50000, 3),

(2, 'Bob Smith', 60000, 7),

(3, 'Charlie Davis', 55000, 10);

Create SQL Function:

CREATE FUNCTION CalculateBonus (

@Salary DECIMAL(10,2),

@YearsOfService INT

RETURNS DECIMAL(10,2)

AS

BEGIN

DECLARE @Bonus DECIMAL(10,2);

SET @Bonus = @Salary *


0.10; IF @YearsOfService > 5

SET @Bonus = @Bonus + (@Salary * 0.05);

RETURN @Bonus;

END;

Using the Function in a Query:

SELECT

FullName,

Salary,

YearsOfServic

e,

dbo.CalculateBonus(Salary, YearsOfService) AS

Bonus FROM Employees;

Sample Output:

| FullName | Salary | YearsOfService | Bonus |

|----------------|---------|----------------| ----- |

| Alice Johnson | 50000.00| 3 | 5000.00 |

| Bob Smith | 60000.00| 7 | 9000.00 |

| Charlie Davis | 55000.00| 10 | 8250.00 |

Conclusion:

This case study demonstrated the practical use of a scalar SQL function to implement business

logic for calculating bonuses. By using user-defined functions, we can promote code reuse,

clarity, and maintainability in SQL programming. Functions are a powerful tool for database

developers to modularize and simplify complex logic.

You might also like