Functions and Grouping
Functions and Grouping
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 .
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
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:
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'
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
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;
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.
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.
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
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.
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
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:
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;
Questions:
1) Create a table having attributes fname, lname, emp_id, deptno, job, sal.
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.