SQL Single Row Functions
SQL Single Row Functions
Functions
Single Row functions Multi Row functions
Note: Single row functions are such functions which will return the
result for each and every input/record. Multi row functions are such
functions which will return the single output by combining all the given
inputs/records.
String Functions
Types of String Functions
1. upper()
2. lower()
3. concat()
4. length()
5. substr()
6. instr()
7. replace()
8. trim()
9
SQL Notes
Output:
-----------------------------------------------------------------------------
2. lower(): The lower() function is used to convert the uppercase
strong or sequence of character or character to lowercase.
Example:
Output:
10
SQL Notes
Note: Here dual table is a default table which will be present in MySQL,
no need to create this table you can just use this table to perform operations
on any raw data.
-----------------------------------------------------------------------------
3. concat(): This concat() function is used to combine or join
multiple strings or sequence of characters or characters as one.
Syntax:
Concat(“value1”,”value2”,….);
Example:
select concat(“Tap”,”Academy”)
from dual;
Output:
-----------------------------------------------------------------------------
4.length(): The length() function is used to fetch the length of the
specified string or sequence of characters.
Example:
11
SQL Notes
Output:
-----------------------------------------------------------------------------
5. substr(): The substr() function is used to fetch the part of the
string.
Syntax1:
Substr(String, position);
Example:
select substr(“TapAcademy”,4)
from dual;
Output:
Syntax2:
substr(String, position, number of characters);
Example:
select substr(“TapAcademy”,4,4)
from dual;
12
SQL Notes
Output:
Note: Number of characters means from the specified position how many
characters we need to fetch.
-----------------------------------------------------------------------------
6.instr(): The instr() function is used to fetch the position of the
character from the specified string.
Syntax:
instr(String, character);
Example:
select instr(“TapAcademy”,”p”)
from dual;
Output:
-----------------------------------------------------------------------------
7. replace(): The replace() function is used to replace the data from
the specified data.
Syntax:
replace(string, old_string, new_string)
Example:
select replace(“tap Academy”,”t”,”T”)
from dual;
13
SQL Notes
Output:
-----------------------------------------------------------------------------
8. trim(): The trim() function is used to truncate/remove/trim the
data from the specified data. We need to use the leading keyword to
trim the data from the beginning. We need to use trailing keyword
to trim the data from the ending.
Example1:
select trim(leading “m” from “madam”)
from dual;
Output:
Example2:
select trim(trailing “m” from “madam”)
from dual;
Output:
14
SQL Notes
Note: We can use ltrim() function to remove the spaces from the left side
of the string. We can use rtrim() function to remove the spaces from the
right side of the string as show below:
Example1:
select ltrim(“ TapAcademy”)
from dual;
Output:
Example2:
select rtrim(“ TapAcademy ”)
from dual;
Output:
-----------------------------------------------------------------------------
15
SQL Notes
Math Functions
Types of Math Functions
1. mod()
2. round()
3. truncate()
4. ceil()
5. floor()
Example:
select mod(125,3)
from dual;
Output:
16
SQL Notes
-4 -3 -2 -1 1 2 3
1 2 3 4.6 7 8
select round(1234.567,2)
from dual;
-4 -3 -2 -1 1 2 3
1 2 3 4 .6 7 8
We have rounded the value at the 2nd position, once we have rounded
we need to check whether the right number is greater than or equal
to(>=) 5 or not as show below:
-4 -3 -2 -1 1 2 3
1 2 3 4 .6 7 8
L R
17
SQL Notes
If the right number is greater than or equal to 5 then the left number
should be incremented by 1, in the above example right number is
>=5 hence the left number will be incremented by 1 and it will
become as 8 and it will return the result as 1234.68 as how below:
select round(1234.567,-3)
from dual;
-4 -3 -2 -1 1 2 3
1 2 3 4 .6 7 8
18
SQL Notes
-4 -3 -2 -1 1 2 3
1 2 3 4 .6 7 8
L R
If the right number is greater than or equal to 5 then the left number
should be incremented by 1, in the above example right number is not
>=5 hence the left number so it will not get incremented by 1 and it
will be as 1 only, and after the left number how many numbers are
present before the decimal point that number each will be converted
into zero(0) and after the decimal point values will be ignored in the
result as show below:
-4 -3 -2 -1 1 2 3
1 2 3 4.6 7 8
19
SQL Notes
select truncate(1234.678,2)
from dual;
-4 -3 -2 -1 1 2 3
1 2 3 4 .6 7 8
Once we have truncated the value it will display the value directly,
truncate will not check the right and left number and incrementation
also will not happen in truncate as show below:
select truncate(1534.567,-3)
from dual;
20
SQL Notes
-4 -3 -2 -1 1 2 3
1 5 3 4 .6 7 8
4.ceil(): The ceil() function will return the integer value that is
greater or equal too for the specified value as show below:
Example1:
select ceil(12)
from dual;
21
SQL Notes
Output
Example2:
select ceil(12.01)
from dual;
Output
In the above we are using ceil() function so it will always give the
integer value that is equal or greater than the specified values hence
here we have 12.01 so it’s rounding up the value and it is returning as
13.
5. floor(): The floor() function will return the integer value that is
less than or equal too for the specified value as show below:
Example1:
select floor(12)
from dual;
Output
22
SQL Notes
Example2:
select floor(12.10)
from dual;
Output
In the above we are using floor() function so it will always give the
integer value that is equal or less than the specified values hence here
we have 12.10 so it’s rounding up the value that is equal and it is
returning as 12.
23
SQL Notes
Date Functions
Example:
select curdate();
Output
24
SQL Notes
2. now(): The now() function will return the current date and time of
the system.
Example:
select now();
Output
Note: This current date and time will be changed according the day
you’re executing this query, it won’t be constant as shown above.
-----------------------------------------------------------------------------
3. now(): The now() function will return the current date and time of
the system.
Example:
select now();
Output
25
SQL Notes
4. day(): The day() function will return the day from the specified
date.
Example:
select day(“2023-09-19”);
Output
Note: Give the same date format while executing the query,
otherwise in MySQL it will not return result, instead it will return as
NULL.
-----------------------------------------------------------------------------
5. month(): The month() function will return the month from the
specified date.
Example:
select month(“2023-09-19”);
Output
26
SQL Notes
6.year(): The year() function will return the year from the specified
date.
Example:
select year(“2023-09-19”);
Output
-----------------------------------------------------------------------------
7. datediff(): The datediff() function will return the difference
between two specified dates in term of days.
Example:
select datediff(“2023-09-19”,”2000-09-19”);
Output
27
SQL Notes
Example:
Output
-----------------------------------------------------------------------------
9. last_day(): The last_day() function will return the last day of
given date.
Example:
select last_day(“2023-12-01”);
Output
28
SQL Notes
Example1:
Output
Example2:
Output
29
SQL Notes
Example3:
Output
Example4:
Output
Example5:
Output
30
SQL Notes
Example1:
Output
Example2:
Output
31
SQL Notes
Example3:
Output
-----------------------------------------------------------------------------
12. date_format(): This function is used to display the date
in the specified format or way.
Specifier Meaning
%D Day of the month with English suffix e.g., 0th, 1st, 2nd, etc.
%d Day of the month with leading zero if it is 1 number e.g., 00, 01,02, …31
32
SQL Notes
Specifier Meaning
%I Same as %h
%s Same as %S
33
SQL Notes
Specifier Meaning
%U Week number with leading zero when the first day of week is Sunday e.g.,
00,01,02…53
%u Week number with leading zero when the first day of week is Monday e.g.,
00,01,02…53
%X Year for the week in four digits where the first day of the week is Sunday; often used
with %V
%x Year for the week, where the first day of the week is Monday, four digits; used with
%v
34
SQL Notes
%Y-%m-%d 2013-07-04
%e/%c/%Y 4/7/2013
%c/%e/%Y 7/4/2013
%d/%m/%Y 4/7/2013
%m/%d/%Y 7/4/2013
35
SQL Notes
%M %e, %Y 4-Jul-13
Example:
Output
36
SQL Notes
Comparison Functions
Types of Comparison Functions
1. isnull()
2. coalesce()
Example1:
select isnull(null);
Output
Example2:
select isnull(1);
Output
37
SQL Notes
Example1:
select coalesce(null,1);
Output
Example2:
select coalesce(2,1);
Output
38
SQL Notes
Syntax
CASE value
WHEN value1 THEN result1
WHEN value2 THEN result2
…
[ELSE else_result]
END
Example
select salary,
case
when e_fname=”William” then salary*10/100
when e_fname=”Aaron” then salary*15/100
else salary*20/100
end
from employee;
39
SQL Notes
-----------------------------------------------------------------------------
2. if: MySQL IF function is one of the MySQL control flow functions that
returns a value based on a condition.
Syntax
IF(expr,if_true_expr,if_false_expr)
Example
select if(5=5,'true','false');
Output
Note: In the above query we are checking the condition and using IF,
i.e., if 5=5 then it will return true else it will return false.
40
SQL Notes
Syntax
IFNULL(expression_1,expression_2);
Example
select ifnull(null,0) ;
Output
-----------------------------------------------------------------------------
4.nullif: The nullif function is one of the control flow functions that
accepts 2 arguments. The nullif function returns null if the first
argument is equal to the second argument, otherwise it returns the
first argument.
Syntax
NULLIF(expression_1,expression_2);
Example
select nullif(5,5);
41
SQL Notes
Output
-----------------------------------------------------------------------------
Window Functions
MySQL has supported window functions since version 8.0. The window
functions allow you to solve query problems in new, easier ways and with
better performance.
Syntax:
window_function_name(expression) OVER (
[partition_defintion]
[order_definition]
[frame_definition]
)
Note:
• First, specify the window function name followed by an expression.
• Second, specify the OVER clause which has three possible elements:
partition definition, order definition, and frame definition.
• The opening and closing parentheses after the OVER clause are
mandatory, even with no expression.
• The partition_clause breaks up the rows into chunks or partitions.
• The ORDER BY clause specifies how the rows are ordered within a
partition.
• A frame clause is a subset of the current partition.
42
SQL Notes
Syntax:
select salary, rank()
over(order by salary desc)
from employee;
Output:
Note: In the above example we are sort the salary in descending order
and we are giving the ranks for each value, here you can see that 4th rank
has been skipped, so if we are using the rank() function we can give the
ranks to the values but there will be a chance that ranks can be skipped as
shown above.
43
SQL Notes
Syntax:
select salary, dense_rank()
over(order by salary desc)
from employee;
Output:
44
SQL Notes
3. row_number():
MySQL introduce the row_number() function since version 8.0.
The row_number() is a window function that assigns a sequential
number to each row in the result set. The first number begins with
one.
Syntax:
select salary, dense_rank()
over(order by salary desc)
from employee;
Output:
45