
Semestr2 / 1 - Oracle / Student_1-8
.pdf
Practice 3, Part One: Overview (continued)
5.Write a query that displays the employee’s last names with the first letter capitalized and all other letters lowercase, and the length of the names, for all employees whose name starts with J, A, or M. Give each column an appropriate label. Sort the results by the employees’ last names.
Introduction to Oracle9i: SQL Basics 3-61

Practice 3 - Part Two
6.For each employee, display the employee’s last name, and calculate the number of months between today and the date the employee was hired. Label the column MONTHS_WORKED. Order your results by the number of months employed. Round the number of months up to the closest whole number.
Note: Your results will differ.
Introduction to Oracle9i: SQL Basics 3-62

Practice 3 - Part Two (continued)
7.Write a query that produces the following for each employee:
<employee last name> earns <salary> monthly but wants <3 times salary>. Label the column Dream Salaries.
…
If you have time, complete the following exercises:
8.Create a query to display the last name and salary for all employees. Format the salary to be 15 characters long, left-padded with $. Label the column SALARY.
…
Introduction to Oracle9i: SQL Basics 3-63

Practice 3 - Part Two (continued)
9.Display each employee’s last name, hire date, and salary review date, which is the first Monday after six months of service. Label the column REVIEW. Format the dates to appear in the format similar to “Monday, the Thirty-First of July, 2000.”
…
10.Display the last name, hire date, and day of the week on which the employee started. Label the column DAY. Order the results by the day of the week starting with Monday.
…
Introduction to Oracle9i: SQL Basics 3-64

Practice 3 - Part Two (continued)
If you want an extra challenge, complete the following exercises:
11.Create a query that displays the employees’ last names and commission amounts. If an employee does not earn commission, put “No Commission.” Label the column COMM.
…
12.Create a query that displays the employees’ last names and indicates the amounts of their annual salaries with asterisks. Each asterisk signifies a thousand dollars. Sort the data in descending order of salary. Label the column EMPLOYEES_AND_THEIR_SALARIES.
…
Introduction to Oracle9i: SQL Basics 3-65

Practice 3 - Part Two (continued)
13.Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, as per the following data:
Job |
Grade |
AD_PRES |
A |
ST_MAN |
B |
IT_PROG |
C |
SA_REP |
D |
ST_CLERK |
E |
None of the above |
0 |
…
14. Rewrite the statement in the preceding question using the CASE syntax.
Introduction to Oracle9i: SQL Basics 3-66

Displaying Data from Multiple Tables
Copyright © Oracle Corporation, 2001. All rights reserved.

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 by using a self join
4-2 |
Copyright © Oracle Corporation, 2001. All rights reserved. |
Lesson Aim
This lesson covers how to obtain data from more than one table.
Introduction to Oracle9i: SQL Basics 4-2

Obtaining Data from Multiple Tables
EMPLOYEES |
DEPARTMENTS |
…
…
4-3 |
Copyright © Oracle Corporation, 2001. All rights reserved. |
Data from Multiple Tables
Sometimes you need to use data from more than one table. In the slide example, the report displays data from two separate tables.
•Employee IDs exist in the EMPLOYEES table.
•Department IDs exist in both the EMPLOYEES and DEPARTMENTS tables.
•Location IDs exist in the DEPARTMENTS table.
To produce the report, you need to link the EMPLOYEES and DEPARTMENTS tables and access data from both of them.
Introduction to Oracle9i: SQL Basics 4-3

Cartesian Products
•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.
4-4 |
Copyright © Oracle Corporation, 2001. All rights reserved. |
Cartesian Products
When a join condition is invalid or omitted completely, the result is a Cartesian product, in which all combinations of rows are displayed. All rows in the first table are joined to all rows in the second table.
A Cartesian product tends to generate a large number of rows, and the result is rarely useful. You should always include a valid join condition in a WHERE clause, unless you have a specific need to combine all rows from all tables.
Cartesian products are useful for some tests when you need to generate a large number of rows to simulate a reasonable amount of data.
Introduction to Oracle9i: SQL Basics 4-4