0% found this document useful (0 votes)
8 views

Functions and Grouping

The document outlines various types of SQL functions, including single row functions, aggregate functions, date functions, and conversion functions, along with examples of their usage. It also explains the GROUP BY and HAVING clauses for grouping data and filtering results based on aggregate conditions. Additionally, it provides practical SQL queries for creating a table, inserting data, and retrieving specific information based on salary and department criteria.

Uploaded by

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

Functions and Grouping

The document outlines various types of SQL functions, including single row functions, aggregate functions, date functions, and conversion functions, along with examples of their usage. It also explains the GROUP BY and HAVING clauses for grouping data and filtering results based on aggregate conditions. Additionally, it provides practical SQL queries for creating a table, inserting data, and retrieving specific information based on salary and department criteria.

Uploaded by

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

FUNCTIONS AND GROUPING:

There are 2 types of functions:

 Single row functions


It works on column from each row and returns one result per row.
 Group / aggregate functions
It manipulates data in a group of rows and returns single result.

Single row functions:

(1) Character or Text Functions:


Character or text functions are used to manipulate text strings. They accept
strings or characters as input and can return both character and number values
as output.
Few of the character or text functions are as given below:

Function Name Return Value

LOWER (string_value) All the letters in 'string_value' is converted to lowercase.

UPPER (string_value) All the letters in 'string_value' is converted to uppercase.

INITCAP (string_value) All the letters in 'string_value' is converted to mixed case.

LTRIM (string_value, All occurrences of 'trim_text' is removed from the left


trim_text) of 'string_value'.

RTRIM (string_value, All occurrences of 'trim_text' is removed from the right of


trim_text) 'string_value' .

TRIM (trim_text FROM All occurrences of 'trim_text' from the left and right
string_value) of 'string_value' ,'trim_text' can also be only one character
long .

SUBSTR (string_value, m, Returns 'n' number of characters


n) from'string_value' starting from the 'm'position.

LENGTH (string_value) Number of characters in 'string_value'in returned.

LPAD (string_value, n, Returns 'string_value' left-padded with'pad_value' . The


pad_value) length of the whole string will be of 'n' characters.

RPAD (string_value, n, Returns 'string_value' right-padded with 'pad_value' . The


pad_value) length of the whole string will be of 'n' characters.

For Example, we can use the above UPPER() text function with the column value as follows.
SELECT UPPER (product_name) FROM product;
The following examples explains the usage of the above character or text functions
Function Name Examples Return Value

LOWER(string_value) LOWER('Good Morning') good morning

UPPER(string_value) UPPER('Good Morning') GOOD


MORNING

INITCAP(string_value) INITCAP('GOOD MORNING') Good Morning

LTRIM(string_value, trim_text) LTRIM ('Good Morning', 'Good) Morning

RTRIM (string_value, trim_text) RTRIM ('Good Morning', ' Good


Morning')

TRIM (trim_text FROM TRIM ('o' FROM 'Good Gd Mrning


string_value) Morning')

SUBSTR (string_value, m, n) SUBSTR ('Good Morning', 6, 7) Morning

LENGTH (string_value) LENGTH ('Good Morning') 12

LPAD (string_value, n, LPAD ('Good', 6, '*') **Good


pad_value)

RPAD (string_value, n, RPAD ('Good', 6, '*') Good**


pad_value)

2) Numeric Functions:
Numeric functions are used to perform operations on numbers. They accept numeric values
as input and return numeric values as output. Few of the Numeric functions are:

Function Return Value


Name

ABS (x) Absolute value of the number 'x'

CEIL (x) Integer value that is Greater than or equal to the number 'x'

FLOOR (x) Integer value that is Less than or equal to the number 'x'

TRUNC (x, y) Truncates value of number 'x' up to 'y' decimal places

ROUND (x, y) Rounded off value of the number 'x' up to the number 'y' decimal places
The following examples explains the usage of the above numeric
functions
Function Name Examples Return Value

ABS (x) ABS (1) 1


ABS (-1) -1

CEIL (x) CEIL (2.83) 3


CEIL (2.49) 3
CEIL (-1.6) -1

FLOOR (x) FLOOR (2.83) 2


FLOOR (2.49) 2
FLOOR (-1.6) -2

TRUNC (x, y) TRUNC (140.234, 2) 140.23


TRUNC (-54, 1) -54
TRUNC (5.7) 5
TRUNC (142, -1) 140

ROUND (x, y) ROUND (125.456, 1) 125.4


ROUND (125.456, 0) 125
ROUND (124.456, -1) 120

The DUAL Table :

A DUAL table is used to perform arithmetic operations. The DUAL table is provided by
oracle. It is owned by user SYS and it is available to all users. This table is useful when we
want to find out the outcome of a function and the argument is not taken from any table.

Desc DUAL;

NAME NULL? TYPE

DUMMY VARCHAR2(1)

3) DATE FUNCTIONS:

These are functions that take values that are of data type DATE as input and return values of
data types DATE, except for the MONTHS_BETWEEN function, which returns a number as
output.

Few date functions are as given below.

Function Name Return Value


ADD_MONTHS (date, n) Returns a date value after adding 'n'months to the date 'x'.

MONTHS_BETWEEN (x1, x2) Returns the number of months between dates x1 and x2.

ROUND (x, date_format) Returns the date 'x' rounded off to the nearest century,
year, month, date, hour, minute, or second as specified by
the 'date_format'.

TRUNC (x, date_format) Returns the date 'x' lesser than or equal to the nearest
century, year, month, date, hour, minute, or second as
specified by the 'date_format'.

NEXT_DAY (x, week_day) Returns the next date of the 'week_day'on or after the
date 'x' occurs.

LAST_DAY (x) It is used to determine the number of days remaining in a


month from the date 'x' specified.

SYSDATE Returns the systems current date and time.

Ex-SELECT SYSDATE FROM DUAL;

NEW_TIME (x, zone1, zone2) Returns the date and time in zone2 if date 'x' represents
the time in zone1.

The below table provides the examples for the above functions

Function Name Examples Return Value

ADD_MONTHS ( ) ADD_MONTHS ('16-Sep-81', 3) 16-Dec-81

MONTHS_BETWEEN( MONTHS_BETWEEN ('16-Sep-81', '16- 3


) Dec-81')

NEXT_DAY( ) NEXT_DAY ('01-Jun-08', 'Wednesday') 04-JUN-08

LAST_DAY( ) LAST_DAY ('01-Jun-08') 30-Jun-08

NEW_TIME( ) NEW_TIME ('01-Jun-08', 'IST', 'EST') 31-May-08

4) Conversion Functions:

These are functions that help us to convert a value in one form to another form. For Ex: a
null value into an actual value, or a value from one datatype to another datatype like NVL,
TO_CHAR, TO_NUMBER, TO_DATE.

Few of the conversion functions available in oracle are:


Function Name Return Value

TO_CHAR Converts Numeric and Date values to a character string


(number/date[,format]) value based on the format provided. It cannot be used for
calculations since it is a string value.

TO_DATE (x [, Converts a valid Numeric and Character values to a Date


date_format]) value. Date is formatted to the format specified
by 'date_format'.

NVL (x, y) If 'x' is NULL, replace it with 'y'. 'x' and 'y' must be of the
same datatype.

The below table provides the examples for the above functions

Function Examples Return Value


Name

TO_CHAR () TO_CHAR (3000, '$9999') $3000


TO_CHAR (SYSDATE, 'Day, Month YYYY') Monday, June 2008

TO_DATE () TO_DATE ('01-Jun-08') 01-Jun-08

NVL () NVL (null, 1) 1

WORKING WITH GROUP BY CLAUSE (GROUPING DATA):

The SQL GROUP BY Clause is used along with the group functions to retrieve data grouped
according to one or more columns.

Syn: SELECT <exp> from <table name> WHERE <condition> group by <col1, col2, … coln>;

For Example: If you want to know the total amount of salary spent on each department, the
query would be:

SELECT deptno, SUM (salary)


FROM employee
GROUP BY deptno;

Note: (1) The column which should be grouped by, should be there in the select list.
(2) The WHERE clause can still be used to restrict data before grouping.
(3) The WHERE clause cannot be used to restrict groups.
(4) A column alias can’t be used in a GROUP BY clause.
WORKING WITH HAVING CLAUSE:

Having clause is used to filter data based on the group functions. This is similar to WHERE
condition but is used with group functions.

Syntax: SELECT <exp> from <Table name> where <condition> group by <col1, col2, …, coln>
having <group condition>;

Example

If you want to select the department that has total salary paid for its employees
more than 25000, the sql query would be like;

SELECT dept, SUM (salary)


FROM employee
GROUP BY dept
HAVING SUM (salary) > 25000 ;

Questions:

1) Create a table having attributes fname, lname, emp_id, deptno, job, sal.

2) Insert 5 rows into the table.

Ans:

Insert into empnew values (‘Ruchi’, ‘Agarwal’, 234, 10, ‘Manager’, 30000);

1 row created.

Insert into empnew values (‘Riya’, ‘Sen’, 123, 10, ‘Sales person’, 10000);

1 row created.

Insert into empnew values (‘Piyush’, ‘Garg’, 467, 20, ‘Peon’, 1000);

1 row created.

Insert into empnew values (‘Raghu’, ‘Ram’, 892, 20, ‘Manager’, 25000);

1 row created.

Insert into empnew values (‘Ashish’, ‘Sharma’, 111, 20, ‘Sales person’, 1500);

1 row created.

3) Write a query which will display department wise sum of sal, max of sal and min of sal
and count the no. of records in each group.
4) Write a query which will display the dept wise sum of sal, max of sal, min of sal whose
max(sal) >=2000

5) Display dept wise, job wise sum of sal, max of sal of those group whose max sal is >=2000
but it will not display the info of deptno 20.

6) Display all emp names with initial letter in capital form.

7) Display the current date of your system.

8) Write a query to round off 25.434 upto 2 decimal places.

9) Find out the sign of -5.5

You might also like