*objectives : *explain & demonstrate use of expressions *list the type of sql function categories *explain & demonstrate arithmetic functions *explain & demonstrate character functions *explain & demonstrate date functions *explain & demonstrate other functions *explain & demonstrate aggregate/group functions *explain & demonstrate the group by clause
using expressions with columns
*arithmetic calculations can be done on numeric columns *aliases can be displayed in place of column or expression example : 1. list the total salary and pf of all the employees. pf is calculated as 10% of salary.
*the functions supported by oracle can be classified
into following categories :
*aggregate/group functions *arithmetic functions *character functions *date functions *general functions
*abs(n) *mod(m,n) *sign(n) *sqrt(n) *trunc(m,n) *round(m,n)
*initcap (string) *lower (string) *upper (string) *lpad (char1,n[,char2]) *rpad(char1, n [,char2]) *trim (leading or trailing or both trim_char from string_source) *substr(string, m[,n]) *instr(string, search_char,[ m, n]) *length (string) *concat(string1, string2)
*add_months (d1, n) *greatest(d1, d2,...) *least(d1,d2,...) *last_day(d) *months_between(d1, d2) *next_day (date, day) *to_date(char, f) *to_char(d,f)
sample date - `13-dec-1995’
format description example mm number of months (1-12) 12 rm roman numeral month xii mon three-letter abbreviation dec month month fully spelt out december ddd day of the year (1-366) 347 dd day of the month (1-31) 13 d day of the week (1-7) 4 dy three-letter wed abbreviation of day day day spelt out wednesday y last digit of the year 5 yy last 2 digits of the year 95 yyy last 3 digits of the year 995 yyyy full 4 digits of the year 1995
year hh, hh12 hh24 mi ss ddth
year spelt out ninteen ninety-five hour of day (1-12) 9 hour of the day 21 hours clock (0-23) minute of the hour 10 (0-59) seconds of the minute 25 (0-59) suffix to a date 13th
*examples : 1. list the employees who have joined in year 1981. 2. list the employees and the length of their service as on today. 3. list the employees who have joined company 25 years back.
*is used for handling null values *nvl function helps in substituting a value in place of a null *syntax : nvl (column, value) *example : 1. list salary, commission, gross salary using nvl for all employees. 2. list the commission received by all the employees.
*syntax: decode(col, orig1, trans1, orig2, trans2, …., default) - translates the original value in the specified column into translated value or returns default value - substitution is on value-by-value basis - actually does if-then-else test examples : *list employee details as ename, job description using following criteria
job clerk manager analyst others description adminstaff executive rndstaff same as job
summarising data: aggregate/group functions
*queries can also produce summarising results *aggregate/group functions operate upon group of
values/rows *aggregate/group functions return one value/row per group
*count(col) *count(*) *sum(col) *avg(col) *min(col) *max(col)
examples: group functions
1. list the number of employees working in the company. 2. list the total salary payable to employees. 3. list the maximum salary for employees working as a ‘salesman’.
grouping the results: group by clause
*the group by clause is used to combine a group of rows based on column(s) and/or expression(s) *syntax:
select <grouped_by_columns, group functions> from <table_name> group by <column|expression, ….>;
*group by returns a single row of summary information for each group *aggregate functions are applied to the individual groups *the column or expression on which the group is to be formed, should be selected *conditional retrieval from a grouped result is possible using having clause
examples: group by clause
1. list the department numbers and number of employees in each department. 2. list the total salaries payable to each department.
3. list the number of employees in each job in the descending order of number of jobs. 4. list the total salary and maximum job wise salary of employees working in department 20 and list only those jobs with total job wise salary greater than 5000.
more about group by clause
*group by returns a single row of summary information for each group *aggregate functions are applied to the individual groups *the column or expression on which the group is to be formed, should be selected *any column or expression in the select list that is not an aggregate function must be in the group by clause. *you cannot use the where clause to restrict groups. *you cannot use group functions in the where clause *conditional retrieval from a grouped result is possible using having clause *you use the having clause to restrict the group.
examples: group by clause
*list the department numbers and number of employees in each department. *list the total salaries payable to each department. *list the number of employees in each job in the descending order of number of jobs. *list the total salary and maximum job wise salary of employees working in department 20 and list only those jobs with total job wise salary greater than 5000.