0% found this document useful (0 votes)
8 views7 pages

Manual No 7

The document discusses SQL functions including aggregate, scalar, and date functions. It provides examples of SUM, AVG, MIN, MAX, COUNT, ABS, POWER, SQRT, ROUND, TRUNCATE, EXP, GREATEST, LEAST, MOD, FLOOR, SIGN, and LOG functions.

Uploaded by

usmanshams787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Manual No 7

The document discusses SQL functions including aggregate, scalar, and date functions. It provides examples of SUM, AVG, MIN, MAX, COUNT, ABS, POWER, SQRT, ROUND, TRUNCATE, EXP, GREATEST, LEAST, MOD, FLOOR, SIGN, and LOG functions.

Uploaded by

usmanshams787
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

3

Submitted By: SWEN221101044


Chapter 7

Lab 7: SQL Functions

Objectives
Familirization with SQL Functions

SQL Functions
These functions are used to manipulating data items and returning the results.

• Group functions or Aggregate functions.

• Single Row or scalar function.

Group functions or Aggregate functions


These functions operated a set of values or rows

• Sum()

• Avg()

• Min()

• Max()

• Count()

Sum()
Sum() used to find out the sum of the salary of employees.
Ex:List the sum of the salary of employees
Select sum(sal) from emp;
Output
3 Lab 7: SQL

It find out the average salaries of employees.


Ex:List the average salary of the employees
Select avg(sal) from emp;
Output

Min()
Min() used to find out the minimum salary of an employee in the given table.
Ex:list out the minimum salary of an employee in the emp table.
Select min(sal) from emp;
Output

Max()
Max() used to find out the maximum salary of an employee in the given table.
Ex:list out the maxiimum salary of an employee in the emp table.
Select max(sal) from emp;
Output

Count()
Count() used to list out the number of values in a particular table.
Ex:
1.List the numbers of jobs.
select count (job) from emp;
Output

2. List the numbers of people and average salary in deptno 30.


select count(*),avg(sal) from emp where deptno=30;
Output
3

Single Row or scalar function

These functions are operated a single row at a time.


Abs()
Abs() used to find the absolute value.
Select abs(10) from dual;
Output

Power()
Power():find the power.
Select power(2,3) from dual;
Output

Sqrt()
Sqrt() used to find the square root of a given value.
Select sqrt(9) from dual;
Output

Round()
Round() used to find the round of the value.
Select round(12.35,1) from dual;
Output

Truncate()
Truncate() used to find the truncate value.
Select trunc(12.35,1) from dual;
3 Lab 7: SQL

Exp()
Exp() used to used to find the exponential of given number.
Select exp(3) from dual;

Greatest()
Greastest() used to find out the greater value.
Select greatest(10,20,30) from dual;
Output

Least()
Least() used to find out the leastervalue.
Select least(10,20,30) from dual;

Mod()
Mod() used to fina tha module of given numbers.
Select mod(3,2) from dual;

Floor()
Floor() used to find the floor value.
Select floor(12.56) from dual;

Sign()
Sign() used to find the sign of a number.
3

Select sign(-10) from dual;

Select sign(10) from dual;


Output

Log()
Log() used to find logarithmic value.
Select log(3,2) from dual;

In these function we are using date functions also there are listed below:

Select months_between(’26-jun-06’,’25-may-06’) from dual;


MONTHS_BETWEEN(’26-JUN-09’,’25-MAY-09’)
1.03225806
Select add_months(’26-jun-06’,5’) from dual;
ADD_MONTH
26-NOV-06
Select next_day(’26-jun-09’,’monday’) from dual;
NEXT_DAY(
29-JUN-09
Select last_day(’26-jun-09’) from dual;
LAST_DAY(
30-JUN-09

SOLUTION:
create database [lab 8]
use [lab 8]
CREATE TABLE emp (
empno INT PRIMARY KEY,
ename VARCHAR(10),
job VARCHAR(9),
mgr INT,
hiredate DATE,
sal NUMERIC(7, 2),
comm NUMERIC(7, 2),
deptno INT
);
-- Inserting sample data
INSERT INTO emp VALUES
(7369, 'SMITH', 'CLERK', 7902, '17-DEC-1980', 800, NULL, 20),
(7499, 'ALLEN', 'SALESMAN', 7698, '20-FEB-1981', 1600, 300, 30),
(7521, 'WARD', 'SALESMAN', 7698, '22-FEB-1981', 1250, 500, 30),
(7839, 'KING', 'PRESIDENT', NULL, '17-NOV-1981', 5000, NULL, 10);
-- Verify the data
SELECT * FROM emp;
-- sum of salary
SELECT SUM(sal) AS SUM_ FROM emp;
-- Average od salary
SELECT AVG(sal) AS AVG_ FROM emp;
-- Minimum Salary
SELECT MIN(sal) AS MIN_ FROM emp;
-- Maximum Salary
SELECT MAX(sal) AS MAX_ FROM emp;
-- Count Number of Jobs
SELECT COUNT(DISTINCT job) AS N#JOBS FROM emp;
4 Lab 7: SQL

-- List Nhumber of people and average salary in deptno 30


SELECT COUNT(*), AVG(sal) FROM emp WHERE deptno =30;
-- Abs Function
SELECT ABS(sal) as absSalary FROM emp;
-- Power Function:
SELECT POWER(30,2) AS powerOf__30;
-- sqrt function
SELECT SQRT(9) AS SQUARE_ROOT;
-- round function
SELECT ROUND(12.35, 1) AS ROUND_;
-- truncate function
SELECT ROUND(12.35, 1) AS TRUNCATE_;
-- exp function
SELECT EXP(3) AS exponent;
-- greatest function
SELECT GREATEST(10,20,30,700) AS LARGEST;
-- least function
SELECT LEAST(10,20,16,5) AS SMALLEST;
-- modulus function
SELECT 3%2 AS MODULUS;
-- floor function
SELECT FLOOR(12.56) AS FLOOR_;
-- sign function
SELECT SIGN(-100) AS SIGN_;
-- log function
SELECT LOG(3,2) AS LOARITHM;

OUTPUT:
4

You might also like