SQL Functions Reference 📚

beginner
24 min

SQL Functions Reference 📚

Welcome to our SQL Functions Reference guide! In this comprehensive tutorial, we'll explore various SQL functions that will help you manipulate and analyze data effectively. This guide is perfect for both beginners and intermediates. 🎯

What are SQL Functions? 📝

SQL functions are built-in functions used to perform specific operations on one or more columns of a table. They allow you to perform mathematical, string, date, and logical operations to extract meaningful insights from your data.

Basic SQL Functions 💡

1. Mathematical Functions

a) SUM()

sql
-- Example: Calculate the total sales for a specific period SELECT SUM(sales) FROM sales_table WHERE period = '2022-Q1';

b) AVG()

sql
-- Example: Calculate the average sales for a specific product SELECT AVG(sales) FROM sales_table WHERE product = 'Product A';

c) MIN() and MAX()

sql
-- Example: Find the minimum and maximum prices for a specific product SELECT MIN(price), MAX(price) FROM products WHERE product = 'Product A';

2. String Functions

a) CONCAT()

sql
-- Example: Concatenate first name and last name SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM customers;

b) LENGTH()

sql
-- Example: Find the length of a string SELECT LENGTH('Example String') AS string_length;

3. Date Functions

a) CURRENT_DATE

sql
-- Example: Get the current date SELECT CURRENT_DATE;

b) DATE_FORMAT()

sql
-- Example: Format a date in a specific format SELECT DATE_FORMAT(your_date_column, '%Y-%m-%d') FROM your_table;

Advanced SQL Functions 💡

1. Aggregate Functions

a) COUNT()

sql
-- Example: Count the number of rows in a table SELECT COUNT(*) FROM your_table;

b) GROUP BY

sql
-- Example: Group sales by product and calculate total sales for each product SELECT product, SUM(sales) FROM sales_table GROUP BY product;

2. Logical Functions

a) IF()

sql
-- Example: Check if a sale is greater than average sale and return 'High Sale' SELECT IF(sales > AVG(sales), 'High Sale', 'Normal Sale') AS sale_category FROM sales_table;

Quiz 🎯

Quick Quiz
Question 1 of 1

What does the `SUM()` function do in SQL?

By understanding and mastering these SQL functions, you'll be well-equipped to manage and analyze data effectively in your projects. Happy coding! 🚀