SQL Tutorial



FUNCTIONS IN SQL


Functions in SQL

SQL functions are built-in operations used to perform calculations or manipulate data in queries. These functions simplify complex queries and make data processing efficient. SQL functions can be categorized mainly into aggregate functions and scalar functions.

Note:

Functions are used within the SELECT, WHERE, GROUP BY, and HAVING clauses to process and return data dynamically.

🔢 Aggregate Functions

  • COUNT() – Returns the number of rows.
  • SUM() – Returns the total sum of a numeric column.
  • AVG() – Returns the average value.
  • MIN() – Returns the minimum value.
  • MAX() – Returns the maximum value.

🧮 Scalar Functions

  • UCASE() / UPPER() – Converts text to uppercase.
  • LCASE() / LOWER() – Converts text to lowercase.
  • LEN() / LENGTH() – Returns the length of a string.
  • ROUND() – Rounds a numeric field to the number of decimals specified.
  • NOW() – Returns the current system date and time.

💡 Example Usage

Here is a simple example using aggregate functions:

SELECT COUNT(*) FROM Customers;

SELECT AVG(Price) FROM Products;

SELECT UPPER(Name) FROM Employees;
  

🎯 When to Use Functions

  • To simplify complex SQL logic.
  • To clean, format, or analyze data.
  • To perform calculations directly in the query.
  • To summarize data for reports and dashboards.

📌 Conclusion

Functions in SQL help developers and analysts process data effectively. Whether you're calculating totals, formatting names, or fetching system values like dates, SQL functions offer powerful tools for working with databases.

Quick Tip:

Always check the SQL dialect (MySQL, PostgreSQL, SQL Server, etc.) as function names and behavior can slightly vary across platforms.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review