SQL Tutorial Part 6.1 | Scalar Functions in SQL

Welcome to SQL Tutorial Part 6.1  - in this session, we’ll explore Scalar user defined Functions in SQL. Scalar functions are user‑defined or built‑in functions that return a single value based on the input provided. They help simplify repetitive calculations and make queries more modular and readable. 🔑 What you’ll learn in this video: 1. What are scalar functions in SQL? 2. Syntax to create scalar user‑defined functions 3. How to use scalar functions in SELECT statements 🎯 Who is this for? 1. Beginners learning SQL step by step 2. Students preparing for exams or interviews 3. Developers refining their database fundamentals By the end of this video, you’ll be confident in creating and using scalar functions to make your SQL queries more efficient and reusable. exam‑ready syntax guide for creating and altering scalar user‑defined functions (UDFs) in SQL Server: CREATE FUNCTION Syntax- CREATE FUNCTION dbo.GetFullName ( @FirstName NVARCHAR(50), @LastName NVARCHAR(50) ) RETURNS NVARCHAR(100) AS BEGIN DECLARE @FullName NVARCHAR(100); SET @FullName = @FirstName + ' ' + @LastName; RETURN @FullName; END; ALTER FUNCTION Syntax :- ALTER FUNCTION dbo.GetFullName ( @FirstName NVARCHAR(50), @LastName NVARCHAR(50), @Title NVARCHAR(20) = 'Ms.' ) RETURNS NVARCHAR(120) AS BEGIN DECLARE @FullName NVARCHAR(120); SET @FullName = @Title + ' ' + @FirstName + ' ' + @LastName; RETURN @FullName; END; Execution of FUNCTION :- SELECT dbo.GetFullName('SAVY', 'TheTechLioness'); -- Output: SAVY TheTechLioness SELECT dbo.GetFullName('Savita', 'TheTechLioness', 'Ms.'); -- Output: Ms. Savita TheTechLioness