TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2509 2.99K
๐Ÿ”ฅ Now, Letโ€™s move to the next topic:

User Defined Functions (UDFs) in SQL

๐Ÿง  1. What is a User Defined Function (UDF)?

A User Defined Function (UDF) is a custom function created by users.

๐Ÿ‘‰ Accepts input parameters

๐Ÿ‘‰ Performs calculations or logic

๐Ÿ‘‰ Returns a single value

Think like this ๐Ÿ‘‡

โœ” Built-in Function โ†’ SUM(), AVG(), COUNT()

โœ” User Defined Function โ†’ Created by YOU

โšก 2. Why Use UDFs?

โœ” Reuse business logic

โœ” Reduce code repetition

โœ” Improve readability

โœ” Easier maintenance

๐Ÿ”ฅ 3. Basic Function Example

๐Ÿ‘‰ Function to Calculate Bonus

DELIMITER //

CREATE FUNCTION CalculateBonus(
salary DECIMAL(10,2)
)
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
RETURN salary ** 0.10;
END //

DELIMITER ;


โ–ถ๏ธ 4. Execute Function

SELECT CalculateBonus(50000);


Output: 5000

๐Ÿ”ฅ 5. Using Function in Query

SELECT
name,
salary,
CalculateBonus(salary) AS bonus
FROM employees;


โšก 6. Function with Multiple Parameters

DELIMITER //

CREATE FUNCTION TotalIncome(
salary DECIMAL(10,2),
bonus DECIMAL(10,2)
)
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
RETURN salary + bonus;
END //

DELIMITER ;


โ–ถ๏ธ Execute

SELECT TotalIncome(50000, 5000);


Output: 55000

๐Ÿ”ฅ 7. Difference Between Function & Procedure

Feature : Function : Procedure

Must return value : Yes : May or may not return

Used inside SELECT : Yes : Called using CALL

Focus : Calculations : Actions

๐ŸŽฏ 8. Practice Tasks

1. Create function to calculate tax

2. Create function to calculate annual salary

3. Create function to calculate total income

4. Use function inside SELECT query

5. Compare function vs procedure

โšก Mini Challenge ๐Ÿ”ฅ

๐Ÿ‘‰ Create a function: EmployeeGrade(salary)

Rules:

โ€ข salary โ‰ฅ 70000 โ†’ 'A'

โ€ข salary โ‰ฅ 50000 โ†’ 'B'

โ€ข otherwise โ†’ 'C'

Then use it for all employees.

๐Ÿ”ฅ Pro Tip (Interview Gold)

Most asked interview question:

๐Ÿ‘‰ When should you use Function instead of Procedure?

โœ… Use Function when:

โ€ข A value must be returned

โ€ข Logic is reusable inside SELECT statements

โ€ข Calculations are needed frequently

Double Tap โค๏ธ For More
  • โค 8
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series โ€” Part 5 ๐Ÿ“Œ Question 5: Find Employees Who Earn More Than Their Managโ€ฆ
  2. Oct 9, 2026๐Ÿ‡ฎ๐Ÿ‡ณ ๐—š๐—ข๐—ฉ๐—˜๐—ฅ๐—ก๐— ๐—˜๐—ก๐—ง ๐—ข๐—™ ๐—œ๐—ก๐——๐—œ๐—” โ€” ๐—”๐—œ๐—–๐—ง๐—˜ ๐—œ๐—ก๐—ง๐—˜๐—ฅ๐—ก๐—ฆ๐—›๐—œ๐—ฃ๐—ฆ ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ ๐Ÿš€โ€ฆ
  3. Oct 8, 2026๐ŸŽ“ ๐— ๐—ถ๐—ฐ๐—ฟ๐—ผ๐˜€๐—ผ๐—ณ๐˜ ๐—™๐—ฅ๐—˜๐—˜ ๐—–๐—ผ๐˜‚๐—ฟ๐˜€๐—ฒ๐˜€ ๐˜„๐—ถ๐˜๐—ต ๐—–๐—ฒ๐—ฟ๐˜๐—ถ๐—ณ๐—ถ๐—ฐ๐—ฎ๐˜๐—ฒ๐˜€! ๐Ÿš€๐Ÿ”ฅ Upgrโ€ฆ
  4. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  5. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  6. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’