SQL Tutorial: Database-Specific Functions 🎯

beginner
13 min

SQL Tutorial: Database-Specific Functions 🎯

Welcome to our comprehensive guide on SQL Database-Specific Functions! 🎉 In this tutorial, we'll explore various built-in functions that are indispensable for working with databases efficiently. Let's dive in!

What are Database-Specific Functions? 📝

Database-Specific Functions are predefined functions that are used to manipulate, extract, and analyze data in a database. They help us perform complex operations easily and provide more flexibility while querying data.

Why do we need Database-Specific Functions? 💡

These functions make it simpler to perform operations like calculating averages, finding maximum or minimum values, handling dates, and converting data types. They help us write more efficient and readable SQL queries.

String Functions 🎯

Let's start with some fundamental String Functions.

CONCAT()

Combines two or more strings.

Example:

sql
SELECT CONCAT('Hello', ' ', 'World'); -- Result: Hello World

LENGTH()

Returns the length of a string.

Example:

sql
SELECT LENGTH('Hello'); -- Result: 5

LOWER() and UPPER()

Converts all the characters in a string to lowercase or uppercase, respectively.

Example:

sql
SELECT LOWER('Hello'), UPPER('hello'); -- Result: hello, HELLO

LEFT() and RIGHT()

Returns the specified number of characters from the left or right side of a string.

Example:

sql
SELECT LEFT('Hello', 3), RIGHT('Hello', 3); -- Result: Hell, lo

Number Functions 🎯

ABS()

Returns the absolute value of a number.

Example:

sql
SELECT ABS(-5); -- Result: 5

SQRT()

Returns the square root of a number.

Example:

sql
SELECT SQRT(16); -- Result: 4

CEILING() and FLOOR()

Returns the smallest and largest integers greater than or equal to the given number, respectively.

Example:

sql
SELECT CEILING(3.14), FLOOR(3.14); -- Result: 4, 3

Date Functions 🎯

CURDATE()

Returns the current date.

Example:

sql
SELECT CURDATE(); -- Result: Your current date

DATE_FORMAT()

Formats a date according to the specified format.

Example:

sql
SELECT DATE_FORMAT(CURDATE(), '%d-%m-%Y'); -- Result: Your current date in DD-MM-YYYY format

Practice Time! 📝

Quiz: What function is used to convert all the characters in a string to lowercase?

A: CONCAT() B: LOWER() C: UPPER()

Correct: B Explanation: The LOWER() function converts all the characters in a string to lowercase.

Stay tuned for more SQL functions and their practical applications! 🎉💻