SQL (Structured Query Language) functions are essential for manipulating and retrieving data from databases. In this guide, we'll cover some of the most common SQL functions. If you're looking to dive deeper into SQL, don't forget to check out our SQL Tutorial.
Common SQL Functions
Here are some of the most frequently used SQL functions:
COUNT()
: Counts the number of rows.SELECT COUNT(*) FROM customers;
SUM()
: Calculates the total sum of a numeric column.SELECT SUM(amount) FROM sales;
AVG()
: Computes the average value of a numeric column.SELECT AVG(price) FROM products;
MAX()
: Finds the maximum value in a column.SELECT MAX(age) FROM users;
MIN()
: Finds the minimum value in a column.SELECT MIN(age) FROM users;
String Functions
String functions are used to manipulate text data in SQL. Here are a few common string functions:
UPPER()
: Converts a string to uppercase.SELECT UPPER(name) FROM users;
LOWER()
: Converts a string to lowercase.SELECT LOWER(name) FROM users;
LENGTH()
: Returns the length of a string.SELECT LENGTH(name) FROM users;
CONCAT()
: Concatenates two or more strings.SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;
Date Functions
Date functions are used to manipulate and retrieve information from date and time columns in SQL.
CURRENT_DATE()
: Returns the current date.SELECT CURRENT_DATE();
EXTRACT()
: Extracts a specific part of a date or time.SELECT EXTRACT(YEAR FROM birthdate) FROM users;
DATEDIFF()
: Calculates the difference between two dates.SELECT DATEDIFF(birthdate, '1990-01-01') FROM users;
Images
Here's a picture of a SQL database to illustrate the concept:
For more information on SQL functions and how to use them effectively, visit our SQL Functions Reference.