Oracle lab practice_lab2
Oracle lab practice_lab2
Content
• Unlocking sample user accounts
• Installing SQL Developer
• Connecting SQL Developer to Oracle Database 11g XE
• About TAB and DUAL
• Writing simple queries
• Selecting data from multiple tables
• Exploring common functions
• Transaction control statements
• DML statements
• Exercises
Or:
TAB is a SYS-owned view used to list tables and views in a table. The following is a sample query to
list all tables/views in the current schema:
31
Currently, the TAB view is not widely used. Instead, the ALL_TABLES and USER_TABLES views are
preferred. ALL_TABLES describes the relational tables accessible to the current user, and
USER_TABLES describes the relational tables owned by the current user.
Suppose we want to retrieve all employees' IDs, names, and job titles. The necessary information
is stored in two tables, and thus we need to join them in the query as follows:
SQL> SELECT e.employee_id, e.first_name, e.last_name, j.job_title
FROM employees e, jobs j
WHERE e.job_id = j.job_id;
This syntax is called inner join in SQL. Beside, we have some other join types in SQL such as outer
join (left, right and full outer join). More details on Oracle joins can be found in the following link:
https://www.oracletutorial.com/oracle-basics/oracle-joins/.
32
Exploring common functions
We can use Oracle Supplied functions in our queries. We can also create our own functions and
use them in queries later. Functions in Oracle can be of type scalar or aggregate. Scalar functions
operate on a single row, whereas aggregate functions work on multiple rows. Some popular Oracle
built-in functions:
DML statements
Data Manipulation Language (DML) statements are used to manipulate data in existing tables.
INSERT, UPDATE, and DELETE are examples of DML statements. We use INSERT to add a new record
to the table, UPDATE to modify one or more columns of a table, and DELETE to remove a record
from the table.
The following is an example of an INSERT statement. We insert a new record in the regions table of
the HR schema:
SQL> INSERT INTO regions VALUES (5, 'Australia');
1 row created
SQL> COMMIT;
Commit complete.
SQL>
33
An example of the UPDATE statement is shown next, where we modify the value of region_name
from Australia to Aus and NZ. This is done as follows:
SQL> UPDATE regions SET region_name='Aus and NZ' Where region_id=5;
1 row updated.
SQL> COMMIT;
Commit complete.
SQL>
The following is an example of the DELETE statement. We remove the newly added record from the
regions table as follows:
SQL> DELETE FROM regions, where region_id = 5;
1 row deleted.
SQL> COMMIT;
Commit complete.
SQL>
Exercises
1. Unlock the HR account and do the examples in the theory sections.
2. Write SQL queries to:
a. Display first name and experience of the employees.
b. Display the length of first name for employees where last name contain character ‘b’
after 3rd position.
c. Display employees who joined in the current year.
d. Display job ID for jobs with average salary more than 10,000.
e. Display the first name, last name, salary and job grade for all employees.
f. Display the first name, last name, department number and department name for all
employees for departments 80 or 40.
g. Display the job title, department name, full name (first and last name) of employee and
starting date for all the jobs which started on or after 1st January, 1993 and ending with
on or before 31 August, 1997.
h. Display the department name and number of employees in each of the department.
i. Display years in which more than 10 employees joined.
j. Display job ID of jobs that were done by more than 3 employees for more than 100
days.
k. Change job ID of the employee 110 to IT_PROG if the employee belongs to department
10 and the existing job ID does not start with IT.
34