SQL Scalar Functions Tutorial 📝

beginner
25 min

SQL Scalar Functions Tutorial 📝

Welcome to our SQL Scalar Functions tutorial! In this lesson, we'll explore various built-in functions that return a single value, referred to as scalar functions. Let's dive in and make your SQL skills shine! 🚀

Introduction 🎯

Scalar functions in SQL help us perform calculations and manipulations on data without needing to use complex queries. They make our SQL queries more flexible, efficient, and powerful.

Throughout this tutorial, we'll learn about different types of scalar functions in SQL, understand their use cases, and see practical examples to help you grasp the concepts better.

Basic Scalar Functions 📝

1. CONCAT() 💡

The CONCAT() function is used to concatenate two or more strings together.

Example:

sql
SELECT CONCAT('Hello, ', 'World!');

Output:

Hello, World!

2. LENGTH() 💡

The LENGTH() function returns the length of a string in characters.

Example:

sql
SELECT LENGTH('Hello, World!');

Output:

13

3. LOWER() and UPPER() 💡

The LOWER() function converts a string to lowercase, while UPPER() converts it to uppercase.

Example:

sql
SELECT LOWER('HELLO, WORLD!'); SELECT UPPER('hello, world!');

Output:

hello, world! HELLO, WORLD!

Numeric Scalar Functions 📝

1. ABS() 💡

The ABS() function returns the absolute value of a number.

Example:

sql
SELECT ABS(-5);

Output:

5

2. ROUND() 💡

The ROUND() function rounds a number to a specified number of decimal places.

Example:

sql
SELECT ROUND(3.14159, 2);

Output:

3.14

Date and Time Scalar Functions 📝

1. CURDATE() 💡

The CURDATE() function returns the current date in the following format: YYYY-MM-DD.

Example:

sql
SELECT CURDATE();

Output:

2022-06-01

2. NOW() 💡

The NOW() function returns the current date and time in the following format: YYYY-MM-DD HH:MI:SS.

Example:

sql
SELECT NOW();

Output:

2022-06-01 10:30:45

Quiz 🎯

Quick Quiz
Question 1 of 1

Which function is used to concatenate two or more strings in SQL?

That's all for our SQL Scalar Functions tutorial! As you continue to learn and practice, remember to keep exploring new concepts and challenge yourself to create efficient and powerful SQL queries. Happy coding! 🎉