Aggregating Data Using Group Functions



Similar documents
Subqueries Chapter 6

Displaying Data from Multiple Tables. Chapter 4

Displaying Data from Multiple Tables

Oracle/SQL Tutorial 1

Appendix A Practices and Solutions

Chapter 1. Writing Basic. SQL Statements

Oracle SQL. Course Summary. Duration. Objectives

Retrieval: Multiple Tables and Aggregation

Producing Readable Output with SQL*Plus

Relational Algebra. Query Languages Review. Operators. Select (σ), Project (π), Union ( ), Difference (-), Join: Natural (*) and Theta ( )

Examine the structure of the EMPLOYEES table: EMPLOYEE_ID NUMBER Primary Key FIRST_NAME VARCHAR2(25) LAST_NAME VARCHAR2(25)

RDBMS Using Oracle. Lecture Week 7 Introduction to Oracle 9i SQL Last Lecture. kamran.munir@gmail.com. Joining Tables

Oracle Database: SQL and PL/SQL Fundamentals

3.GETTING STARTED WITH ORACLE8i

Oracle Database: SQL and PL/SQL Fundamentals

Oracle Database: SQL and PL/SQL Fundamentals NEW

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Conversion Functions

Mini User's Guide for SQL*Plus T. J. Teorey

AN INTRODUCTION TO THE SQL PROCEDURE Chris Yindra, C. Y. Associates

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Programming with SQL

Retrieving Data Using the SQL SELECT Statement. Copyright 2006, Oracle. All rights reserved.

Oracle Database 12c: Introduction to SQL Ed 1.1

Financial Data Access with SQL, Excel & VBA

Structured Query Language (SQL)

REPORT GENERATION USING SQL*PLUS COMMANDS

Creating QBE Queries in Microsoft SQL Server

Chapter 1 Overview of the SQL Procedure

Database Applications Microsoft Access

Database Query 1: SQL Basics

Data Models and Database Management Systems (DBMSs) Dr. Philip Cannata

D B M G Data Base and Data Mining Group of Politecnico di Torino

Relational Database: Additional Operations on Relations; SQL

SQL Introduction Chapter 7, sections 1 & 4. Introduction to SQL. Introduction to SQL. Introduction to SQL

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

P_Id LastName FirstName Address City 1 Kumari Mounitha VPura Bangalore 2 Kumar Pranav Yelhanka Bangalore 3 Gubbi Sharan Hebbal Tumkur

Netezza SQL Class Outline

Performing Queries Using PROC SQL (1)

Unit 10: Microsoft Access Queries

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Oracle Database: SQL and PL/SQL Fundamentals NEW

Handling Exceptions. Copyright 2006, Oracle. All rights reserved. Oracle Database 10g: PL/SQL Fundamentals 8-1

SQL SELECT Query: Intermediate

Using TimesTen between your Application and Oracle. between your Application and Oracle. DOAG Conference 2011

Introduction to Microsoft Jet SQL

Writing Control Structures

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

GET DATA FROM MULTIPLE TABLES QUESTIONS

GUJARAT TECHNOLOGICAL UNIVERSITY

SQL - QUICK GUIDE. Allows users to access data in relational database management systems.

Objectives. Oracle SQL and SQL*PLus. Database Objects. What is a Sequence?

How To Create A Table In Sql (Ahem)

Oracle For Beginners Page : 1

Advanced SQL. Jim Mason. Web solutions for iseries engineer, build, deploy, support, train

Database CIS 340. lab#6. I.Arwa Najdi

Kaseya 2. Quick Start Guide. for VSA 6.3

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

SQL. Short introduction

9.1 SAS. SQL Query Window. User s Guide

Oracle Database 10g: Introduction to SQL

Database Programming with PL/SQL: Learning Objectives

Ad Hoc Advanced Table of Contents

2. DECODE Function, CASE Expression

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Data Structure: Relational Model. Programming Interface: JDBC/ODBC. SQL Queries: The Basic From

Oracle Database 11g SQL

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING DR.NAVALAR NEDUNCHEZHIAYN COLLEGE OF ENGINEERING, THOLUDUR , CUDDALORE DIST.

Access Queries (Office 2003)

Handling Exceptions. Schedule: Timing Topic. 45 minutes Lecture 20 minutes Practice 65 minutes Total

Oracle Database: Introduction to SQL

Oracle Database: Introduction to SQL

Database Administration with MySQL

Using AND in a Query: Step 1: Open Query Design

Introduction to Proc SQL Steven First, Systems Seminar Consultants, Madison, WI

SQL Tuning Proven Methodologies

Oracle Database: Introduction to SQL

Maximizing Materialized Views

Handling Missing Values in the SQL Procedure


SECTION 3 LESSON 1. Destinations: What s in My Future?

Week 4 & 5: SQL. SQL as a Query Language

Introduction to SQL: Data Retrieving

There are five fields or columns, with names and types as shown above.

Inquiry Formulas. student guide

Displaying Data from Multiple Tables

University of Massachusetts Amherst Department of Computer Science Prof. Yanlei Diao

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Katie Minten Ronk, Steve First, David Beam Systems Seminar Consultants, Inc., Madison, WI

The Power of DECODE()

- Eliminating redundant data - Ensuring data dependencies makes sense. ie:- data is stored logically

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Microsoft Access 3: Understanding and Creating Queries

VARRAY AND NESTED TABLE

Comp 5311 Database Management Systems. 3. Structured Query Language 1

Transcription:

Aggregating Data Using Group Functions Objectives Capter 5 After completing this lesson, you should be able to do the following: Identify the available group functions Describe the use of group functions Group data using the GROUP BY clause Include or exclude grouped rows by using the HAVING clause 1

What Are Group Functions? Group functions operate on sets of rows to give one result per group. Group Functions Unlike single-row functions, group functions operate on sets of rows to give one result per group. These sets may be the whole table or the table split into groups. Types of Group Functions AVG COUNT MAX MIN STDDEV SUM VARIANCE 2

Using Group Functions SELECT [column, ] group_function(column) FROM table [WHERE condition] [GROUP BY column] [ORDER BY colunın] ; Guidelines for Using Group Functions DISTINCT makes the function consider only nonduplicate values: ALL makes it consider evenvalue including duplicates. The default is ALL and therefore does not need to be specified. The datatypes for the arguments may be CHAR, VARCHAR2 NUMBER, or DATE where exprs listed. All group functions except COUNT(*) ignore null values. To substitute a value for null values, use the NVL function. The Oracle Server implicitly sorts the result set in ascending order when using a GROUP BY clause To override this default ordering. DESC can be used in an ORDER BY clause. 3

Using AVG and SUM Functions You can use AVG and SUM for numeric data. SELECT AVG(sal), SUM(sal) ; AVG(SAL) SUM(SAL) 2073,21429 29025 Group Functions You can use AVG, SUM. MIN, and MAX functions against columns that can store numeric data. The example on the slide displays the average, highest, lowest, and sum of monthly salaries for all salespeople. 4

Using MIN and MAX Functions You can use MIN and MAX for any datatype. Using MIN, MAX for numeric data types: SELECT min(sal), max(sal), ; MIN(SAL) MAX(SAL) 800 5000 5

Using MIN and MAX Functions You can use MIN and MAX for date datatype. Group Functions (continued) You can use MAX and MIN functions for any datatype. The slide example displays the most junior and most senior employee. SELECT MIN(hiredate), MAX(hiredate) ; MIN(HIREDA 17/12/1980 12/01/1983 MAX(HIREDA 6

Using MIN and MAX Functions You can use MIN and MAX for char datatype. Group Functions (continued) The following example displays the employee name that is first and the employee name that is the last in an alphabetized list of all employees. SELECT MIN(ename), MAX(ename) ; ADAMS MIN(ENAME) WARD MAX(ENAME) 7

Using the COUNT Function COUNT(*) returns the number of rows in a table. SELECT COUNT(*) ; COUNT(*) 14 The COUNT Function The COUNT function has two formats: COUNT(*) COUNT(expr) COUNT(*) returns the number of rows in a table, including duplicate rows and rows containing null values in any of the columns. If a WHERE clause is included in the SELECT statement COUNT(*) returns the number of rows that satisfies the condition in the WHERE clause. In contrast. COUNT(expr) returns the number of nonnull rows in the column identified by expr. 8

COUNT(expr) returns the number of non NULL rows in a table, i.e. number of rows that satisfies expr. SELECT COUNT(deptno) ; COUNT(DEPTNO) 14 The COUNT Function (continued) The slide example displays the number of employees in department 30. SELECT COUNT(*) WHERE deptno = 30 ; COUNT(*) 6 9

Using the COUNT Function COUNT(expr) returns the number of non NULL rows satisfying expr in a table. SELECT COUNT(comm) ; COUNT(COMM) 4 The COUNT Function (continued) COUNT(expr) returns the number of nonnull rows in the column identified by expr. The slide example displays the number of employees in departme'nt 30 who can earn a commission. Notice that the result gives the total number of rows to be four because two employees in department 30 cannot earn a commission and contain a null value in the COMM column. SELECT COUNT(comm) WHERE deptno = 30 ; COUNT(COMM) 4 SELECT COUNT(comm) WHERE deptno = 20 ; COUNT(COMM) 0 10

SELECT COUNT(*) WHERE comm IS NULL ; COUNT(*) 10 11

Using the COUNT Function COUNT( expr) returns the number of rows in a table that satisfies the expr. SELECT COUNT(DISTINCT deptno) ; COUNT(DISTINCT deptno) 3 12

Group Functions and Null Values Group functions ignore null values in the column. SELECT AVG(comm) ; AVG(comm) 550 Group Functions and Null Values All group functions except COUNT (*) ignore null values in the column. In the slide example, the average is calculated based only on the rows in the table where a valid value is stored in the COMM column. The average is calculated as total commission being paid to all employees divided by the number of employees receiving commission (4). 13

Using the NVL Function with Group Functions The NVL function forces group functions to include null values. SELECT AVG(NVL (comm,0)) ; AVG(NVL(comm, 0)) 157,142857 Group Functions and Null Values (continued) The NVL function forces group functions to include null values. In the slide example, the average is calculated based on all rows in the table regardless of whether null values are stored in the COMM column. The average is calculated as total commission being paid to all employees divided by the total number of employees in the company (14). 14

Creating Groups of Data SELECT deptno, AVG(sal) GROUP BY deptno ; DEPTNO AVG(SAL) 30 1566,66667 20 2175 10 2916,66667 Groups of Data Until now. all group functions have treated the table as one large group of information. At times, you need to divide the table of information into smaller groups. This can be done by using the GROUP BY clause. 15

Creating Groups of Data: GROUP BY Clause SELECT column, group_function (column) FROM table [WHERE condition] [GROUP BY group by] [ORDER BY column]; Divide rows in a table into smaller groups by using the GROUP BY clause. The GROUP BY Clause You can use the GROUP BY clause to divide the rows in a table into groups. You can then use the group functions to return summon' information for each group. In the syntax: group by expression specifies columns whose values determine the basis for grouping rows Guidelines If you include a group function in a SELECT clause, you cannot select individual results as well unless the individual column appears in the GROUP BY clause. You will receive an error-message if you fail to include the column list. Using a WHERE clause, you can preexclude rows before dividing them into groups. You must include the columns in the GROUP BY clause. You cannot use the column alias in the GROUP BY clause. 16

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) GROUP BY deptno ; DEPTNO AVG(SAL) 30 1566,66667 20 2175 10 2916,66667 The GROUP BY Clause (continued) When using the GROUP BY clause, make sure that all columns in the SELECT list that are not in the group functions are included in the GROUP BY clause. The example on the slide displays the department number and the average salary for each department. Here is how this SELECT statement, containing a GROUP BY clause, is evaluated: The SELECT clause specifies the columns to be retrieved: Department number column in the EMP table The average of all the salanes in the group you specified in the GROUP BY clause The FROM clause specifies the tables that the database must access: the EMP table. The WHERE clause specifies the rows to be retrieved. Since there is no WHERE clause, bv default all rows are retrieved. 17

Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. SELECT deptno, AVG(sal) GROUP BY deptno ORDER BY AVG(sal) ; DEPTNO AVG(SAL) 30 1566,66667 20 2175 10 2916,66667 18

Creating Groups of Data SELECT deptno, AVG(sal) GROUP BY deptno; DEPTNO AVG(SAL) 30 1566,66667 20 2175 10 2916,66667 19

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) GROUP BY deptno; DEPTNO AVG(SAL) 30 1566,66667 20 2175 10 2916,66667 20

Using the GROUP BY Clause The GROUP BY column does not have to be in the SELECT list. SELECT AVG(sal) GROUP BY deptno; AVG(sal) 1566,66667 2175 2916,66667 21

SELECT FROM deptno, AVG(sal) emp GROUP BY deptno ORDER BY AVG(sal) ; DEPTNO AVG(SAL) 30 1566,66667 20 2175 10 2916,66667 22

Grouping by More Than One Column SELECT deptno, job, sal ORDER BY deptno ; DEPTNO JOB SAL 14 rows selected. 10 MANAGER 2450 10 PRESIDENT 5000 10 CLERK 1300 20 MANAGER 2975 20 ANALYST 3000 20 CLERK 1100 20 CLERK 800 20 ANALYST 3000 30 SALESMAN 1250 30 SALESMAN 1500 30 SALESMAN 1600 30 CLERK 950 30 MANAGER 2850 30 SALESMAN 1250 23

Grouping by More Than One Column SELECT deptno, job, SUM(sal) GROUP BY deptno, job ORDER BY deptno ; DEPTNO JOB SUM(SAL) 10 CLERK 1300 10 MANAGER 2450 10 PRESIDENT 5000 20 ANALYST 6000 20 CLERK 1900 20 MANAGER 2975 30 CLERK 950 30 MANAGER 2850 30 SALESMAN 5600 9 rows selected. Groups Within Groups Sometimes there is a need to see results for groups within groups. The slide shows a report that displays the total salary being paid to each job title, within each department. The EMP table is grouped first by department number, and within that grouping, it is grouped by job title. For example, the two clerks in department 20 are grouped together and a single result (total salary) is produced for all salespeople within the group. 24

Grouping by More Than One Column SELECT deptno, job, AVG(sal), MAX(sal) GROUP BY deptno, job; DEPTNO JOB AVG(SAL) MAX(SAL) 9 rows selected. 20 CLERK 950 1100 30 SALESMAN 1400 1600 20 MANAGER 2975 2975 30 CLERK 950 950 10 PRESIDENT 5000 5000 30 MANAGER 2850 2850 10 CLERK 1300 1300 10 MANAGER 2450 2450 20 ANALYST 3000 3000 Groups Within Groups Sometimes there is a need to see results for groups within groups. The slide shows a report that displays the total salary being paid to each job title, within each department. The EMP table is grouped first by department number, and within that grouping, it is grouped by job title. For example, the two clerks in department 20 are grouped together and a single result (total salary) is produced for all salespeople within the group. 25

Using the GROUP BY Clause on Multiple Columns SELECT deptno, job, sum(sal) GROUP BY deptno, job; DEPTNO JOB SUM(SAL) 20 CLERK 1900 30 SALESMAN 5600 20 MANAGER 2975 30 CLERK 950 10 PRESIDENT 5000 30 MANAGER 2850 10 CLERK 1300 10 MANAGER 2450 20 ANALYST 6000 9 rows selected. Groups Within Groups (continued) You can return summary results for groups and subgroups by listing more than one GROUP BY column. You can determine the default sort order of the results by the order of the columns in the GROUP BY clause. Here is how the SELECT statement on the slide, containing a GROUP BY clause, is evaluated: The SELECT clause specifies the column to be retrieved: o Department number in the EMP table o Job title in the EMP table o The sum of all the salaries in the group that you specified in the GROUP BY clause The FROM clause specifies the tables that the database must access: the EMP table. The GROUP BY clause specifies how you must group the rows: 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. SELECT deptno, COUNT(ename) ; SELECT deptno, COUNT(ename) * ERROR at line 1: ORA-00937: not a single-group group function Illegal Queries Using Group Functions Whenever you use a mixture of individual items (DEPTNO) and group functions (COUNT) in the same SELECT statement, you must include a GROUP BY clause that specifies the individual items (in this case. DEPTNO). If the GROUP BY clause is missing, then the error message "not a single-group group function'" appears and an asterisk (*) points to the offending column. You can correct the error on the slide by adding the GROUP BY clause. SELECT deptno, COUNT(ename) FROM emp GROUP BY deptno ; DEPTNO COUNT(ENAME) 30 6 20 5 10 3 27

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) WHERE AVG(sal) > 2000 GROUP BY deptno; WHERE AVG(sal) > 2000 * ERROR at line 3: ORA-00934: group function is not allowed here SELECT deptno, AVG(sal) GROUP BY deptno HAVING AVG(sal) > 2000; DEPTNO AVG(SAL) 20 2175 10 2916,66667 28

Correct form: SELECT deptno, AVG(sal) GROUP BY deptno HAVING AVG(sal) > 2000; DEPTNO AVG(SAL) 20 2175 10 2916,66667 Illegal Queries Using Group Functions (continued) The WHERE clause cannot be used to restrict groups. The SELECT statement on the slide results in an error because it uses the WHERE clause to restrict the display of average salaries of those departments that have an average salary greater than $2000. You can correct the slide error by using the HAVING clause to restrict groups. 29

Excluding Group Results Restricting Group Results In the same way that you use the WHERE clause to restrict the rows that you select, you use the HAVING clause to restrict groups. To find the maximum salary of each department, but show only the departments that have a maximum salary of more than $2900. you need to do the following: Find the average salary for each department by grouping by department number. Restrict the groups to those departments with a maximum salary greater than $2900. SELECT ; DEPTNO deptno, sal SAL 20 800 30 1600 14 rows selected. SELECT deptno, MAX(sal) GROUP BY deptno; DEPTNO MAX(SAL) 30 2850 20 3000 10 5000 30

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]; The HAVING Clause You use the HAVING clause to specify which groups are to be displayed. Therefore, you further restrict the groups on the basis of aggregate information. In the syntax: group condition restricts the groups of rows returned to those groups for which the specified condition is TRUE The Oracle Server performs the following steps when you use the HAVING clause: Rows are grouped. The group function is applied to the group. The groups that match the criteria in the HAVING clause are displayed. The HAVING clause can precede the GROUP BY clause, but it is recommended that you place the j GROUP BY clause first because it is more logical. Groups are formed and group functions are calculated before the HAVING clause is applied to the groups in the SELECT list. 31

Using the HAVING Clause Excluding Group Results SELECT deptno, max(sal) GROUP BY deptno HAVING max(sal) > 2900; DEPTNO MAX(SAL) 20 3000 10 5000 The HAVING Clause (continued) The slide example displays department numbers and maximum salary for those departments whose maximum salary is greater than $2900. You can use the GROUP BY clause without using a group function in the SELECT list. If you restrict rows based on the result of a group function, you must have a GROUP BY clause as well as the HAVING clause. The following example displays the department numbers and average salary for those departments whose maximum salary is greater than $2900: 32

Restricting Group Results in the same way that you use the WHERE clause to restrict the rows that you select, you use the HAVING clause to restrict groups. To find the maximum salary of each department, but show only the departments that have a maximum salary of more than $2900, you need to do the follovving; Find the average salany for each department by grouping by department number. Restrict the groups to those departments with maximum salary greater than $2900. 33

Using the HAVING Clause SELECT job, SUM (sal) Ücretler WHERE job NOT LIKE 'SALES%' GROUP BY job HAVING SUM(sal)>5000 ORDER BY SUM (sal) ; JOB ÜCRETLER ANALYST 6000 MANAGER 8275 The HAVING Clause (continued) The slide example displays the job title and total monthly salary for each job title with a total payroll exceeding $5000. The example excludes salespeople and sorts the list by the total monthly salary. 34

Nesting Group Functions Display the maximum average salary. SELECT max(avg (sal) ) GROUP BY deptno; MAX(AVG(SAL)) 2916,66667 Nesting Group Functions Group functions can be nested to a depth of two. The slide example displays the maximum average salary. 35

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 ciause HAVING clause Summary Seven group functions are available in SQL. AVG COUNT MAX MIN SUM STDDEV VARIANCE 36

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 FROM [WHERE [GROUP BY column, group_ function table condition] group_by_expression] [ HAVING group_condition ] [ORDER BY column]; The HAVING Clause You use the HAVING clause to specify to which groups are to be displayed. Therefore, you further restrict the groups on the basis of aggregate information in the syntax. group condition restricts the groups of rows returned to those groups for which the specified condition is TRUE. The Oracle Server performs the following steps when you use the HAVING clause: Rows are grouped. The group function is applied to the group. The groups that match the criteria in the HAVING clause are displayed. The HAVING clouse can precede the GROUP BY chuse, but it is recommended that you place the GROUP BY clause first because it is more logical. Groups are formed and group functions are 37

Practice 5 1. Groups functions work across many rows to produce one result per group. TRUE / FALSE 2. Group functions include NULLs in calculations. TRUE / FALSE 3. The WHERE clause restricts rows prior to inclusion in a group calculations. TRUE / FALSE 4. Display the hihhest, lowest, sum, and average salary of all employees. Label the columns Maximum, Minimum, Sum,, and Average, respectively. Round your results to the nearest whole number. Save your SQL statement in a file called p5q4.sql. SELECT MAX(sal) MAximum, Min(sal) Minimum, SUM(sal) SUM, ROUND(AVG(sal),0) Average ; MAXIMUM MINIMUM SUM AVERAGE 5000 800 29025 2073 1 rows returned in 0,00 seconds 5. 38

8. 39

Exercices Practice 5 (continued) If you have time, complete the following exercises. 9 Display the manager number and the salary of the lowest paid employee for that manager. Exclude any one whose manager is not known. Exclude any groups where the minimum salary is less than $1000. Sort the output in descending order of salary. SELECT mgr, MIN(sal) WHERE mgr IS NOT NULL GROUP BY mgr HAVING MIN(sal) > 1000 ORDER BY MIN(sal) DESC; MGR MIN(SAL) 7566 3000 7839 2450 7782 1300 7788 1100 4 rows returned in 0,00 seconds CSV Export 40

10. Write a query to display the department name, location name, number of employees, and the average salary for all employees in that department. Label the columns dname, loc, Number of People, and Salary, respectively. Round the average salary to two decimal places. Select d.dname, d.loc, COUNT(e.empno) ĐşçiSayısı, ROUND(AVG(e.sal),2) OrtalamaÜcret FROM dept d, emp e WHERE d.deptno = e.deptno GROUP BY d.dname, d.loc; DNAME LOC ISÇISAYISI ORTALAMAÜCRET RESEARCH DALLAS 5 2175 SALES CHICAGO 6 1566,67 ACCOUNTING NEW YORK 3 2916,67 3 rows returned in 0,00 seconds 41

11. Create a query that will display the total number of employees and of the total number of employees who were hired in 1980, 1981, 1982, and 1983. Give appropriate column headings. Solution 11a SELECT TO_CHAR(hiredate, 'YY') "Đşe Giriş Yılı", COUNT(*) ĐşeGirenSayısı GROUP BY TO_CHAR(hiredate, 'YY') ORDER BY TO_CHAR(hiredate, 'YY') ; Ise Giris Yili 80 1 81 10 82 2 83 1 4 rows returned in 0,00 seconds ISEGIRENSAYISI 42

Solution 11b SELECT COUNT(a.ename) "Toplam", COUNT(b.ename) "1980", COUNT(c.ename) "1981", COUNT(d.ename) "1982", COUNT(e.ename) "1983" a, b, c, d, e WHERE ( SELECT ename WHERE TO_CHAR(hiredate, 'YY') = '80' ) ( SELECT ename WHERE TO_CHAR(hiredate, 'YY') = '81' ) ( SELECT ename WHERE TO_CHAR(hiredate, 'YY') = '82' ) ( SELECT ename WHERE TO_CHAR(hiredate, 'YY') = '83' ) a.ename = b.ename(+) AND a.ename = c.ename(+) AND a.ename = d.ename(+) AND a.ename = e.ename(+) ; Toplam 1980 1981 1982 1983 14 1 10 1 1 43