RienSQL
LESSON 47 / 62 · Function usage

Function usage

Examples of scalar SQL functions and their applications on literals and field values from tables.

Using Functions

When creating SQL queries, we can use built-in functions. For example, if we want to output a string in uppercase, we can use the UPPER function.

SELECT UPPER('Hello world') AS upper_string;
upper_string
HELLO WORLD

What is a built-in function?

The built–in function is a piece of code implemented in the DBMS, with which you can perform transformations of string, numeric and other data in queries.

Each function accepts a set of arguments of a certain type, performs the operations inherent in it and necessarily returns one of the possible literals. It is worth noting that functions can take either zero arguments or several.

For example, the function NOW() takes zero arguments and returns a literal in date format, and LENGTH('sql-academy') takes one string argument and returns the numeric literal "11".

Examples of Functions

There are many functions, but the main ones can always be found using the search bar in the header or on the function reference page.

Here are some examples:

LOWER

Returns a string in which all characters are written in lowercase

SELECT LOWER('RianAcademy') AS lower_string;
lower_string
sql academy

YEAR

Returns the year for a given date.

SELECT YEAR('2022-06-16') AS year;
year
2022

INSTR

Searches for a substring in a string, returning the position of its first character. At the same time, the countdown starts with one, not zero, as in most programming languages.

The function works by character-by-character comparison of the source string with the desired one. For example, in the string sql-academy, the substring academy appears starting from the fifth character.

SELECT INSTR('sql-academy', 'academy') AS idx;
idx
5

LENGTH

Returns the length of the specified string.

SELECT LENGTH('sql-academy') AS str_length;
str_length
11

Applying functions over table field values

Functions can be used not only on literals, but also on values taken from a table. In this case, the function performs transformations for each row separately.

For example, let's go back to our database and look at the FamilyMembers table: it contains the name, status, and birthdate of people.

member_idINTgood_idINTgood_type_idINTpayment_idINT

SELECT member_name,
	LENGTH(member_name) AS fullname_length
FROM FamilyMembers;
member_namefullname_length
Headley Quincey15
Flavia Quincey14
Andie Quincey13
Lela Quincey12
Annie Quincey13
Ernest Forrest14
Constance Forrest17
Wednesday Addams16

Operations on the result of the function

Since we know that each function must return any of the possible literals, its result can also be used in further calculations and transformations.

For example, we want to get the first three letters in a string and convert them to uppercase. To do this, it will be enough for us to combine two functions: LEFT and UPPER, where the result of one function will be an argument for the second.

SELECT UPPER(LEFT('sql-academy', 3)) AS str;
str
SQL

Or we want to calculate the length of a person's last name by having a string in the format first namelast name. One of the possible ways to calculate the length of the last name can be using the functions LENGTH and position search, using the formula = - ( + ):

  • The value can be obtained using the LENGTH function
  • For + , you need to calculate the position of the character where the name ends and add one, because the space has a length of "1". We can do this using only the INSTR function, focusing on the "space" character

Since both functions return numeric literals, we can perform arithmetic operations on them. Let's subtract one from the other and get the length of the last name (lastname_length):

SELECT member_name,
	LENGTH(member_name) AS full_length,
	INSTR(member_name, ' ') AS firstname_with_space_length,
	LENGTH(member_name) - INSTR(member_name, ' ') AS lastname_length
FROM FamilyMembers;
member_namefull_lengthfirstname_with_space_lengthlastname_length
Headley Quincey1587
Flavia Quincey1477
Andie Quincey1367
Lela Quincey1257
Annie Quincey1367
Ernest Forrest1477
Constance Forrest17107
Wednesday Addams16106

Python Academy

Practice

Solve it in the editor. The query runs against a sample database in your browser.

Task 1Print string in lowercase
Print the text "Hello world" in lowercase using the corresponding function.
To print text, use the alias lower_string.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 2Print year of date
Print the full name of the family member and his year of birth.
Use the alias year_of_birth to display the year of birth.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.

Task 3Last name length calculation
Print the full name of the family member and the length of their last name.
Use the lastname_length alias to display the length of the last name.
SQL editor
SQLite
Ctrl / ⌘ + ↵
Result

Write a query and run it to see the results.