1 / 96

Single-Row Functions

Single-Row Functions. Input. Output. arg 1. arg 2. Result value. arg n. SQL Functions. Function. Function performs action. Two Types of SQL Functions. Functions. Multiple-row functions. Single-row functions. Single-Row Functions. Manipulate data items

Download Presentation

Single-Row Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Single-Row Functions

  2. Input Output arg 1 arg 2 Result value arg n SQL Functions Function Function performs action

  3. Two Types of SQL Functions Functions Multiple-row functions Single-row functions

  4. Single-Row Functions • Manipulate data items • Accept arguments and return one value • May modify the data type • Can be nested function_name (column|expression, [arg1, arg2,...])

  5. Single-Row Functions Character Number Single-row functions Conversion Date

  6. Character Functions Character functions Character manipulation functions Case conversion functions LOWER UPPER SUBSTRING LEN LTRIM RTRIM LEFT RIGHT

  7. Case Conversion Functions • Convert case for character strings Function Result sql course SQL COURSE LOWER('SQL Course') UPPER('SQL Course')

  8. SELECT empno, ename, deptno FROM emp WHERE LOWER(ename) = 'blake' EMPNO ENAME DEPTNO --------- ---------- --------- 7698 BLAKE 30 Using Case Conversion Functions • Display the employee number, name, and department number for employee Blake. SELECT empno, ename, deptno FROM emp WHERE ename = 'blake' no rows selected

  9. Character Manipulation Functions • Manipulate character strings Function Result Str 6 String String Str ing SUBSTRING('String',1,3) LEN(‘String’) LTRIM(‘ String ’) RTRIM(‘ String ’) LEFT(‘String’, 3) RIGHT(‘String’, 3)

  10. Using the Character Manipulation Functions SELECT SUBSTRING('String',1,3) substr, LEN('String') Lenstr, RTRIM(' string ') Rtrimstr, LTRIM(' string ') Ltrimstr, LEFT('String',3) Leftstr, RIGHT('String',3) Rightstr

  11. Using the ROUND Function SELECT ROUND(45.923,2), ROUND(45.923,0), ROUND(45.923,-1) ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1) --------------- -------------- ----------------- 45.92 46 50

  12. Using the FLOOR,CEILING Functions SELECT FLOOR(123.45), FLOOR(-123.45), FLOOR(123.95) SELECT CEILING(123.45), CEILING(-123.45), CEILING(0.0)

  13. Aggregating Data Using Group Functions

  14. What Are Group Functions? EMP • Group functions operate on sets of rows to give one result per group. DEPTNO SAL --------- --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 MAX(SAL) --------- 5000 “maximum salary in the EMP table”

  15. Types of Group Functions • AVG • COUNT • MAX • MIN • SUM

  16. Using Group Functions SELECT [column,] group_function(column) FROM table [WHERE condition] [GROUP BY column] [ORDER BY column]

  17. Using AVG and SUM Functions • You can use AVG and SUM for numeric data. SELECT AVG(sal), MAX(sal), MIN(sal), SUM(sal) FROM emp WHERE job LIKE 'SALES%' AVG(SAL) MAX(SAL) MIN(SAL) SUM(SAL) -------- --------- --------- --------- 1400 1600 1250 5600

  18. Using MIN and MAX Functions • You can use MIN and MAX for any datatype. SELECT MIN(hiredate), MAX(hiredate) FROM emp MIN(HIRED MAX(HIRED --------- --------- 17-DEC-80 12-JAN-83

  19. Using the COUNT Function • COUNT(*) returns the number of rows in a table. SELECT COUNT(*) FROM emp WHERE deptno = 30 COUNT(*) --------- 6

  20. Using the COUNT Function • COUNT(expr) returns the number of nonnull rows. SELECT COUNT(comm) FROM emp WHERE deptno = 30 COUNT(COMM) ----------- 4

  21. Group Functions and Null Values • Group functions ignore null values in the column. SELECT AVG(comm) FROM emp AVG(COMM) --------- 550

  22. Using the ISNULL Function with Group Functions • The ISNULL function forces group functions to include null values. SELECT AVG(ISNULL(comm,0)) COMMISSION FROM emp COMMISSION ---------- 157.14286

  23. 2916.6667 2175 1566.6667 Creating Groups of Data EMP DEPTNO SAL --------- --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 DEPTNO AVG(SAL) ------- --------- 10 2916.6667 20 2175 30 1566.6667 “averagesalary in EMPtable for each department”

  24. Creating Groups of Data: GROUP BY Clause • Divide rows in a table into smaller groups by using the GROUP BY clause. SELECT column, group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [ORDER BY column]

  25. Using the GROUP BY Clause • All columns in the SELECT list that are not in group functions must be in the GROUP BY clause. SELECT deptno, AVG(sal) FROM emp GROUP BY deptno DEPTNO AVG(SAL) --------- --------- 10 2916.6667 20 2175 30 1566.6667

  26. Using the GROUP BY Clause • The GROUP BY column does not have to be in the SELECT list. SELECT AVG(sal) FROM emp GROUP BY deptno AVG(SAL) --------- 2916.6667 2175 1566.6667

  27. Grouping by More Than One Column EMP DEPTNO JOB SAL --------- --------- --------- 10 MANAGER 2450 10 PRESIDENT 5000 10 CLERK 1300 20 CLERK 800 20 CLERK 1100 20 ANALYST 3000 20 ANALYST 3000 20 MANAGER 2975 30 SALESMAN 1600 30 MANAGER 2850 30 SALESMAN 1250 30 CLERK 950 30 SALESMAN 1500 30 SALESMAN 1250 DEPTNO -------- 10 10 10 20 20 20 30 30 30 JOB SUM(SAL) --------- --------- CLERK 1300 MANAGER 2450 PRESIDENT 5000 ANALYST 6000 CLERK 1900 MANAGER 2975 CLERK 950 MANAGER 2850 SALESMAN 5600 “sum salaries in the EMP tablefor each job, grouped by department”

  28. Using the GROUP BY Clause on Multiple Columns SELECT deptno, job, sum(sal) FROM emp GROUP BY deptno, job DEPTNO JOB SUM(SAL) --------- --------- --------- 10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 20 ANALYST 6000 20 CLERK 1900 ... 9 rows selected.

  29. 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. SELECT deptno, COUNT(ename) FROM emp Column missing in the GROUP BY clause Server: Msg 8118, Level 16, State 1, Line 1 Column 'emp.DEPTNO' is invalid in the select list because it is not contained in an aggregate function and there is no GROUP BY clause.

  30. Illegal Queries Using Group Functions • You cannot use the WHERE clause to restrict groups. • You use the HAVING clause to restrict groups. SELECT deptno, AVG(sal) FROM emp WHERE AVG(sal) > 2000 GROUP BY deptno Cannot use the WHERE clause to restrict groups Server: Msg 147, Level 15, State 1, Line 3 An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.

  31. 5000 3000 2850 Excluding Group Results EMP DEPTNO SAL --------- --------- 10 2450 10 5000 10 1300 20 800 20 1100 20 3000 20 3000 20 2975 30 1600 30 2850 30 1250 30 950 30 1500 30 1250 “maximumsalaryper department greater than$2900” DEPTNO MAX(SAL) --------- --------- 10 5000 20 3000

  32. Excluding Group Results: HAVING Clause • Use the HAVING clause to restrict groups • Rows are grouped. • The group function is applied. • Groups matching the HAVING clause are displayed. SELECT column, group_function FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column]

  33. Using the HAVING Clause SELECT deptno, max(sal) FROM emp GROUP BY deptno HAVING max(sal)>2900 DEPTNO MAX(SAL) --------- --------- 10 5000 20 3000

  34. Using the HAVING Clause SELECT job, SUM(sal) PAYROLL FROM emp WHERE job NOT LIKE 'SALES%' GROUP BY job HAVING SUM(sal)>5000 ORDER BY SUM(sal) JOB PAYROLL --------- --------- ANALYST 6000 MANAGER 8275

  35. Nesting Group Functions • Display the maximum average salary. SELECT max(avg(sal)) FROM emp GROUP BY deptno MAX(AVG(SAL)) ------------- 2916.6667

  36. Summary SELECT column, group_function(column) FROM table [WHERE condition] [GROUP BY group_by_expression] [HAVING group_condition] [ORDER BY column] • Order of evaluation of the clauses: • WHERE clause • GROUP BY clause • HAVING clause

  37. Displaying Data from Multiple Tables

  38. Objectives • After completing this lesson, you should be able to do the following: • Write SELECT statements to access data from more than one table using equality and nonequality joins • View data that generally does not meet a join condition by using outer joins • Join a table to itself

  39. Obtaining Data from Multiple Tables EMP DEPT EMPNO ENAME ... DEPTNO------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO ... 14 rows selected.

  40. What Is a Join? • Use a join to query data from more than one table. • Write the join condition in the WHERE clause. • Prefix the column name with the table name when the same column name appears in more than one table. SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2

  41. Cartesian Product • A Cartesian product is formed when: • A join condition is omitted • A join condition is invalid • All rows in the first table are joined to all rows in the second table • To avoid a Cartesian product, always include a valid join condition in a WHERE clause.

  42. “Cartesianproduct: 14*4=56 rows” Generating a Cartesian Product DEPT (4 rows) EMP (14 rows) DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON EMPNO ENAME ... DEPTNO------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10 ENAME DNAME ------ ---------- KING ACCOUNTING BLAKE ACCOUNTING ... KING RESEARCH BLAKE RESEARCH ... 56 rows selected.

  43. Types of Joins • Equijoin • Non-equijoin • Outer join • Self join

  44. Foreign key Primary key What Is an Equijoin? EMP DEPT EMPNO ENAME DEPTNO ------ ------- ------- 7839 KING 10 7698 BLAKE 30 7782 CLARK 10 7566 JONES 20 7654 MARTIN 30 7499 ALLEN 30 7844 TURNER 30 7900 JAMES 30 7521 WARD 30 7902 FORD 20 7369 SMITH 20 ... 14 rows selected. DEPTNO DNAME LOC ------- ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON 4 rows selected.

  45. Retrieving Records with Equijoins SELECT emp.empno, emp.ename, emp.deptno, dept.deptno, dept.loc FROM emp, dept WHERE emp.deptno=dept.deptno EMPNO ENAME DEPTNO DEPTNO LOC ----- ------ ------ ------ --------- 7839 KING 10 10 NEW YORK 7698 BLAKE 30 30 CHICAGO 7782 CLARK 10 10 NEW YORK 7566 JONES 20 20 DALLAS ... 14 rows selected.

  46. “salary in the EMP table is between low salary and high salary in the SALGRADE table” Non-Equijoins EMP SALGRADE EMPNO ENAME SAL ------ ------- ------ 7839 KING 5000 7698 BLAKE 2850 7782 CLARK 2450 7566 JONES 2975 7654 MARTIN 1250 7499 ALLEN 1600 7844 TURNER 1500 7900 JAMES 950 ... 14 rows selected. GRADE LOSAL HISAL ----- ----- ------ 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999

  47. Retrieving Records with Non-Equijoins SELECT e.ename, e.sal, s.grade FROM emp e, salgrade s WHERE e.sal BETWEEN s.losal AND s.hisal ENAME SAL GRADE ---------- --------- --------- JAMES 950 1 SMITH 800 1 ADAMS 1100 1 ... 14 rows selected.

  48. Types of Join Inner Join Outer Join Self Join

  49. Inner Join In this type of join, Records in the joined tables must have the same value for fields that are joined. For example List the employees who work in ‘ACCOUNTING’ department.

  50. Example SELECT ename, dname FROM emp INNER JOIN dept ON emp.deptno=dept.deptno WHERE UPPER(dname)='ACCOUNTING'

More Related