Informatics Pratices (Mysql Functions) : Whole (-1.58) Number Will Be Rounded To - 2
Informatics Pratices (Mysql Functions) : Whole (-1.58) Number Will Be Rounded To - 2
Single row functions returns a single result row for every row of queried table. They are categorized into Numeric functions, string functions and date and time functions S.no Name 1 Power() I Numeric functions Description example Returns the argument i. pow(3,4) raised to the specified power. Pow()works same way ii. pow(2,-2) iii. pow(-2,3) ROUND(X): Rounds i. ROUND(-1.23) The Argument. ROUND(X,D)Rounds the argument to D ii.ROUND(-1.48) decimal places. i. If D is iii. ROUND(-1.58) negative it rounds left of the decimal places ii. If D is positive it iv. ROUND(-2.66) rounds right of the v. ROUND(-22.66) decimal places vi. ROUND(3.798, 1) Result 81
ROUND()
0.25 -8 i. -1(here .23 is below .5 so rounded to -1) ii. -1 iii. -2 because D is not specified, the whole(-1.58) number will be rounded to -2 iv. -3 v. -23 vi. 3.8 because it is .798 is more than .750 vii. 3.4 because . 446 is below 450. viii. 3.5
vii. round(3.446,1)
x. round( 3.456, 2)
x. 3. 46
because it should be rounded to 2 decimal places. xi. 3.5 xii. 3.456 xiii. 0 because nearest digit is 0. xiv. xv. xvi. xvii. xviii. xix. xx. xxi. xxii. xxiii. xxiv. xxv. i. ii. iii. iv. Result 8 0 10 10 20 0 200 100 1000 1000 1000 2000 30000 7.2 20 200 200
Truncate()
Truncates the argument to specified number of decimal ii. truncate(27.29, -1) places iii. truncate(255.56, -2) iv. truncate(275.67, -2) II. character functions Description Returns the length of the string in bytes. (including spaces) Returns the character for each integer passed Returns concatenated string Returns the argument in lower case Returns the argument in example Length(COMPUTER)
xiv. round(4.456,-1) xv. round(5.456, -1) xvi. round(13.456,-1) xvii. round (15.456, -1) xviii. round(15.456,-2) xix. round(156.456,-2) xx.round(146.456, -2) xxi. round(1074.567,-3) xxii. round(1174.567,-3) xxiii. round(1474.567,-3) Xxiv. round(1574.567,-3) xxv. round(25567.567,-4) i. truncate(7.29,1)
Char()
Char(65) Note : ASCII value A-65 a-97 0-48 Concat(we, , can win) Concat( concat(are, we), learn) Lower(ABCDefg) upperr(ABCDefg)
Concat()
4 5
UCASE() LEFT()
RIGHT()
MID() SUBSTR()
upper case Returns the leftmost number of character as specified. Returns the specified number of character from the rightmost position Returns the substring as specified.
LEFT(ABCDE, 3)
ABC
RIGHT(ABCD,3)
BCD
LCOME OME T IP If starting point is negative it should extracts from right TO IP OME TO IP YOU CAN YOU CAN Ii wont remove leading spaces. YPU CAN
SUBSTR( WELCOME TO IP,-5,5) SUBSTR( WELCOME TO IP,5) 10 11 12 LTRIM() RTRIM() TRIM() Removes leading spaces Removes trailing spaces Removes trailing spaces and leading spaces. Returns the position first occurrence of search string LTRIM( RTRIM( TRIM( YOU CAN) YOU CAN ) YOU CAN )
13
INSTR()
III. DATE Description Returns the current date Returns the current date and time Returns the
DATE()
MONTH()
6 7
YEAR() DAYNAME()
time at which the function executes Extracts the date part of a date or datetime expression Returns the month From the date passed Returns the year Returns the name of the weekday Returns the day of the month(0-31) Returns the weekday index of the argument Return the day of the year(1-366)
DATE(2010-12-21 13:46:23)
2010-12-21
MONTH(2010-12-21)
12
YEAR(2010-12-21) DAYNAME(2010-12-21)
2010 TUESDAY
8 9
DAYOFMONTH DAYOFWEEK()
DAYOFMONTH(2010-12-21) DAYOFWEEK(2010-12-21)
10
DAYOFYEAR()
DAYOFYEAR(2010-01-21) DAYOFYEAR(2010-12-31)
IV. AGGREGATE FUNCTION. EMP Empn o 7839 7698 7782 7566 7654 7499 7844 Enam e King Blake Clark Jones Marti n Allen Turne Job Preside nt Manage r Manage r Manage r salesm an Salesm an Salesm Mgr Hiredate 2001-1221 2010-1112 2010-0304 2010-0411 2008-0303 2009-1003 2009-07Sal 500 0 285 0 245 0 297 5 125 0 160 0 150 Com m. Deptn o 10 30 10 20 1400 300 0 30 30 30
r an James Clerk Ward Ford Smith Scott Adam s Miller Salesm an Analyst Clerk Analyst Clerk Clerk Loc
30 30 20 20 20 20 10
SUMMARIZING DATA WITH AGGREGATE FUNCTIONS 1. SUM() : computes total of a column ; If DISTINCT option is specified then the duplicates values are ignored. SELECT SUM ( SAL ) FROM EMP Ans: SELECT SUM ( DISTINCT SAL) FROM EMP Ans: 2. AVG () : computes the average value in column; If DISTINCT option is specified then the duplicates values are ignored. SELECT AVG ( SAL ) FROM EMP; Ans: SELECT AVG ( DISTINCT SAL ) FROM EMP Ans: 3. MIN() : Finds the smallest value in the column SELECT MIN ( SAL) FROM EMP Ans: 4. MAX () : finds the largest value in a column SELECT MAX ( MAX) FROM EMP; Ans: 5. i. COUNT () Counts the number of values in a column[ excludes NULLs ] ii. COUNT(*) Counts rows of query results[ includes NULLs ] 1. To count no. of values(different) in a column E.G. SELECT COUNT (COMM) [ excludes NULLs ] FROM EMP; Ans : E.G. SELECT COUNT ( DISTINCT JOB)[ excludes NULLs ] FROM EMP; Ans : TRY UR SELF : SUM() I. To display total salary paid to clerks II. To display total salary paid to managers without duplicates AVG()
I. To display average salary paid to clerks II. To display average salary paid to managers without duplicates MAX() I. To display highest salary of all managers II. To display highest salary of all managers joined after 5th April-98 III. To display difference between highest and lowest salaries Min() I. To display highest salary of all managers II. To display highest salary of all managers joined after 5th April-99 Count() i. To display number employees getting commission ii. To display number employees not getting commission iii. To display all jobs without duplicates iv. To display number of employees working as managers and clerks v. To display cardinality of the table
Worksheets on MYSQL functions: refer tables EMP AND DEPT 1. SELECT POW(2,-4) ; 2. SELECT POW(2,3) FROM EMP WHERE JOB=MANAGER; 3. SELECT ROUND(12.34, 1); 4. SELECT ROUND(12.34, -1); 5. SELECT ROUND(124.34, -2), ROUND(124.34, 1); 6. SELECT ROUND(1234.34, 1), SELECT ROUND(1204.34, -3); 7. SELECT TRUNCATE(124.34, 1); 8. SELECT TRUNCATE(124.34, -2); 9. SELECT TRUNCATE(124.355, 2); 10. SELECT LEFT(JOB, 2) FROM EMP; 11. SELECT LEFT(JOB, 4) FROM EMP; 12. SELECT RIGHT(JOB, 2) FROM EMP; 13. SELECT RIGHT(JOB, 4) FROM EMP; 14. SELECT CONCAT(ENMAE, JOB) FROM EMP; 15. SELECT CONCAT(LCASE(ENMAE), LCASE(JOB)) FROM EMP; 16. SELECT CONCAT(CONCAT(ENMAE, IS WORKING AS), JOB) FROM EMP; 17. SELECT SUBSTR(ENAME, 2,3) FROM EMP; 18. SELECT SUBSTR(ENAME, -2,3) FROM EMP; 19. SELECT SUBSTR(ENAME, 2) FROM EMP; 20. SELECT SUBSTR(ENAME, 2,3) FROM EMP WHERE SAL>3000; 21. SELECT TRIM( LEADING FROM IP IS JAVA ); 22. SELECT TRIMTRAILING FROM IP IS JAVA ); 23. SELECT TRIM( BOTH FROM IP IS JAVA ); 24. SELECT TRIM( LEADING # FROM ####IP IS JAVA ###); 25. SELECT TRIM( LEADING # FROM ####IP IS JAVA ###); 26. SELECT TRIM( LEADING # FROM ####IP IS JAVA ###); 27. Select instr(ENAME , LL) FROM EMP; 28. Select LENGTH(ENAME) FROM EMP; 29. SELECT MID(ENAME, 2,3) FROM EMP WHERE ENAME LIKE J%; 30. SELECT MONTH(HIREDATE) FROM EMP WHERE JOB =MANAGER; 31. SELECT YEAR(HIREEDATE) FROM EMP WHERE JOB =MANAGER; 32. SELECT DATE(HIREDATE) FROM EMP WHERE JOB =MANAGER; 33. SELECT DAYOFMONTH(HIREDATE) FROM EMP WHERE JOB =MANAGER; 34. SELECT DAYOFWEEK(HIREDATE) FROM EMP WHERE JOB =MANAGER; 35. SELECT DAYOFYEAR(HIREDATE) FROM EMP WHERE JOB =MANAGER; 36. SELECT NOW(), SYSDATE() ; 37. SELECT ENAME, YEAR(CURDATE())- YEAR(HIREDATE) FROM EMP; 38. SELECT ENAME, YEAR(CURDATE())- YEAR(HIREDATE) FROM EMP WHERE SAL<2000; 39. SELECT ENAME, YEAR(CURDATE())- YEAR(HIREDATE) FROM EMP WHERE JOB =CLERK; 40. SELECT ENAME, YEAR(CURDATE())- YEAR(HIREDATE) FROM EMP WHERE ENAME LIKEK%;