SQL Functions are predefined operations provided by database systems to perform calculations, manipulate text, process dates, handle NULL values, and analyze data efficiently. Instead of writing complex logic manually, developers can use built-in SQL functions to simplify queries and improve productivity.
Functions are widely used in reporting systems, banking applications, e-commerce websites, educational portals, healthcare systems, and business intelligence tools. They help retrieve meaningful information from stored data while reducing query complexity.
Understanding SQL Functions is essential for database developers, administrators, analysts, and students because they are used in almost every real-world SQL query.
An SQL Function is a predefined routine that accepts input values, performs a specific operation, and returns a result.
Functions can be used within SELECT, WHERE, HAVING, ORDER BY, GROUP BY, and other SQL clauses.
SELECT UPPER('cse gyan');
Output:
CSE GYAN
The UPPER() function converts text into uppercase letters.
SQL functions are generally classified into two major categories:
| Function Type | Description |
|---|---|
| Single Row Functions | Operate on one row and return one result. |
| Aggregate Functions | Operate on multiple rows and return one result. |
In Part 1, we will focus on Single Row Functions, Numeric Functions, and String Functions.
Single Row Functions process one row at a time and return a single value for each row.
Numeric Functions perform mathematical calculations on numeric data. They are commonly used in financial systems, statistical reports, billing applications, and scientific calculations.
The ABS() function returns the absolute value of a number by removing its negative sign.
ABS(number)
SELECT ABS(-250);
Output:
250
Banks and accounting systems use ABS() to display positive differences between values.
The ROUND() function rounds a number to a specified number of decimal places.
ROUND(number, decimal_places)
SELECT ROUND(125.6789, 2);
Output:
125.68
The CEILING() function returns the smallest integer greater than or equal to a given number.
SELECT CEILING(10.2);
Output:
11
Shipping systems may round package weights upward to determine charges.
The FLOOR() function returns the largest integer less than or equal to a given number.
SELECT FLOOR(10.9);
Output:
10
The MOD() function returns the remainder after division.
MOD(dividend, divisor)
SELECT MOD(15,4);
Output:
3
String Functions are used to manipulate and process textual information stored in databases.
These functions are commonly used in customer management systems, educational portals, e-commerce platforms, and content management systems.
The UPPER() function converts all characters to uppercase.
SELECT UPPER('cse gyan');
Output:
CSE GYAN
The LOWER() function converts text into lowercase characters.
SELECT LOWER('SQL FUNCTIONS');
Output:
sql functions
The LENGTH() function returns the number of characters in a string.
SELECT LENGTH('Database');
Output:
8
The CONCAT() function combines multiple strings into a single string.
SELECT CONCAT('CSE',' ','Gyan');
Output:
CSE Gyan
The TRIM() function removes leading and trailing spaces from text.
SELECT TRIM(' SQL ');
Output:
SQL
The SUBSTRING() function extracts a portion of text from a string.
SUBSTRING(text,start,length)
SELECT SUBSTRING('Database',1,4);
Output:
Data
SQL functions can be used together in a single query.
SELECT UPPER(
SUBSTRING('csegyan',1,3)
);
Output:
CSE
SELECT UPPER(StudentName), LENGTH(StudentName) FROM Student;
The query converts student names to uppercase and calculates their lengths.
Aggregate Functions are special SQL functions that perform calculations on multiple rows and return a single result. These functions are widely used in reporting, analytics, dashboards, and business intelligence systems.
Unlike single-row functions that work on one value at a time, aggregate functions analyze a group of records together.
The COUNT() function returns the total number of records that match a specified condition.
SELECT COUNT(column_name) FROM table_name;
SELECT COUNT(StudentID) FROM Student;
This query returns the total number of students stored in the Student table.
SELECT COUNT(*) FROM Student;
COUNT(*) counts all rows in a table regardless of NULL values.
The SUM() function calculates the total of a numeric column.
SELECT SUM(column_name) FROM table_name;
SELECT SUM(Fees) FROM Student;
The query returns the total fees collected from all students.
The AVG() function calculates the average value of a numeric column.
SELECT AVG(Marks) FROM Student;
The query returns the average marks of all students.
The MAX() function returns the highest value from a column.
SELECT MAX(Salary) FROM Employee;
The query returns the highest salary in the Employee table.
The MIN() function returns the smallest value from a column.
SELECT MIN(Price) FROM Product;
The query returns the lowest product price.
Multiple aggregate functions can be used within a single query.
SELECT COUNT(*), SUM(Fees), AVG(Fees), MAX(Fees), MIN(Fees) FROM Student;
This query generates a complete summary report.
Date and Time Functions help manage temporal data such as dates, timestamps, schedules, and logs.
These functions are heavily used in attendance systems, booking applications, banking systems, and reporting platforms.
The NOW() function returns the current date and time of the database server.
SELECT NOW();
Sample Output:
2026-07-21 10:45:30
CURRENT_DATE returns the current system date.
SELECT CURRENT_DATE;
CURRENT_TIME returns the current system time.
SELECT CURRENT_TIME;
DATE_ADD() adds a specific interval to a date value.
SELECT DATE_ADD( '2026-07-21', INTERVAL 10 DAY );
The result will be a date 10 days later.
DATEDIFF() calculates the difference between two dates.
SELECT DATEDIFF( '2026-12-31', '2026-01-01' );
This query returns the number of days between the two dates.
Conversion functions change data from one type into another type.
These functions improve compatibility between different data formats.
CAST() converts a value into a specified data type.
CAST(expression AS datatype)
SELECT CAST( 125.75 AS INT );
Output:
125
CONVERT() is another conversion function available in many database systems.
SELECT CONVERT( VARCHAR, 125 );
The number is converted into text format.
NULL values represent missing or unknown information. SQL provides special functions to handle NULL values effectively.
COALESCE() returns the first non-NULL value from a list.
SELECT COALESCE( NULL, NULL, 'Available' );
Output:
Available
IFNULL() replaces NULL values with an alternative value.
SELECT IFNULL( PhoneNumber, 'Not Available' ) FROM Student;
Aggregate functions are often used with GROUP BY to summarize data.
SELECT Department, COUNT(*) FROM Student GROUP BY Department;
The query counts students in each department.
HAVING filters grouped data after aggregate calculations are performed.
SELECT Department, AVG(Marks) FROM Student GROUP BY Department HAVING AVG(Marks) > 75;
Only departments with average marks greater than 75 are displayed.
SELECT COUNT(AccountID), SUM(Balance), AVG(Balance) FROM Accounts;
Banks use aggregate functions to generate financial summaries.
SELECT COUNT(OrderID), SUM(TotalAmount) FROM Orders;
This query calculates total orders and revenue.
SELECT Department, AVG(Marks) FROM Student GROUP BY Department;
Universities use such reports to evaluate academic performance.
Functions are powerful but should be used carefully to maintain performance.
A predefined routine that performs a specific task and returns a value.
Single Row Functions and Aggregate Functions.
Counts rows or values.
Adds numeric values.
Calculates average values.
The highest value.
The smallest value.
Converts text to uppercase.
Converts text to lowercase.
The number of characters in a string.
Combines strings.
Removing extra spaces.
Extracts part of a string.
The absolute value of a number.
Rounding decimal values.
Returns the lower integer value.
Returns the higher integer value.
Returns the remainder after division.
Current date and time.
Current system date.
Current system time.
Adds an interval to a date.
Calculates difference between dates.
Converts one data type into another.
A data conversion function.
Returns the first non-NULL value.
Replaces NULL values.
Yes.
Yes.
They simplify data processing, calculations, and reporting.
SQL Functions are among the most powerful tools available in relational database systems. They help developers perform calculations, manipulate text, manage dates, handle NULL values, and generate meaningful reports without writing complicated logic.
By mastering Numeric Functions, String Functions, Aggregate Functions, Date Functions, Conversion Functions, and NULL Functions, students and developers can write efficient SQL queries and build professional database applications. SQL Functions are essential for database development, data analysis, reporting systems, and interview preparation.