SlideShare a Scribd company logo
1 of 14
Download to read offline
Chapter 3
Database
query
using sql –
functions
New
syllabus
2022-23
Visit : python.mykvs.in for regular updates
SQL functions
Basically, it is a set of SQL statements that accept only input parameters, perform
actions and return the result. A function can return an only a single value or a
table. Functions are not alternate to sql commands but are used as a part of sql
command(generally select command).
Types of Function(System defined)
A scalar function is a function that operates on scalar values -- that is, it takes
one (or more) input values as arguments directly and returns a value.Maths,text,
date functions etc. These functions can be applied over column(s) of a table
to perform relevant operation on value of each record.
For e.g. select left(name,4) from student;
Will display 4 left side letters of each row of name field from student table.
An aggregate function is a function that operates on aggregate data -- that is, it
takes a complete set of data as input and returns a value that is computed from
all the values in the set. E.g. max(), min(), count(), sum(), avg().Generally these
are used for report preparation & mostly used with group by and having clause.
Visit : python.mykvs.in for regular updates
Mathematical functions –Perform operation over numeric value
POWER() – power() returns the value of a number raised to the
power of another number. The synonym of power() is pow().
Syntax - pow(m,n)
m A number which is the base of the exponentiation.
n A number which is the exponent of the exponentiation.
E.g.
Mysql> select pow(2,3);
Mysql>8
Mysql> select pow(2.37,3.45);
Mysql>19.6282……
Visit : python.mykvs.in for regular updates
SQL functions
Mathematical functions
ROUND() – the round()
function returns a number
rounded to a certain number of
decimal places.
Syntax -
ROUND(column_name,decimals)
column_name -Required. The
field to round.
decimals -Required,
Specifies the number of decimals
to be returned.
Decimal places position value is
rounded to next integer ,if its next right
side number is>=5
Default decimal place is 0 position if we
not specify
Visit : python.mykvs.in for regular updates
SQL functions
Mathematical functions
MOD() – The MOD() function returns the remainder of one
number divided by another. The following shows the syntax of the
MOD() function:
Syntax - MOD(dividend,divisor)
Dividend - is a literal number or a numeric expression to divide.
Divisor- is a literal number or a numeric expression by which to
divide the dividend.
E.g.
Mysql> SELECT MOD(11, 3);
Mysql>2
Mysql> SELECT MOD(10.5, 3);
Mysql>1.5
Visit : python.mykvs.in for regular updates
SQL functions
Text functions- Perform operation over string values.
UPPER() – UPPER(str)
Returns the string str with all characters changed to uppercase.
mysql> SELECT UPPER(‘Tej');
-> ‘TEJ'
UCASE(str)-UCASE() is a synonym for UPPER().
LOWER(str)-Returns the string str with all characters changed to
lowercase
mysql> SELECT LOWER('QUADRATICALLY');
-> 'quadratically’
LCASE(str)
LCASE() is a synonym for LOWER().
Visit : python.mykvs.in for regular updates
SQL functions
Text functions- Perform operation over string values.
SUBSTRING(str,pos) - SUBSTRING(str FROM pos),
SUBSTRING(str,pos,len)- SUBSTRING(str FROM pos FOR len)
The forms without a len argument return a substring from string str starting at position pos.
The forms with a len argument return a substring len characters long from string str,
starting at position pos. The forms that use FROM are standard SQL syntax. It is also
possible to use a negative value for pos. In this case, the beginning of the substring is pos
characters from the end of the string, rather than the beginning.
mysql> SELECT SUBSTRING(‘practically',5);
-> 'tically'
mysql> SELECT SUBSTRING('foofarbar' FROM 4);
-> ‘farbar'
mysql> SELECT SUBSTRING('Quadratically',5,6);
-> 'ratica'
mysql> SELECT SUBSTRING(‘Aakila', -3);
-> 'ila'
mysql> SELECT SUBSTRING(‘Aakila', -5, 3);
-> 'aki'
mysql> SELECT SUBSTRING(‘Aakila' FROM -4 FOR 2);
-> 'ki'
MID(str,pos,len)
MID(str,pos,len) is a synonym for
SUBSTRING(str,pos,len),substr()
Visit : python.mykvs.in for regular updates
SQL functions
Text functions- Perform operation over string values.
LENGTH(str) - Returns the length of the string str
mysql> SELECT LENGTH('text');
-> 4
LEFT(str,len) - Returns the leftmost len characters from the
string str, or NULL if any argument is NULL.
mysql> SELECT LEFT(‘Toolbar', 4);
-> ‘Tool‘
RIGHT(str,len)-Returns the rightmost len characters from
the string str, or NULL if any argument is NULL.
mysql> SELECT RIGHT(‘Toolbar', 3);
-> 'bar'
Visit : python.mykvs.in for regular updates
SQL functions
Text functions- Perform operation over string values.
INSTR(str,substr)-Returns the position of the first
occurrencee of substring substr in string str.
mysql> SELECT INSTR(‘Toobarbar', 'bar');
-> 4
mysql> SELECT INSTR('xbar', ‘ybar');
-> 0
Visit : python.mykvs.in for regular updates
SQL functions
Text functions- Perform operation over string values.
LTRIM(str)-Returns the string str with leading space characters removed.
mysql> SELECT LTRIM(' Toolbar');
-> ‘Toolbar‘
RTRIM(str)-Returns the string str with trailing space characters removed.
mysql> SELECT RTRIM(‘Toolbar ');
-> ‘Toolbar‘
TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)- Returns the string str
with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING,
or TRAILING is given , BOTH is assumed.
mysql> SELECT TRIM(' tool ');
-> 'bar'
mysql> SELECT TRIM(LEADING 'x' FROM 'xxxtoolxxx');
-> ‘toolxxx'
mysql> SELECT TRIM(BOTH 'x' FROM 'xxxtoolxxx');
-> ‘tool'
mysql> SELECT TRIM(TRAILING 'xyz' FROM ‘toolxxx');
-> ‘tool'
Visit : python.mykvs.in for regular updates
SQL functions
Date functions- Perform operation over date values.
NOW()-Returns the current date and time as a value in 'YYYY-MM-
DD hh:mm:ss' or YYYYMMDDhhmmss format, depending on
whether the function is used in string or numeric context.
mysql> SELECT NOW();
-> '2020-04-05 23:50:26'
mysql> SELECT NOW() + 0;
-> 20200415235026.000000
Here +0 means +0 second
DATE(expr)-Extracts the date part of the date or datetime
expression expr.
mysql> SELECT DATE('2003-12-31 01:02:03');
-> '2003-12-31'
Visit : python.mykvs.in for regular updates
SQL functions
Date functions- Perform operation over date values.
MONTH(date)-Returns the month for date, in the range 1 to
12 for January to December, or 0 for dates such as '0000-00-
00' or '2008-00-00' that have a zero month part.
mysql> SELECT MONTH('2008-02-03');
-> 2
MONTHNAME(date)-Returns the full name of the month for
date.
mysql> SELECT MONTHNAME('2008-02-03');
-> 'February‘
Visit : python.mykvs.in for regular updates
SQL functions
Date functions- Perform operation over date values.
YEAR(date)-Returns the year for date, in the range 1000 to
9999, or 0 for the “zero” date.
mysql> SELECT YEAR('1987-01-01');
-> 1987
DAY(date)-Returns the day of the month for date, in the
range 1 to 31, or 0 for dates such as '0000-00-00' or '2008-00-
00' that have a zero day part.
mysql> SELECT DAYOFMONTH('2007-02-03');
-> 3
DAYNAME(date)-Returns the name of the weekday for date.
mysql> SELECT DAYNAME('2007-02-03');
-> 'Saturday'
Visit : python.mykvs.in for regular updates
SQL functions
Visit : python.mykvs.in for regular updates
Aggregate Functions & NULL- Perform operation over set of
valuesConsider a table Emp having following records as-
Null values are excluded while (avg)aggregate function is used
SQL Queries
mysql> Select Sum(Sal) from EMP;
mysql> Select Min(Sal) from EMP;
mysql> Select Max(Sal) from EMP;
mysql> Select Count(Sal) from EMP;
mysql> Select Avg(Sal) from EMP;
mysql> Select Count(*) from EMP;
Emp
Code Name Sal
E1 Mohak NULL
E2 Anuj 4500
E3 Vijay NULL
E4 Vishal 3500
E5 Anil 4000
Result of query
12000
3500
4500
3
4000
5
SQL functions

More Related Content

Similar to sql functions3 (1).pdf

Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUadAccount
 
Mysqlppt
MysqlpptMysqlppt
MysqlpptReka
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functionsNIKET CHAURASIA
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programmingVisnuDharsini
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10Syed Asrarali
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sqlpgolhar
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetJose Perez
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?Andrej Pashchenko
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEDrkhanchanaR
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkAngelo Leto
 
Interface Python with MySQL connectivity.pptx
Interface Python with MySQL connectivity.pptxInterface Python with MySQL connectivity.pptx
Interface Python with MySQL connectivity.pptxBEENAHASSINA1
 
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxINFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxcarliotwaycave
 

Similar to sql functions3 (1).pdf (20)

Using-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptxUsing-Python-Libraries.9485146.powerpoint.pptx
Using-Python-Libraries.9485146.powerpoint.pptx
 
Function and types
Function  and typesFunction  and types
Function and types
 
Mysqlppt
MysqlpptMysqlppt
Mysqlppt
 
A brief introduction to apply functions
A brief introduction to apply functionsA brief introduction to apply functions
A brief introduction to apply functions
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Intro to tsql unit 10
Intro to tsql   unit 10Intro to tsql   unit 10
Intro to tsql unit 10
 
python modules1522.pdf
python modules1522.pdfpython modules1522.pdf
python modules1522.pdf
 
Spark workshop
Spark workshopSpark workshop
Spark workshop
 
Key functions in_oracle_sql
Key functions in_oracle_sqlKey functions in_oracle_sql
Key functions in_oracle_sql
 
GeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheetGeoGebra JavaScript CheatSheet
GeoGebra JavaScript CheatSheet
 
SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?SQL Macros - Game Changing Feature for SQL Developers?
SQL Macros - Game Changing Feature for SQL Developers?
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Database programming
Database programmingDatabase programming
Database programming
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Sql 3
Sql 3Sql 3
Sql 3
 
Chapter8 my sql revision tour
Chapter8 my sql revision tourChapter8 my sql revision tour
Chapter8 my sql revision tour
 
Introduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with sparkIntroduction to parallel and distributed computation with spark
Introduction to parallel and distributed computation with spark
 
Interface Python with MySQL connectivity.pptx
Interface Python with MySQL connectivity.pptxInterface Python with MySQL connectivity.pptx
Interface Python with MySQL connectivity.pptx
 
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docxINFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
INFORMATIVE ESSAYThe purpose of the Informative Essay assignme.docx
 
Chapter-5.ppt
Chapter-5.pptChapter-5.ppt
Chapter-5.ppt
 

Recently uploaded

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 

Recently uploaded (20)

(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 

sql functions3 (1).pdf

  • 1. Chapter 3 Database query using sql – functions New syllabus 2022-23 Visit : python.mykvs.in for regular updates
  • 2. SQL functions Basically, it is a set of SQL statements that accept only input parameters, perform actions and return the result. A function can return an only a single value or a table. Functions are not alternate to sql commands but are used as a part of sql command(generally select command). Types of Function(System defined) A scalar function is a function that operates on scalar values -- that is, it takes one (or more) input values as arguments directly and returns a value.Maths,text, date functions etc. These functions can be applied over column(s) of a table to perform relevant operation on value of each record. For e.g. select left(name,4) from student; Will display 4 left side letters of each row of name field from student table. An aggregate function is a function that operates on aggregate data -- that is, it takes a complete set of data as input and returns a value that is computed from all the values in the set. E.g. max(), min(), count(), sum(), avg().Generally these are used for report preparation & mostly used with group by and having clause. Visit : python.mykvs.in for regular updates
  • 3. Mathematical functions –Perform operation over numeric value POWER() – power() returns the value of a number raised to the power of another number. The synonym of power() is pow(). Syntax - pow(m,n) m A number which is the base of the exponentiation. n A number which is the exponent of the exponentiation. E.g. Mysql> select pow(2,3); Mysql>8 Mysql> select pow(2.37,3.45); Mysql>19.6282…… Visit : python.mykvs.in for regular updates SQL functions
  • 4. Mathematical functions ROUND() – the round() function returns a number rounded to a certain number of decimal places. Syntax - ROUND(column_name,decimals) column_name -Required. The field to round. decimals -Required, Specifies the number of decimals to be returned. Decimal places position value is rounded to next integer ,if its next right side number is>=5 Default decimal place is 0 position if we not specify Visit : python.mykvs.in for regular updates SQL functions
  • 5. Mathematical functions MOD() – The MOD() function returns the remainder of one number divided by another. The following shows the syntax of the MOD() function: Syntax - MOD(dividend,divisor) Dividend - is a literal number or a numeric expression to divide. Divisor- is a literal number or a numeric expression by which to divide the dividend. E.g. Mysql> SELECT MOD(11, 3); Mysql>2 Mysql> SELECT MOD(10.5, 3); Mysql>1.5 Visit : python.mykvs.in for regular updates SQL functions
  • 6. Text functions- Perform operation over string values. UPPER() – UPPER(str) Returns the string str with all characters changed to uppercase. mysql> SELECT UPPER(‘Tej'); -> ‘TEJ' UCASE(str)-UCASE() is a synonym for UPPER(). LOWER(str)-Returns the string str with all characters changed to lowercase mysql> SELECT LOWER('QUADRATICALLY'); -> 'quadratically’ LCASE(str) LCASE() is a synonym for LOWER(). Visit : python.mykvs.in for regular updates SQL functions
  • 7. Text functions- Perform operation over string values. SUBSTRING(str,pos) - SUBSTRING(str FROM pos), SUBSTRING(str,pos,len)- SUBSTRING(str FROM pos FOR len) The forms without a len argument return a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. mysql> SELECT SUBSTRING(‘practically',5); -> 'tically' mysql> SELECT SUBSTRING('foofarbar' FROM 4); -> ‘farbar' mysql> SELECT SUBSTRING('Quadratically',5,6); -> 'ratica' mysql> SELECT SUBSTRING(‘Aakila', -3); -> 'ila' mysql> SELECT SUBSTRING(‘Aakila', -5, 3); -> 'aki' mysql> SELECT SUBSTRING(‘Aakila' FROM -4 FOR 2); -> 'ki' MID(str,pos,len) MID(str,pos,len) is a synonym for SUBSTRING(str,pos,len),substr() Visit : python.mykvs.in for regular updates SQL functions
  • 8. Text functions- Perform operation over string values. LENGTH(str) - Returns the length of the string str mysql> SELECT LENGTH('text'); -> 4 LEFT(str,len) - Returns the leftmost len characters from the string str, or NULL if any argument is NULL. mysql> SELECT LEFT(‘Toolbar', 4); -> ‘Tool‘ RIGHT(str,len)-Returns the rightmost len characters from the string str, or NULL if any argument is NULL. mysql> SELECT RIGHT(‘Toolbar', 3); -> 'bar' Visit : python.mykvs.in for regular updates SQL functions
  • 9. Text functions- Perform operation over string values. INSTR(str,substr)-Returns the position of the first occurrencee of substring substr in string str. mysql> SELECT INSTR(‘Toobarbar', 'bar'); -> 4 mysql> SELECT INSTR('xbar', ‘ybar'); -> 0 Visit : python.mykvs.in for regular updates SQL functions
  • 10. Text functions- Perform operation over string values. LTRIM(str)-Returns the string str with leading space characters removed. mysql> SELECT LTRIM(' Toolbar'); -> ‘Toolbar‘ RTRIM(str)-Returns the string str with trailing space characters removed. mysql> SELECT RTRIM(‘Toolbar '); -> ‘Toolbar‘ TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str)- Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given , BOTH is assumed. mysql> SELECT TRIM(' tool '); -> 'bar' mysql> SELECT TRIM(LEADING 'x' FROM 'xxxtoolxxx'); -> ‘toolxxx' mysql> SELECT TRIM(BOTH 'x' FROM 'xxxtoolxxx'); -> ‘tool' mysql> SELECT TRIM(TRAILING 'xyz' FROM ‘toolxxx'); -> ‘tool' Visit : python.mykvs.in for regular updates SQL functions
  • 11. Date functions- Perform operation over date values. NOW()-Returns the current date and time as a value in 'YYYY-MM- DD hh:mm:ss' or YYYYMMDDhhmmss format, depending on whether the function is used in string or numeric context. mysql> SELECT NOW(); -> '2020-04-05 23:50:26' mysql> SELECT NOW() + 0; -> 20200415235026.000000 Here +0 means +0 second DATE(expr)-Extracts the date part of the date or datetime expression expr. mysql> SELECT DATE('2003-12-31 01:02:03'); -> '2003-12-31' Visit : python.mykvs.in for regular updates SQL functions
  • 12. Date functions- Perform operation over date values. MONTH(date)-Returns the month for date, in the range 1 to 12 for January to December, or 0 for dates such as '0000-00- 00' or '2008-00-00' that have a zero month part. mysql> SELECT MONTH('2008-02-03'); -> 2 MONTHNAME(date)-Returns the full name of the month for date. mysql> SELECT MONTHNAME('2008-02-03'); -> 'February‘ Visit : python.mykvs.in for regular updates SQL functions
  • 13. Date functions- Perform operation over date values. YEAR(date)-Returns the year for date, in the range 1000 to 9999, or 0 for the “zero” date. mysql> SELECT YEAR('1987-01-01'); -> 1987 DAY(date)-Returns the day of the month for date, in the range 1 to 31, or 0 for dates such as '0000-00-00' or '2008-00- 00' that have a zero day part. mysql> SELECT DAYOFMONTH('2007-02-03'); -> 3 DAYNAME(date)-Returns the name of the weekday for date. mysql> SELECT DAYNAME('2007-02-03'); -> 'Saturday' Visit : python.mykvs.in for regular updates SQL functions
  • 14. Visit : python.mykvs.in for regular updates Aggregate Functions & NULL- Perform operation over set of valuesConsider a table Emp having following records as- Null values are excluded while (avg)aggregate function is used SQL Queries mysql> Select Sum(Sal) from EMP; mysql> Select Min(Sal) from EMP; mysql> Select Max(Sal) from EMP; mysql> Select Count(Sal) from EMP; mysql> Select Avg(Sal) from EMP; mysql> Select Count(*) from EMP; Emp Code Name Sal E1 Mohak NULL E2 Anuj 4500 E3 Vijay NULL E4 Vishal 3500 E5 Anil 4000 Result of query 12000 3500 4500 3 4000 5 SQL functions