Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Open Database Connectivity wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Functional Database Model wikipedia , lookup
Concurrency control wikipedia , lookup
Clusterpoint wikipedia , lookup
Relational model wikipedia , lookup
Database Systems dr Grzegorz Michalski 14 May 2014 Database Systems 1/26 General Functions The following instructions work with any data type and pertain to using nulls: NVL (expr1, expr2) NVL2 (expr1, expr2, expr3) NULLIF(expr1, expr2) COALESCE (expr1, expr2, . . . , exrn) Database Systems 2/26 NVL Function Converts a null value to an actual value: Data types that can be used are date, character, and number. Data types must match: NVL(commission pct,0) NVL(hire date,’01=JAN-97’) NVL(job id, ’No Job Yet’) Database Systems 3/26 NVL, NVL2 examples Example SELECT last name, salary, NVL(commission pct, 0), (salary*12 + (12*salary*NVL(commission pct, 0)) ANNUAL SALLARY, FROM employees; Database Systems 4/26 NVL, NVL2 examples Example SELECT last name, salary, NVL(commission pct, 0), (salary*12 + (12*salary*NVL(commission pct, 0)) ANNUAL SALLARY, FROM employees; Example SELECT last name, salary, commission pct, NVL2(commission pct, ’SAL and COMM’,’SAL’) income FROM employees WHERE department id IN (40,90); Database Systems 4/26 NULLIF example Example SELECT first name, LENGTH(first name) "expr1", last name, LENGTH(last name) "expr2" NULLIF(LENGTH(first name), LENGTH(first name)) res FROM employees; Database Systems 5/26 Usign the COALESCE Function 1 The adventage of the COALESCE function over the NVL function is that the COALESCE function can take multiple alternate values. 2 If the first expression is not null, the COALESCE function returns that expression; otherwise, it does a COALESCE of the remaining expressions. Example SELECT last name, employee id, COALESCE(TO CHAR(commission pct), TO CHAR(manager id), ’No commision and no manager’) Info FROM employees; Database Systems 6/26 COALESCE exmaple Example SELECT last name, salary, commission pct, COALESCE((salary + (commission pct*salary)), salary+2000, salary) ”New Salary”) FROM employees; Database Systems 7/26 Conditional Expressions 1 2 Provide the use of the IF--THEN--ELSE logic within a SQL statement. Use a two methods: CASE expression. DECODE function. CASE expression Facilitates conditional inquiries by doing the work of an IF--THEN--ELSE: CASE expr WHEN comparison expr1 THEN return expr1 [WHEN comparison expr2 THEN return expr2 WHEN comparison exprn THEN return exprn ELSE else expr] END Database Systems 8/26 Using the CASE expression Example SELECT last name, job id, salary, CASE job id WHEN ’IT PROG’ THEN 2.50*salary WHEN ’ST CLERK’ THEN 1.20*salary WHEN ’SA REP’ THEN 1.25*salary ELSE salary END "Revised salary" FROM employees; Example SELECT last name, job id, salary, CASE WHEN salary < 5000 THEN ’Low’ WHEN salary < 10000 THEN ’Medium’ WHEN salary < 15000 THEN ’High’ ELSE ’Excellent’ END Salary FROM employees; Database Systems 9/26 DECODE Function Facilitates conditional inquiries by doing work of a CASE expression or an IF--THEN--ELSE statement DECODE(col|expression, search1, result1 [, search2, result2,. . . ] [, default]) Example SELECT last name, job id, salary, DECODE(job id, ’IT PROG’, 2.50*salary, ’ST CLERK’, 1.20*salary, ’SA REP’, 1.25*salary, salary) "Revised salary" FROM employees; Database Systems 10/26 DECODE example Example SELECT last name, salary, DECODE( TRUNC( SALARY/2000, 0), 0, 0.00, 1, 0.09, 2, 0.20, 3, 0.30, 4, 0.40, 5, 0.42, 5, 0.44, 0.45) "Tax Rate" FROM employees; Database Systems 11/26 Reporting Aggregated Data Using the Group Functions Database Systems 12/26 What are group Functions? group functions operate on sets of rows to give one result per group. Types of group functions AVG COUNT MAX MIN STDDEV VARIANCE Database Systems 13/26 Types of group functions Function AVG([DISTINCT|ALL] n) COUNT([DISTINCT|ALL] expr) MAX([DISTINCT|ALL] expr) MIN([DISTINCT|ALL] expr) STDDEV([DISTINCT|ALL] n) SUM([DISTINCT|ALL] n) VARIANCE([DISTINCT|ALL] n) Description Averange value of n, ignoring nulls Number of rows, where expr evaluates to something other than nulls Maximum value of expr ignoring nulls Minimum value of expr, ignoring nulls Standard deviation of n, ignoring nulls Sum values of n, ignoring nulls Variance of n, ignoring nulls Database Systems 14/26 Group Functions: Syntax SELECT group function(column), . . . FROM table [WHERE conditions]; Using the AVG and SUM Functions You may use AVG and SUM for numeric data. Example SELECT AVG(salary), MAX(salary), MIN(salary), SUM(salary) FROM employees WHERE job id LIKE ‘%REP%’; Database Systems 15/26 Using the MIN and MAX Functions You can use the MIN and MAX functions for numeric, character, and date data types. Example SELECT MIN(hire date), MAX(hire date), FROM employees Example SELECT MIN(last name), MAX(last name), FROM employees Database Systems 16/26 Using the COUNT Function Example COUNT(*) returns the number of rows in a table: SELECT COUNT(*) FROM employees WHERE department id = 50 Example COUNT(expr) returns the number of rows with non–null values for expr: SELECT COUNT(commission pct) FROM employees WHERE department id = 50 Database Systems 17/26 Using the DISTINCT Keyword COUNT(DISTINCT expr) retuns the number of distinct non–null values of expr To display the number of distinct department values in the EMPLOYEES table: Example SELECT COUNT(DISTINCT department id) FROM employees; Database Systems 18/26 Group Functions and Null Values All group functions ignore null values in column, The NVL function forces group functions to include null values. Example SELECT AVG(commision pct), AVG(NVL(commision pct, 0)) FROM employees; Database Systems 19/26 Creating Groups of Data: GROUP BY Clause Syntax You can divide rows in a table into smaller groups by using the GROUP BY clause: SELECT column, gropu function(column) FROM table [WHERE condition] [GROUP BY group by expression] [ORDER BY column] Database Systems 20/26 Using the GROUP BY Clause All the columns in the SELECT list that are not in group functions must be in the GROUP BY clause. Example SELECT department id, AVG (salary) FROM employees GROUP BY department id; Database Systems 21/26 Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. In the ORDER BY clause you may use a group function. Example SELECT AVG(salary) FROM employees GROUP BY department id; Example SELECT department id, AVG(salary) FROM employees GROUP BY department id ORDER BY AVG(salary) Database Systems 22/26 Using the GROUP BY Clause on Multiple Columns Example SELECT department id, job id, SUM(salary) FROM emplyees WHERE department id > 20 GROUP BY department id, job id ORDER BY department id; Database Systems 23/26 Illegal Queries Using Group Functions Any column or expression in the SELECT list that is not an aggregate function must be in the GROUP BY clause. If you use a aggregate function, you must use a GROUP BY clause. Example SELECT department id, COUNT(last name) FROM employees; Example SELECT department id, job id, COUNT(last name) FROM employees GROUP BY department id; Database Systems 24/26 Restricting Group Results with the HAVING Clause When you use the HAVING clause, the Oracle server restricts groups as follows: Rows are grouped. The group function is applied. Groups matching the HAVING clause are displayed. SELECT column, gropu function(column) FROM table [WHERE condition] [GROUP BY group by expression] [HAVING group condition] [ORDER BY column] Database Systems 25/26 Using the HAVING clause Example SELECT department id, MAX(salary) FROM employees GROUP BY department id HAVING MAX(salary) > 10000; Example SELECT job id, SUM(salary) payroll FROM employees WHERE job id NOT LIKE ’%REP%’ GROUP BY job id HAVING SUM(salary) > 12500 ORDER BY SUM(salary); Database Systems 26/26