SQL Numeric Functions Tutorial šŸŽÆ

beginner
19 min

SQL Numeric Functions Tutorial šŸŽÆ

Welcome to our deep dive into SQL Numeric Functions! In this lesson, we'll explore a variety of functions that help manipulate numeric data in your SQL queries. Whether you're a beginner or an intermediate learner, this guide will provide you with a comprehensive understanding of these essential tools. Let's get started!

Understanding Numeric Functions šŸ“

Numeric functions in SQL are used to perform mathematical operations on numerical data. These functions can help you calculate averages, find maximum and minimum values, and perform other operations that are crucial for data analysis.

Essential Numeric Functions šŸ’”

1. AVG()

The AVG() function calculates the average value of a specified column.

sql
SELECT AVG(column_name) FROM table_name;

šŸ“ Note: Replace column_name with the name of the column you want to calculate the average of, and table_name with the name of your table.

2. COUNT()

The COUNT() function returns the number of rows that are not null in a specified column.

sql
SELECT COUNT(column_name) FROM table_name;

šŸ“ Note: Replace column_name with the name of the column you want to count, and table_name with the name of your table.

3. MAX() and MIN()

The MAX() function returns the highest value, and the MIN() function returns the lowest value in a specified column.

sql
SELECT MAX(column_name) FROM table_name; SELECT MIN(column_name) FROM table_name;

šŸ“ Note: Replace column_name with the name of the column you want to find the maximum or minimum value of, and table_name with the name of your table.

4. SUM()

The SUM() function adds up all the values in a specified column.

sql
SELECT SUM(column_name) FROM table_name;

šŸ“ Note: Replace column_name with the name of the column you want to sum, and table_name with the name of your table.

Practical Example šŸ’”

Let's consider a simple example: a table named sales with columns id, product, price, and quantity.

sql
CREATE TABLE sales ( id INT PRIMARY KEY, product VARCHAR(255), price DECIMAL(10, 2), quantity INT );

You can use the following queries to find the average price, total sales, and more:

sql
SELECT AVG(price) FROM sales; SELECT SUM(price * quantity) FROM sales; SELECT COUNT(id) FROM sales;

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

Which SQL function calculates the sum of all values in a specified column?

Remember, practice makes perfect! Continue experimenting with these functions to become comfortable with SQL Numeric Functions. Happy learning! šŸš€


This is just a snippet of the SQL Numeric Functions tutorial. Stay tuned for more comprehensive lessons on SQL, right here on CodeYourCraft! šŸ¤