Class 12 Notes
Class 12 Notes
My SQL
Ch – 1 (SQL Queries using Functions)
1. String Function
These are the Single Row Functions. These Functions only works with string
columns.
(i)Upper() / Ucase()-This string function use to convert all the alphabets in
capital letters.
Example - Select ucase(“Informatics Practices”)
Result - INFORMATICS PRACTICES
(ii)lower() / Lcase()-This string function use to convert all the alphabets in small
letters.
Example - Select Lcase(“Informatics Practices”)
Result - informatics practices
(iii) length() - This string function use to display the length of a string.
Example - Select length(“Informatics Practices”)
Result - 21
(iv) left()- This string function use to display the substring from left side of a
string on given length.
Example - Select left(“Informatics Practices”,6)
Result - Inform
(v) right()- This string function use to display the substring from right side of a
string on given length.
Example - Select right(“Informatics Practices”,3)
Result - ces
(viii) rtrim()- this string function is used to remove space from right hand side of
string(trailing space).
Example - Select rtrim(“ Informatics Practices ”)
Result - “ Informatics Practices”
(ix) trim()- this string function is used to remove space from both sides of string
(Leading & trailing space both ).
Example - Select trim(“ Informatics Practices ”)
Result - “Informatics Practices”
(x) instr()- This string function use to display the position of substring from a
string on given character(s).
Example – Select instr(“Informatics Practices”, “for”)
Result - 3
2. Numerical Function
These are the Single Row Functions. These functions works on integer or float
datatypes.
(i) Pow()/Power()- this function is used to display any power of any no.
Example - Select power(10,3)
Result - 1000
(ii) sqrt() – this Function is used to calculate square root of a number.
Example - Select sqrt(121)
Result - 11
(iv) round()-This function is used to round figure the digit to its near by digit of
given size.
Example - Select round(76892.52865,3)
Result - 76892.529
(v) truncate-this function is used to only remove the digit on given size.
Example - Select truncate (76892.52865,3)
Result - 76892.528
(iv) date()- It will return date only from date time format of given data.
Example - Select date(‘2025-04-20 08:50:52’)
Result - 2025-04-20
(v) time()- It will return time only from date time format of given data.
Example - Select time(‘2025-04-20 08:50:52’)
Result - 08:50:52
(vi) year ()- It will return year only from date time format of given data.
Example - Select year(‘2025-04-20 08:50:52’)
Result - 2025
(vii) month()- It will return month only from date time format of given data.
Example - Select month(‘2025-04-20 08:50:52’)
Result - 04
(viii) day()- It will return day only from date time format of given data.
Example - Select day(‘2025-04-20 08:50:52’)
Result - 20
(ix) hour()- It will return hour only from date time format of given data.
Example - Select hour(‘2025-04-20 08:50:52’)
Result - 08
(x) minute()- It will return minute only from date time format of given data.
Example - Select minute(‘2025-04-20 08:50:52’)
Result - 50
(xi) second()- It will return second only from date time format of given data.
Example - Select second(‘2025-04-20 08:50:52’)
Result - 52
(xii) monthname() - It will return month name only from date time format of
given data.
Example - Select monthname(‘2025-04-20’)
Result - April
(xiii) dayname() - It will return day name like Sunday Monday only from date
time format of given data.
Example - Select dayname(‘2025-04-20‘)
Result - Sunday
(xiv) dayofweek() - It will return day number from weekdays like 1-Sunday, 2-
Monday up so on.
Example - Select dayofweek(‘2025-04-20‘)
Result - 1
(xiv) dayofyear() - It will return day number from a year(365) or leap year(366)
Example - Select dayofyear(‘2025-04-20‘)
Result - 110
4. Aggregate Function
These are the Multiple Row Functions. These function works along with MySQL
tables. We can use Group by Clause and Having Clause with these functions
Table: Student
RollNo Name DOB Class Fees Sports
1 Aman Sharma 2008-11-12 11 10000 Basketball
2 Rohit Kumar 2009-01-01 11 20000 Football
3 Ravi Jain 2008-04-05 12 10000 Basketball
4 Puneet Gupte 2009-03-11 11 15000 Basketball
5 Abhay Sharma 2009-12-25 11 NULL Tennis
6 Yash Godha 2008-07-01 12 10000 Cricket
7 Mohit Sharma 2008-09-09 11 15000 Cricket
8 Pawan Jain 2007-05-12 12 10000 Tennis
9 Pulkit Kumar 2009-10-21 10 15000 Football
10 Suyash Mehra 2008-07-07 11 20000 Cricket
(i) Sum() – This Function given the sum of all values from the table column.
Example – Select Sum(Fees) from Student;
Result – 125000
(ii) Min() – This Function given the Minimum values from the table column.
Example – Select Min(Class) from Student;
Result – 10
(iv) Avg() – This Function given the average values from the table column. It not
count the null value in it.
Example – Select Avg(Fees) from Student;
(125000/9)
Result – 13888.88
GROUP by Clause
This Clause works with only aggregate function of MySQL. This function works
with group of the data.
Example – Select Sports, count(*) from Student Group by sports;
Result –
Sports Count(*)
Basketball 3
Football 2
Tennis 2
Cricket 3