SQL Notes
Functions
Single Row functions Multi Row functions
➢ String Functions ➢ Aggregate Functions
➢ Math Functions
➢ Window Functions
➢ Date Functions
➢ Control Flow Functions
➢ Comparison 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
1. upper(): The upper() function is used to convert the lowercase
string or sequence of characters or character to uppercase.
Example:
select upper(“this is upper function”)
from dual;
Output:
-----------------------------------------------------------------------------
2. lower(): The lower() function is used to convert the uppercase
strong or sequence of character or character to lowercase.
Example:
select lower(“THIS IS LOWER FUNCTION”)
from dual;
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:
select length(“Tap Academy”)
from dual;
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:
Note: Position in MySQL starts from 1, it means from which position we
want to fetch the data from the specified string.
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()
1. mod(): The mod() function is used to get the remainder value
after performing the division.
Syntax:
mod(dividend, divisor)
Example:
select mod(125,3)
from dual;
Output:
16
SQL Notes
2. round(): The round() function is used to round up the values at
the specified position, here position can be positive or negative. After
the decimal point we need to consider the positive positions, before
the decimal point we need to consider the negative positions as show
below.
-4 -3 -2 -1 1 2 3
1 2 3 4.6 7 8
Let’s take one example as show below:
select round(1234.567,2)
from dual;
In the above query they have mentioned as we need to round up the
valued at the 2nd position. It’s a positive value hence we need to check
after the decimal point.
-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:
Let’s take one example for negative position as show
below:
select round(1234.567,-3)
from dual;
In the above query they have mentioned as we need to round up the
valued at the 3rd position. It’s a negative value hence we need to
check before the decimal point.
-4 -3 -2 -1 1 2 3
1 2 3 4 .6 7 8
We have rounded the value at the 3rd 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:
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:
3. truncate(): The truncate() function is used to truncate/remove
the values at the specified position, here position can be positive or
negative. After the decimal point we need to consider the positive
positions, before the decimal point we need to consider the negative
positions as show below.
-4 -3 -2 -1 1 2 3
1 2 3 4.6 7 8
19
SQL Notes
Let’s take one example as show below:
select truncate(1234.678,2)
from dual;
In the above query they have mentioned as we need to truncate the
valued at the 2nd position. It’s a positive value hence we need to check
after the decimal point.
-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:
Let’s take one example for negative position as show
below:
select truncate(1534.567,-3)
from dual;
20
SQL Notes
In the above query they have mentioned as we need to truncate the
valued at the 3rd position. It’s a negative value hence we need to
check before the decimal point.
-4 -3 -2 -1 1 2 3
1 5 3 4 .6 7 8
We have truncated the value at the 3rd position, once we have
truncated, it will directly take the value and it will display it will not
check for the right and the left number and it will not increment the
values, but after truncating the remaining each number will be
converted as zero(0) and after decimal point the values will be
ignored same as round() function as show below:
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
Types of Date Functions
1. curdate() 11. Date_format()
2. now()
3. day()
4. month()
5. year()
6. datediff()
7. date_add()
8. last_day()
9. timestampdiff()
10. extract()
1. curdate(): The curdate() function is used to display the current
date that is present in your system.
Example:
select curdate();
Output
Note: Whenever we are using any inbuild function in date functions
we no need to specify any table name in MySQL directly we can use the
functions as shown above.
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
8. date_add: The date_add() function will add the seconds to
the given date.
Example:
select date_add(“2023-12-31 23:59:59”, interval 1 second);
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
10. timestampdiff(): The timestampdiff() function is used
to find the month,day,hours,minutes etc difference from the specified
dates.
Example1:
select timestampdiff(day,”2000-12-01” ,“2023-12-01”);
Output
Example2:
select timestampdiff(hour,”2000-12-01” ,“2023-12-01”);
Output
29
SQL Notes
Example3:
select timestampdiff(minute,”2000-12-01” ,“2023-12-01”);
Output
Example4:
select timestampdiff(month,”2000-12-01” ,“2023-12-01”);
Output
Example5:
select timestampdiff(year,”2000-12-01” ,“2023-12-01”);
Output
30
SQL Notes
11. extract(): This function is used to extract the seconds,
minutes,hours etc from the specified single date.
Example1:
select extract(second from “2000-12-01 12:40:39” );
Output
Example2:
select extract(minute from “2000-12-01 12:40:39” );
Output
31
SQL Notes
Example3:
select extract(hour from “2000-12-01 12:40:39” );
Output
-----------------------------------------------------------------------------
12. date_format(): This function is used to display the date
in the specified format or way.
Consider the below table to understand the
formats:
Specifier Meaning
%a Three-characters abbreviated weekday name e.g., Mon, Tue, Wed, etc.
%b Three-characters abbreviated month name e.g., Jan, Feb, Mar, etc.
%c Month in numeric e.g., 1, 2, 3…12
%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
%e Day of the month without leading zero e.g., 1,2,…31
%f Microseconds in the range of 000000..999999
%H Hour in 24-hour format with leading zero e.g., 00..23
%h Hour in 12-hour format with leading zero e.g., 01, 02…12
%I Same as %h
%i Minutes with leading zero e.g., 00, 01,…59
%j Day of year with leading zero e.g., 001,002,…366
%k Hour in 24-hour format without leading zero e.g., 0,1,2…23
%l Hour in 12-hour format without leading zero e.g., 1,2…12
%M Full month name e.g., January, February,…December
%m Month name with leading zero e.g., 00,01,02,…12
%p AM or PM, depending on other time specifiers
%r Time in 12-hour format hh:mm:ss AM or PM
%S Seconds with leading zero 00,01,…59
%s Same as %S
%T Time in 24-hour format hh:mm:ss
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
%V Same as %U; it is used with %X
%v Same as %u; it is used with %x
%W Full name of weekday e.g., Sunday, Monday,…, Saturday
%w Weekday in number (0=Sunday, 1= Monday,etc.)
%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
%Y Four digits year e.g., 2000 and 2001.
%y Two digits year e.g., 10,11,and 12.
%% Add percentage (%) character to the output
34
SQL Notes
Consider the below table to understand the
combination of formats:
DATE_FORMAT string Formatted date
%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
%e/%c/%Y %H:%i 4/7/2013 11:20
%c/%e/%Y %H:%i 7/4/2013 11:20
%d/%m/%Y %H:%i 4/7/2013 11:20
%m/%d/%Y %H:%i 7/4/2013 11:20
%e/%c/%Y %T 4/7/2013 11:20
%c/%e/%Y %T 7/4/2013 11:20
%d/%m/%Y %T 4/7/2013 11:20
%m/%d/%Y %T 7/4/2013 11:20
%a %D %b %Y Thu 4th Jul 2013
%a %D %b %Y %H:%i Thu 4th Jul 2013 11:20
35
SQL Notes
DATE_FORMAT string Formatted date
%a %D %b %Y %T Thu 4th Jul 2013 11:20:05
%a %b %e %Y Thu Jul 4 2013
%a %b %e %Y %H:%i Thu Jul 4 2013 11:20
%a %b %e %Y %T Thu Jul 4 2013 11:20:05
%W %D %M %Y Thursday 4th July 2013
%W %D %M %Y %H:%i Thursday 4th July 2013 11:20
%W %D %M %Y %T Thursday 4th July 2013 11:20:05
%l:%i %p %b %e, %Y 7/4/2013 11:20
%M %e, %Y 4-Jul-13
%a, %d %b %Y %T Thu, 04 Jul 2013 11:20:05
Example:
select date_format(“2023-09-19”,” %W %D %M %Y”);
Output
36
SQL Notes
Comparison Functions
Types of Comparison Functions
1. isnull()
2. coalesce()
1. isnull(): This function take only one argument/parameter, if the
argument is null it will return 1, else it will return 0.
Example1:
select isnull(null);
Output
Example2:
select isnull(1);
Output
37
SQL Notes
2. coalesce(): This function takes two arguments, here if first
argument is null, it will replace null with the second argument value
and it will display that, if first argument is not null it will display the
first argument only.
Example1:
select coalesce(null,1);
Output
Example2:
select coalesce(2,1);
Output
38
SQL Notes
Control Flow Functions
Types of Control Flow Functions
1. case
2. if
3. ifnull
4. nullif
1. case: MySQL case expression is a control flow structure that allows
you to add if-else logic to a query.
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
Note: In the above query we are checking multiple conditions and
for each condition we are fetching the separate data. So here if the
employee first name is William it will fetch the 10% of the salary of
William, if the employee first_name is Aaron it will fetch the 15% of
the salary of Aaron, from the remaining employees it will fetch 20% of
their salary. Hence whenever, we want to check multiple condition
based on the conditions if we want to fetch separate data then we can
use CASE function.
-----------------------------------------------------------------------------
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
3. ifnull: MySQL ifnull function is one of the MySQL control flow
functions that accepts two arguments and returns the first argument
if it is not null. Otherwise, the ifnull function returns the second
argument.
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
Types of Window Functions
1. rank()
2. dense_rank()
3. row_number()
1. rank(): It will assign the rank to the values in the specified
column, but if same value is present the rank value can be skipped if
we are using rank() function to give the rank.
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
2. dense_rank(): Assigns a rank to every row within its partition
based on the ORDER BY clause. It assigns the same rank to the rows
with equal values. If two or more rows have the same rank, then there
will be no gaps in the sequence of ranked values.
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