SQL functions are essential tools for manipulating and analyzing data in databases. They allow you to perform calculations, transformations, and operations on data rows or sets. Let's explore key categories and examples:
1. Types of SQL Functions
- Aggregate Functions (🧮)
AVG()
,SUM()
,COUNT()
,MAX()
,MIN()
- Used to perform calculations on a set of values.
- Example:
SELECT COUNT(*) FROM users;
- String Functions (📖)
CONCAT()
,UPPER()
,LOWER()
,TRIM()
,SUBSTRING()
- Manipulate text data.
- Example:
SELECT UPPER(name) FROM employees;
- Date Functions (📅)
NOW()
,DATEDIFF()
,DATE_ADD()
,EXTRACT()
- Handle date and time operations.
- Example:
SELECT DATE_ADD(hire_date, INTERVAL 1 YEAR) FROM hires;
- Mathematical Functions (🧮)
ROUND()
,ABS()
,CEIL()
,FLOOR()
- Perform arithmetic operations.
- Example:
SELECT ROUND(salary, 2) FROM salaries;
2. Practical Use Cases
- Data Transformation: Use
CONCAT()
to merge first and last names. - Filtering Results: Combine
WHERE
withBETWEEN
orLIKE
for precise queries. - Time Series Analysis: Calculate time intervals with
DATEDIFF()
orTIMESTAMPDIFF()
.
3. Expand Your Knowledge
For a deeper dive into SQL fundamentals, check out our SQL Tutorial Overview.
Remember to use CASE
, IF
, or COALESCE
for conditional logic! 🧠
Explore more advanced topics like window functions or JSON operations here. 🚀