Name: Neha Shrestha Rollno:181418 IT Morning (4th
semester)
Title: Inbuilt Functions
Objective: To practice and implement in-built functions to be executed using DML
Theory:
SQL Command:
Numeric function:
Command Query Output
Abs(n) Select abs(-10) FROM 10
<table>
Ceil(n) Select ceil(55.67) FROM 56
<table>
Floor(n) Select floor(100.2) FROM 100
<table>
Exp(n) Select exp(4) FROM <table> 54.59
Power(m,n) Select power(4,2) FROM 16
<table>
Mod(m,n) Select mod(10,3) FROM 1
<table>
Sqrt(n) Select sqrt(16) FROM 4
<table>
Round(m,n) Select round(100.256,2) 100.26
FROM <table>
Character function:
Command Query Output
Lower(char) select lower(‘HELLO’) FROM <table> hello
Upper(char) Select upper(‘hello’) FROM <table> HELLO
Ltrim(char_exp) Select ltrim(‘ Beit’) FROM <table> Beit
Rtrim(char_exp) Select rtrim(‘Beit ’) FROM <table> Beit
Replace(SE,SP,SR) Select replace(‘hello’, ’ll’, ‘xx’) FROM hexxo
<table>
Substring(exp,start,length) Select substring(‘hello’,2 , 3) ell
Count function
• COUNT(*): counts all, inclusive of duplicates and null values
o Select count(*) FROM employee
• COUNT(col_name): avoids null value
o Select count(salary) FROM employee
• COUNT(distinct col_name): avoids repeated and null values
o Select count(distinct salary) FROM employee
1. Group function
• AVG()
▪ Select avg(salary) FROM employee
• MAX()
▪ Select max(salary) FROM employee
• MIN()
▪ Select min(salary) FROM employee
• SUM()
▪ Select sum(salary) FROM employee
➢ GROUP BY clause
• Allows us to use simultaneous column name and group function
• Use in conjunction with the aggregate functions to group the result-set by one or
more columns.
• E.g.: select max(salary), job FROM employee group by job
➢ HAVING clause
• Use to specify conditions on rows retrieved by using group by clause.
• Added to SQL because the WHERE keyword could not be used with aggregate
functions.
• E.g. : select max(salary), job FROM employee group by job having count(*)>=2
Lab Exercise:
Numeric Function:
1.
Query:
SELECT ABS(-10) FROM employee
Output:
2.
Query:
SELECT CEIL(55.6) FROM employee
Output:
3.
Query:
SELECT FLOOR(55.6) FROM employee
Output:
4.
Query:
SELECT exp(eid) FROM employee
Output:
5.
Query:
SELECT power(4,eid) FROM employee
Output:
6.
Query:
SELECT mod(10,3) FROM employee
Output:
7.
Query:
SELECT sqrt(salary) FROM employee
Output:
8.
Query:
SELECT round(100.2686,2) FROM employee
Output:
Character Function:
9.
Query:
SELECT lower(name) FROM employee
Output:
10.
Query:
SELECT upper(name) FROM employee
Output:
11.
Query:
SELECT ltrim(“ name”) FROM employee
Output:
12.
Query:
SELECT rtrim(“ name ”)FROM employee
Output:
13.
Query:
SELECT REPLACE(name,”Ram”,”Shyam”) FROM employee
Output:
14.
Query:
SELECT substring(designation,2,3) FROM employee
Output:
Count Function:
15.
Query:
SELECT count(*) FROM employee
Output:
16.
Query:
SELECT count(eid) FROM employee
Output:
Group Function:
17.
Query:
SELECT AVG(salary) FROM employee
Output:
18.
Query:
SELECT MAX(salary) FROM employee
Output:
19.
Query:
SELECT MIN(salary) FROM employee
Output:
20.
Query:
SELECT SUM(salary) FROM employee WHERE designation=”programmer”
Output:
21.
Query:
SELECT MAX(salary) FROM employee WHERE designation=”developer”
Output:
Group By Clause:
22.
Query:
SELECT MAX(salary),designation FROM employee GROUP BY designation
Output:
Having Clause
23.
Query:
SELECT MIN(salary),designation FROM employee GROUP BY designation HAVING
count(*)>=2
Output:
Conclusion:
In this way we can implement in-built function to be executed using DML.