Microsoft SQL Server Certification Course
- 5k Enrolled Learners
- Weekend
- Live Class
Structured Query Language aka SQL is used to handle data in databases. It provides various in-built functions and commands to access and manage databases according to our requirements. In this article on SQL Functions, I will discuss the various in-built functions to perform different types of calculation on the data.
The following topics will be covered in this article:
Before we delve into the different types of functions offered by SQL, let us understand what are functions.
Functions are methods used to perform data operations. SQL has many in-built functions used to perform string concatenations, mathematical calculations etc.
SQL functions are categorized into the following two categories:
Let us look into each one of them, one by one.
The Aggregate Functions in SQL perform calculations on a group of values and then return a single value. Following are a few of the most commonly used Aggregate Functions:
Function | Description |
SUM() | Used to return the sum of a group of values. |
COUNT() | Returns the number of rows either based on a condition, or without a condition. |
AVG() | Used to calculate the average value of a numeric column. |
MIN() | This function returns the minimum value of a column. |
MAX() | Returns a maximum value of a column. |
FIRST() | Used to return the first value of the column. |
LAST() | This function returns the last value of the column. |
Let us look into each one of the above functions in depth. For your better understanding, I will be considering the following table to explain to you all the examples.
StudentID | StudentName | Marks |
1 | Sanjay | 64 |
2 | Varun | 72 |
3 | Akash | 45 |
4 | Rohit | 86 |
5 | Anjali | 92 |
Used to return a total sum of numeric column which you choose.
SELECT SUM(ColumnName) FROM TableName;
Write a query to retrieve the sum of marks of all students from the Students table.
SELECT SUM(Marks) FROM Students;
359
Returns the number of rows present in the table either based on some condition or without any condition.
SELECT COUNT(ColumnName) FROM TableName WHERE Condition;
Write a query to count the number of students from the Students table.
SELECT COUNT(StudentID) FROM Students;
5
Write a query to count the number of students scoring marks > 75 from the Students table.
SELECT COUNT(StudentID) FROM Students WHERE Marks >75;
Output:
2
This function is used to return the average value of a numeric column.
SELECT AVG(ColumnName) FROM TableName;
Write a query to calculate the average marks of all students from the Students table.
SELECT AVG(Marks) FROM Students;
71.8
Used to return the minimum value of a numeric column.
SELECT MIN(ColumnName) FROM TableName;
Example:
Write a query to retrieve the minimum marks out of all students from the Students table.
SELECT MIN(Marks) FROM Students;
45
Returns the maximum value of a numeric column.
SELECT MAX(ColumnName) FROM TableName;
Write a query to retrieve the maximum marks out of all students from the Students table.
SELECT MAX(Marks) FROM Students;
92
This function returns the first value of the column which you choose.
SELECT FIRST(ColumnName) FROM TableName;
Write a query to retrieve the marks of the first student.
SELECT FIRST(Marks) FROM Students;
64
Used to return the last value of the column which you choose.
SELECT LAST(ColumnName) FROM TableName;
Write a query to retrieve the marks of the last student.
SELECT LAST(Marks) FROM Students;
Well, with that we come to an end to SQL Aggregate Functions. Next in this article on SQL Functions, let us understand the various Scalar Functions.
The Scalar Functions in SQL are used to return a single value from the given input value. Following are a few of the most commonly used Aggregate Functions:
Let us look into each one of the above functions in depth.
Function | Description |
LCASE() | Used to convert string column values to lowercase |
UCASE() | This function is used to convert a string column values to Uppercase. |
LEN() | Returns the length of the text values in the column. |
MID() | Extracts substrings in SQL from column values having String data type. |
ROUND() | Rounds off a numeric value to the nearest integer. |
NOW() | This function is used to return the current system date and time. |
FORMAT() | Used to format how a field must be displayed. |
Used to convert values of a string column to lowercase characters.
SELECT LCASE(ColumnName) FROM TableName;
Write a query to retrieve the names of all students in lowercase.
SELECT LCASE(StudentName) FROM Students;
sanjay varun akash rohit anjali
Used to convert values of a string column to uppercase characters.
SELECT UCASE(ColumnName) FROM TableName;
Example:
Write a query to retrieve the names of all students in lowercase.
SELECT UCASE(StudentName) FROM Students;
SANJAY VARUN AKASH ROHIT ANJALI
Used to retrieve the length of the input string.
SELECT LENGTH(String) AS SampleColumn;
Write a query to extract the length of the student name “Sanjay”.
SELECT LENGTH(“Sanjay”) AS StudentNameLen;
6
This function is used to extract substrings from columns having string data type.
SELECT MID(ColumnName, Start, Length) FROM TableName;
Write a query to extract substrings from the StudentName column.
SELECT MID(StudentName, 2, 3) FROM Students;
anj aru kas ohi nja
This function is used to round off a numeric value to the nearest integer.
SELECT ROUND(ColumnName, Decimals) FROM TableName;
For this example, let us consider the following Marks table in the Students table.
StudentID | StudentName | Marks |
1 | Sanjay | 90.76 |
2 | Varun | 80.45 |
3 | Akash | 54.32 |
4 | Rohit | 72.89 |
5 | Anjali | 67.66 |
Write a query to round the marks to the integer value.
SELECT ROUND(Marks) FROM Students;
91 80 54 73 68
Used to return the current date and time. The date and time are returned in the “YYYY-MM-DD HH-MM-SS” format.
SELECT NOW();
Example:
Write a query to retrieve the current date and time.
SELECT NOW();
Output:
NOW() |
2019-10-14 09:16:36 |
This function formats the way a field must be displayed.
FORMAT(InputValue, Format)
Example:
Write a query to display the numbers “123456789” in the format “###-###-###”
SELECT FORMAT(123456789, “###-###-###”);
Output:
123-456-789
With this, we come to an end to this article on SQL Functions. I hope you understood how to use the various types of functions in SQL. If you wish to learn more about MySQL and get to know this open-source relational database, then check out our MySQL DBA Certification Training which comes with instructor-led live training and real-life project experience. This training will help you understand MySQL in-depth and help you achieve mastery over the subject.
If you wish to learn MongoDB and build a career in the Database User Roles and Database Administration Roles, then check out our interactive, live-online MongoDB Online Course here, which comes with 24*7 support to guide you throughout your learning period.
Got a question for us? Please mention it in the comments section of ”SQL Functions” and I will get back to you.
Course Name | Date | Details |
---|---|---|
Microsoft SQL Server Certification Course | Class Starts on 7th December,2024 7th December SAT&SUN (Weekend Batch) | View Details |
edureka.co