52
Oracle Database 11g SQL Fundamentals Lab 2

Oracle Database 11g SQL Fundamentals Lab 2

  • Upload
    others

  • View
    32

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Oracle Database 11g SQL Fundamentals Lab 2

Oracle Database 11g

SQL Fundamentals – Lab 2

Page 2: Oracle Database 11g SQL Fundamentals Lab 2

SQL Functions

Page 3: Oracle Database 11g SQL Fundamentals Lab 2

Single-Row Functions

Page 4: Oracle Database 11g SQL Fundamentals Lab 2

4

Function Result

Character Functions

These functions convert case for character strings.

LOWER('SQL Course')

UPPER('SQL Course')

INITCAP('SQL Course')

sql course

SQL COURSE

Sql Course

Page 5: Oracle Database 11g SQL Fundamentals Lab 2

5

Using Case Manipulation Functions

Display the employee number, name, and department number for employee Higgins:

SELECT employee_id, last_name, department_id

FROM employees

WHERE last_name = 'higgins';

no rows selected

SELECT employee_id, last_name, department_id

FROM employees

WHERE LOWER(last_name) = 'higgins';

Page 6: Oracle Database 11g SQL Fundamentals Lab 2

6

Working with Dates

• The default date display format is DD-MON-RR.

SELECT last_name, hire_date

FROM employees

WHERE last_name like 'G%';

Page 7: Oracle Database 11g SQL Fundamentals Lab 2

7

Using Arithmetic Operators

with Dates

SELECT last_name, (SYSDATE-hire_date)/7 AS WEEKS

FROM employees

WHERE department_id = 90;

SYSDATE is a function that returns current Date and

Time

Subtract two dates to find the number of days

between those dates

Page 8: Oracle Database 11g SQL Fundamentals Lab 2

8

Data Type Conversion

NUMBER CHARACTER

TO_CHAR

TO_NUMBER

DATE

TO_CHAR

TO_DATE

Page 9: Oracle Database 11g SQL Fundamentals Lab 2

9

General Functions: NVL Function

• Converts a null 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')

Page 10: Oracle Database 11g SQL Fundamentals Lab 2

10

SELECT last_name, salary, NVL(commission_pct, 0),

(salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SAL

FROM employees;

Using the NVL Function

1 2

1

2

Page 11: Oracle Database 11g SQL Fundamentals Lab 2

Summarizing Data Using

Group Functions

Page 12: Oracle Database 11g SQL Fundamentals Lab 2

12

SELECT [column,] group_function(column), ...

FROM table

[WHERE condition]

[GROUP BY column]

[ORDER BY column];

• Types of Group Functions include:

- AVG - COUNT - MAX - MIN - SUM

• Group functions operate on set of values to return ONE value.

• Group Functions Syntax

Group Functions

Page 13: Oracle Database 11g SQL Fundamentals Lab 2

13

SELECT AVG(salary), MAX(salary),

MIN(salary), SUM(salary)

FROM employees

WHERE job_id LIKE '%REP%';

Using the AVG and SUM Functions

You can use AVG and SUM for numeric data.

Page 14: Oracle Database 11g SQL Fundamentals Lab 2

14

Using the MIN and MAX Functions

You can use MIN and MAX for any data type.

SELECT MIN(hire_date), MAX(hire_date)

FROM employees;

Page 15: Oracle Database 11g SQL Fundamentals Lab 2

15

SELECT COUNT(*)

FROM employees;

Using the COUNT Function

COUNT(*) returns the number of rows in a table.

COUNT(*) returns the number of rows in a table that satisfy the

criteria of the SELECT statement, including duplicate rows and

rows containing null values in any of the columns.

Page 16: Oracle Database 11g SQL Fundamentals Lab 2

16

Using the COUNT Function

• COUNT(column) returns the number of rows with non-

null values for the column.

SELECT COUNT(commission_pct)

FROM employees

WHERE department_id = 80;

Page 17: Oracle Database 11g SQL Fundamentals Lab 2

17

SELECT COUNT(DISTINCT department_id)

FROM employees;

Using the DISTINCT Keyword

• COUNT(DISTINCT column) returns the number of

distinct non-null values of the column.

• Display the number of distinct department values in the EMPLOYEES table.

Page 18: Oracle Database 11g SQL Fundamentals Lab 2

18

SELECT AVG(commission_pct)

FROM employees;

Group Functions and Null Values

• Group functions ignore null values in the column.

• The average is calculated as the total commission

paid divided by the number of employees receiving a

commission (4).

Page 19: Oracle Database 11g SQL Fundamentals Lab 2

19

SELECT AVG(NVL(commission_pct, 0))

FROM employees;

Using the NVL Function

with Group Functions

• The NVL function forces group functions to include

null values.

• The average is calculated as the total commission

paid to all employees divided by the total number of

employees in the company (20).

Page 20: Oracle Database 11g SQL Fundamentals Lab 2

20

Group Functions

• Display maximum salary of all employees.

Display maximum salary of employees in department 20.

Select MAX (Salary)

From Employees;

Select MAX (Salary)

From Employees

Where department_id= 20;

Page 21: Oracle Database 11g SQL Fundamentals Lab 2

21

Grouping

• What if we need to display max salary ofemployees in each department?

Solution :

We need to repeat the last select statement x times

where x is the number of departments in the system.

OR Use Group By Clause

Page 22: Oracle Database 11g SQL Fundamentals Lab 2

22

SELECT column, group_function(column)

FROM table

[WHERE condition]

[GROUP BY group_by_ column ]

[ORDER BY column];

Creating Groups of Data: The GROUP BY Clause Syntax

• Divide rows in a table into smaller groups by using

the GROUP BY clause.

• If the group-by column contains null values, a group

will be created for them.

Page 23: Oracle Database 11g SQL Fundamentals Lab 2

23

SELECT department_id, AVG(salary)

FROM employees

GROUP BY department_id ;

Using the GROUP BY Clause

• Display the average salary for each department

Page 24: Oracle Database 11g SQL Fundamentals Lab 2

24

Using the GROUP BY Clause

• The GROUP BY column does not have to be in theSELECT list.

SELECT AVG(salary)

FROM employees

GROUP BY department_id ;

Is the query objective clear in this case ??

Page 25: Oracle Database 11g SQL Fundamentals Lab 2

25

Illegal Queries

Using Group Functions

• Any column in the SELECT list that is not an aggregate function must be in the GROUP BY

clause.

SELECT department_id, COUNT(last_name)

FROM employees;

SELECT department_id, COUNT(last_name)

*

ERROR at line 1:

ORA-00937: not a single-group group function

Page 26: Oracle Database 11g SQL Fundamentals Lab 2

26

SQL Statement Execution

1. Table is identified due to FROM clause.

2. Rows are selected due to the WHERE condition.

3. Rows are grouped due to the GROUP BY clause.

4. The GROUP FUNCTION is applied to each group.

5. OREDER BY clause sorts results.

Page 27: Oracle Database 11g SQL Fundamentals Lab 2

27

SQL Statement Execution: Example

– The FROM clause specifies the tables that the database must access: the

EMPLOYEES table.

– The WHERE clause specifies the rows to be retrieved. Since there is no

WHERE clause, all rows are retrieved by default.

– The GROUP BY clause specifies how the rows should be grouped. The

rows are being grouped by department number.

– The AVG function applied to the salary column will calculate the average

salary for each department.

– The SELECT clause displays department number and average salary for

each department.

SELECT department_id, AVG(salary)

FROM employees

GROUP BY department_id ;

Page 28: Oracle Database 11g SQL Fundamentals Lab 2

28

Illegal Queries

Using Group Functions

• You cannot use the WHERE clause to restrict groups.

• You cannot use group functions in the WHERE clause.

SELECT department_id, AVG(salary)

FROM employees

WHERE AVG(salary) > 8000

GROUP BY department_id;

WHERE AVG(salary) > 8000

*

ERROR at line 3:

ORA-00934: group function is not allowed here

Page 29: Oracle Database 11g SQL Fundamentals Lab 2

Having Clause

Page 30: Oracle Database 11g SQL Fundamentals Lab 2

30

SELECT column, group_function

FROM table

[WHERE condition]

[GROUP BY group_by_expression]

[HAVING group_condition]

[ORDER BY column];

Excluding Group Results: The HAVING

Clause

• To restrict the group results, that is displayonly groups that satisfy a specific condition,we use HAVING clause.

Page 31: Oracle Database 11g SQL Fundamentals Lab 2

31

Excluding Group Results: The HAVING

Clause

Use the HAVING clause to restrict groups:

1. Table is identified due to FROM clause.

2. Rows are selected due to the WHERE condition.

3. Rows are grouped due to the GROUP BY clause.

4. The group function is applied to each group.

5. Groups matching HAVING clause are returned.

6. The ORDER BY clause sorts results.

Page 32: Oracle Database 11g SQL Fundamentals Lab 2

32

Using the HAVING Clause

SELECT department_id, MAX(salary)

FROM employees

GROUP BY department_id

HAVING MAX(salary)>10000 ;

Page 33: Oracle Database 11g SQL Fundamentals Lab 2

33

General Syntax

SELECT [DISTINCT] { * | column [alias], …}

FROM table

[WHERE condition (s)]

[GROUP BY group-by column]

[HAVING group condition]

[ORDER BY {column | alias} [ASC|DESC]];

Page 34: Oracle Database 11g SQL Fundamentals Lab 2

Subqueries

Page 35: Oracle Database 11g SQL Fundamentals Lab 2

35

• The subquery (inner query) executes once before the main query (outer query).

• The result of the subquery is used by the main query.

• You can place the subquery in a number of SQL clauses, including the following:

– WHERE clause

– HAVING clause

SELECT select_list

FROM table

WHERE column operator

(SELECT select_list

FROM table);

Subquery Syntax

Page 36: Oracle Database 11g SQL Fundamentals Lab 2

36

Subquery

• Enclose subqueries in parentheses.

• Place subqueries on the right side of the comparison

condition.

• The ORDER BY clause cannot be used in the

subquery.

• They can be very useful when you need to select rows

from a table with a condition that depends on the data

in the table itself, or data from another table.

Page 37: Oracle Database 11g SQL Fundamentals Lab 2

37

SELECT last_name

FROM employees

WHERE salary >

(SELECT salary

FROM employees

WHERE employee_id = 141);

Using a Subquery

11000

Who gets a higher salary than employee number 141 ?

Page 38: Oracle Database 11g SQL Fundamentals Lab 2

38

Types of Subqueries

– Single-row subquery

– Multiple-row subquery

Main query

Subqueryreturns

ST_CLERK

ST_CLERK

SA_MAN

Main query

Subqueryreturns

Single-row subqueries:

Queries that return only one row from inner SELECT statement

Multiple-row subqueries:

Queries that return more than one row from inner SELECT statement

Page 39: Oracle Database 11g SQL Fundamentals Lab 2

39

Using Subqueries

• Use single-row operators with single-row

subqueries, and use multiple-row operators with

multiple-row subqueries.

Page 40: Oracle Database 11g SQL Fundamentals Lab 2

40

Single-Row Subqueries

• Return only one row

• Use single-row comparison operators

Operator Meaning

= Equal to

> Greater than

>= Greater than or equal to

< Less than

<= Less than or equal to

<> Not equal to

Page 41: Oracle Database 11g SQL Fundamentals Lab 2

41

Multiple-Row Subqueries

• Returns more than one row

• Use multiple-row comparison operators, such

as IN.

Page 42: Oracle Database 11g SQL Fundamentals Lab 2

42

Single-Row Subqueries

• Display the employees whose job ID is the same as that of employee 141:

SELECT last_name, job_id

FROM employees

WHERE job_id =

(SELECT job_id

FROM employees

WHERE employee_id = 141);

Page 43: Oracle Database 11g SQL Fundamentals Lab 2

43

Single-Row Subqueries

• Display information about the employees who work in the Sales department:

SELECT last_name, job_id

FROM employees

WHERE departmentid =

(SELECT departmentid

FROM departments

WHERE departmentName=

‘Sales’);

Page 44: Oracle Database 11g SQL Fundamentals Lab 2

44

SELECT last_name, job_id, salary

FROM employees

WHERE salary =

(SELECT MIN(salary)

FROM employees);

Using Group Functions in a Subquery

2500

Display the employee last name, job ID, and salary of all

employees whose salary is equal to the minimum salary

Page 45: Oracle Database 11g SQL Fundamentals Lab 2

45

SELECT department_id, MIN(salary)

FROM employees

GROUP BY department_id

HAVING MIN(salary) >

(SELECT MIN(salary)

FROM employees

WHERE department_id = 50);

The HAVING Clause with Subqueries

• The Oracle server executes subqueries first.

• The Oracle server returns results into the HAVING

clause of the main query.

• Display all the departments that have a minimumsalary greater than that of department 50.

2500

Page 46: Oracle Database 11g SQL Fundamentals Lab 2

46

SELECT employee_id, last_name

FROM employees

WHERE salary =

(SELECT MIN(salary)

FROM employees

GROUP BY department_id);

What Is Wrong with This Statement?

ERROR at line 4:

ORA-01427: single-row subquery returns more than

one row

Single-row operator with multiple-row subquery

How to correct this error ?? Change the = operator to IN

Page 47: Oracle Database 11g SQL Fundamentals Lab 2

Practice

SQL Developer

Page 48: Oracle Database 11g SQL Fundamentals Lab 2

48

4

8

Page 49: Oracle Database 11g SQL Fundamentals Lab 2

49

Writing SQL Statements

• SQL statements are not case sensitive.

• SQL statements can be on one or more lines.

• Keywords cannot be abbreviated.

• Clauses are usually placed on separate lines.

• Indents are used to enhance readability.

Page 50: Oracle Database 11g SQL Fundamentals Lab 2

50

Practice

• Display the minimum, maximum, sum, and

average salary for each job type.

Page 51: Oracle Database 11g SQL Fundamentals Lab 2

51

SELECT job_id, MAX(salary), MIN(salary), SUM(salary), AVG(salary)

FROM EMPLOYEES

GROUP BY job_id

Page 52: Oracle Database 11g SQL Fundamentals Lab 2

Thank You