Table of contents
SQL functions are built-in operations that are used to perform various tasks on the data in a database. In this blog, we will discuss commonly used SQL functions and their syntax.
- COUNT Function The COUNT function is used to count the number of rows in a table that matches a certain condition.
Syntax: SELECT COUNT(column_name) FROM table_name WHERE condition;
Example: SELECT COUNT(*) FROM customers WHERE age > 18;
- SUM Function The SUM function is used to calculate the sum of values in a column.
Syntax: SELECT SUM(column_name) FROM table_name WHERE condition;
Example: SELECT SUM(salary) FROM employees WHERE department = 'Sales';
- AVG Function The AVG function is used to calculate the average value of a column.
Syntax: SELECT AVG(column_name) FROM table_name WHERE condition;
Example: SELECT AVG(age) FROM customers WHERE gender = 'Male';
- MAX Function The MAX function is used to retrieve the maximum value from a column.
Syntax: SELECT MAX(column_name) FROM table_name WHERE condition;
Example: SELECT MAX(salary) FROM employees WHERE department = 'Finance';
- MIN Function The MIN function is used to retrieve the minimum value from a column.
Syntax: SELECT MIN(column_name) FROM table_name WHERE condition;
Example: SELECT MIN(age) FROM customers WHERE gender = 'Female';
- CONCAT Function The CONCAT function is used to concatenate two or more strings.
Syntax: SELECT CONCAT(string1, string2, ...) FROM table_name WHERE condition;
Example: SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
- DATE Functions several SQL functions are used to manipulate date values. Some of the commonly used functions include:
DATE: Returns the date portion of a datetime value.
YEAR: Returns the year portion of a date value.
MONTH: Returns the month portion of a date value.
DAY: Returns the day portion of a date value.
Syntax: SELECT DATE(date_column), YEAR(date_column), MONTH(date_column), DAY(date_column) FROM table_name WHERE condition;
Example: SELECT DATE(joining_date), YEAR(joining_date), MONTH(joining_date), DAY(joining_date) FROM employees;
Conclusion
SQL functions are powerful tools that are used to perform various tasks on the data in a database. By using functions such as COUNT, SUM, AVG, MAX, and MIN, you can retrieve and manipulate data in a variety of ways. Additionally, functions such as CONCAT and DATE can be used to manipulate strings and date values, respectively. By understanding the syntax and functionality of SQL functions, you can improve the efficiency and effectiveness of your SQL queries.