SQL Tutorial
SQL Clauses / Operators
SQL-Injection
SQL Functions
SQL Queries
PL/SQL
MySQL
SQL Server
Misc
In SQL, functions perform an action or a computation on values. These functions can be broadly categorized into two main types: Aggregate Functions and Scalar Functions.
Aggregate functions perform a calculation on a set of values and return a single value. They are often used in conjunction with the GROUP BY
clause.
Common Aggregate Functions:
COUNT(): Counts the number of rows in a set. If a column name is passed as an argument, it counts the number of non-NULL values in that column.
SELECT COUNT(column_name) FROM table_name;
SUM(): Adds up the values in a numeric column.
SELECT SUM(column_name) FROM table_name;
AVG(): Calculates the average value of a numeric column.
SELECT AVG(column_name) FROM table_name;
MIN(): Returns the smallest value in a column.
SELECT MIN(column_name) FROM table_name;
MAX(): Returns the largest value in a column.
SELECT MAX(column_name) FROM table_name;
Scalar functions operate on a single value and return a single value. Unlike aggregate functions, they don't work on a set of rows but rather on a single value.
Common Scalar Functions:
UCASE() or UPPER(): Converts a string to upper case.
SELECT UPPER(column_name) FROM table_name;
LCASE() or LOWER(): Converts a string to lower case.
SELECT LOWER(column_name) FROM table_name;
LENGTH(): Returns the length of a string.
SELECT LENGTH(column_name) FROM table_name;
SUBSTRING() or SUBSTR(): Extracts a portion of a string.
SELECT SUBSTRING(column_name, start_position, length) FROM table_name;
ROUND(): Rounds a numeric value to the number of decimals specified.
SELECT ROUND(column_name, number_of_decimals) FROM table_name;
NOW(): Returns the current date and time.
SELECT NOW();
DATEPART(), DAY(), MONTH(), YEAR(): Extracts parts of a date.
SELECT DATEPART(year, column_name) FROM table_name; -- Syntax might vary based on RDBMS.
It's important to note that the availability and exact syntax of these functions can vary depending on the specific relational database management system (RDBMS) you're using. Always refer to the documentation of the RDBMS you are working with for precise details and additional functions.
UPPER() Function:
SELECT UPPER('hello') AS result;
LOWER() Function:
SELECT LOWER('Hello') AS result;
LENGTH() or LEN() Function:
SELECT LENGTH('apple') AS result;
CONCAT() or || Operator:
SELECT CONCAT('John', ' ', 'Doe') AS full_name;
SUBSTRING() Function:
SELECT SUBSTRING('abcdef', 2, 3) AS result;
TRIM() Function:
SELECT TRIM(' Hello ') AS result;
REPLACE() Function:
SELECT REPLACE('Hello, world!', 'world', 'John') AS result;
CHAR_LENGTH() Function:
SELECT CHAR_LENGTH('apple') AS result;
LEFT() Function:
SELECT LEFT('apple', 3) AS result;
RIGHT() Function:
SELECT RIGHT('apple', 3) AS result;
SUM() Function:
SELECT SUM(salary) AS total_salary FROM employees;
AVG() Function:
SELECT AVG(salary) AS avg_salary FROM employees;
COUNT() Function:
SELECT COUNT(*) AS total_employees FROM employees;
MIN() Function:
SELECT MIN(salary) AS min_salary FROM employees;
MAX() Function:
SELECT MAX(salary) AS max_salary FROM employees;
SELECT department_id, GROUP_CONCAT(employee_name) AS employee_list FROM employees GROUP BY department_id;
SELECT UPPER(employee_name) AS upper_name FROM employees;
SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
SELECT UPPER(employee_name) AS upper_name FROM employees;
SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;
SELECT UPPER(product_name) AS upper_name FROM products;
SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id;
SELECT LEFT(employee_name, 3) AS initials FROM employees;