Notes Unit-04
Notes Unit-04
FUNCTION in MySQL
❖ Function:
A function is a predefined command set that performs some operation and
returns the single value. A function can have single, multiple or no arguments
at all.
42 | P a g e
1) Character / String Functions:
i. CONCAT()
ii. LOWER() / LCASE()
iii. UPPER()/UCASE()
iv. LTRIM()
v. TRIM()
vi. RTRIM()
vii. SUBSTR()/MID()
viii. INSTR(),
ix. LENGTH()
x. RIGHT()
xi. LEFT()
3) Date Functions:
i. SYSDATE()
ii. NOW()
iii. DATE()
iv. MONTH()
v. YEAR()
vi. DAYNAME()
vii. MONTHNAME()
viii. DAY()
43 | P a g e
Math Functions:
1.Pow(x,y )/power(x,y): Returns the value of X raised to the power
of Y.
Example:
(i)Select POW(2,4); Result:16
(ii)SELECT POW(2,-2; Result:0.25
(iii)SELECT POW(-2,3); Result: -8
(iv)SELECT id, salary, POWER(salary,2) FROM employee;
Result:
+----+----------+-----------------+
| id | salary | power(salary,2) |
+----+----------+-----------------+
| 1 | 25000.00 | 625000000 |
| 2 | 30000.00 | 900000000 |
| 3 | 32000.50 | 1024032000.25 |
| 4 | 37500.50 | 1406287500.25 |
| 5 | 42389.50 | 1796869710.25 |
+----+----------+-----------------+
2.ROUND(X): Rounds the argument to zero decimal place, whereas
ROUND(X, d) rounds X to d decimal places.
Example:
(i) ROUND(-1.23); Result: -1
(ii) ROUND(-1.68); Result: -2
(iii) ROUND(1.58); Result: 2
(iv) ROUND(3.798, 1); Result: 3.8
(v) ROUND(1.298, 0); Result: 1
(vi) ROUND(76823.298, -1); Result: 76820
(vii) ROUND( 25.298,-1); Result: 30
(viii) ROUND(3.798, 1); Result: 3.8
(ix) ROUND(4536.78965,-3) Result: 5000
(X) ROUND(4536.564553,-2): Result: 4500
44 | P a g e
(XI) ROUND(4586.564553,-2): Result: 4600
(XII)ROUND(76823.298, -2); Result:76800
XII)ROUND(76823.298, 2); Result: 76823.30
(XIII) ROUND(3.798, 2); Result: 3.80
+--------------------+
| LENGTH(First_Name) |
+--------------------+
|4|
|7|
|8|
|5|
|6|
+--------------------+
5 rows in set (0.00 sec)
45 | P a g e
(ii) SELECT INSTR ('Computers', 'pet');
Result: 0
(iii) mysql> SELECT INSTR (First_Name,'Kiran') FROM Employee;
Result:
+---------------------------+
| INSTR(First_Name,'Kiran') |
+---------------------------+
|0|
|0|
| 4 | Select instr(“good morning to all”,”or”)
|0|
|0|
+---------------------------+
Example:
SELECT UCASE(‘informatics’); Result: INFORMATICS
46 | P a g e
8. RIGHT(): Returns the given number of characters by extracting them from
the right side of the given string
Example:
SELECT RIGHT(‘INFORMATICS PRACTICES’,3); Result: CES
47 | P a g e
12. TRIM(): Removes leading and trailing spaces.
Example:
SELECT TRIM(' $$INFOR MATICS$$ '); Result: ‘$$INFOR MATICS$$’
Date/Time Functions
1. NOW(): Returns the current date and time
Example:
select NOW(); Result: '2020-04-06 13:58:11'
48 | P a g e
Multiple Row Functions
(Aggregate Function)
3) MAX(): It returns the maximum value among the given set of values of
any column or expression based on column.
Example: SELECT MAX(MARKS) FROM STUDENT;
It displays maximum marks from the column marks of student table.
4) MIN(): It returns the minimum value among the given set of values of
any column or expression based on column.
Example: SELECT MIN (MARKS) FROM STUDENT;
It displays minimum marks from the column marks of student table.
49 | P a g e
5) COUNT(): It count the number of non-null values in a column. It can
take one argument, which can be a column name or *. When the argument
is a column name then COUNT() returns the non-null values in that column.
If the argument is an * then COUNT() counts the total number of records /
rows along with the NULL values satisfying the condition, if any, in the table.
So, it returns the total number of records or rows from the table.
50 | P a g e
Sorting data on Multiple columns:
Syntax:
SELECT <column_name> FROM <table_name>
[where <condition>]
ORDER BY <column_name> [ASC/DESC] , <column_name> [ASC/DESC];
Example: To display the roll number, name and marks of all the
students in descending order of their marks and ascending order of
their names.
SELECT ROLLNO, NAME , MARKS FROM STUDENT
ORDER BY MARKS DESC, NAME;
GROUP BY in SQL
• At times we need to fetch a group of rows on the basis of common
values in a column. This can be done using a GROUP BY clause.
• It groups the rows tog-ether that contain the same values in a
specified column. We can use the aggregate functions (COUNT, MAX,
MIN, AVG and SUM) to work on the grouped values.
• HAVING Clause in SQL is used to specify conditions on the rows with
GROUP BY clause.
Syntax:
SELECT <column1, column2…..> , aggregate function(colname)
FROM <tablename>
WHERE <condition>
GROUP BY <column1>
HAVING <condition>;
51 | P a g e
Consider the SALE table is given below.
52 | P a g e
(iii) Display number of people in each category of payment mode
from the table SALE.
mysql> SELECT PaymentMode, COUNT(PaymentMode) FROM SALE
GROUP BY Paymentmode ORDER BY Paymentmode;
1 Write down name of four functions that can be used with Group by?
54 | P a g e
ii. SELECT INSTR(‘ARTIFICIAL INTELLIGENCE’, ‘IA’);
→ O/P - 8
UNSOLVED QUESTIONS
55 | P a g e
iii. I am a date and time function, returns the month name from the
specified date.
5) Explain the following function with examples.
i. dayname( )
ii. instr( )
iii. now( )
Empid Salary
1 45000
2 50000
3 55000
4 40000
5 NULL
Write the output of the following:
i. Select mod(Salary, 100) from emp;
ii. Select average(Salary) from emp;
iii. Select sum(Salary) from emp where empid > 3;
iv. Select max(Salary) from emp;
56 | P a g e
11) Predict the output for following query:
i. select pow(month(now()),2);
ii. select left(dayname(now()),5)
iii. select length('Informatics Practice Class 12’);
OR
Explain the functions with suitable example to do this:
i. To find the position of specific word or character in the given text
ii. Display the total number characters from the text
iii. To display remainder of given two numbers
12) Vats is working with functions of MySQL. Explain him the following with
example:
i. To remove extra leading spaces from the text
ii. To return only day part from today’s date
iii. To return average of particular column from the table
13) Om has written following queries:
i. select count(*) from student;
ii. select count(std_no) from student;
He was surprised with the output as query (i) returns 5 rows whereas
Query(ii) returns only 3 rows. Explain why?
14) Which functions in MySQL extract words from specified character to n
number of character from given string. Write the function names and explain
them with example.
15) Anuj is student of class XII, trying to execute the following queries, help
him to predict the output.
i. select round (45.9,-2);
ii. select round ( -101.86,0)
57 | P a g e