Uploaded by Rahul Shivanarthi

Week 1 SQL1 Lab Exercise

advertisement
COMP508 Database System Design
Lab week 1: Introduction to SQL
After completing this lesson, you should be able to do the following:
•
•
•
•
•
Task 1
Login to Oracle SQL Developer and run a script
Execute a basic SQL Select statement
Run SQL queries with “concatenation” option
Create reports with customized column titles
Limiting output results using “where” clause
Step 1: Login to Oracle SQL Developer (see instructions on connecting to Oracle)
Step 2: Download the Scripts
Download the script (HR.sql) from Blackboard available under “COMP508 Course Notes\Week
1\Lab 1” to your preferred location.
Step 3: Execute/Run the scripts
To run the code of HR.sql in your database, you can either drag and drop the file to SQL developer
or use the @symbol followed by the location of the path and file name where you saved the file in
step 2: @path-of-the-folder\HR.sql; For example, to execute the code of HR.sql that is saved in the
folder COMP508 of my H drive, I would use
@H:\COMP508\HR.sql;
You can see from the following sample code that it will first drop all the tables in the HR database
(just in case they have been created before) and then creates the tables (the sample code below
shows the CREATE TABLE command that creates the regions table and then the ALTER command
to add a primary key – you don’t have to learn this now, you will learn these concepts later).
This will create the tables shown in the diagram below for the HR database;
Task 2
1.1 Explore the data and tables structure of the HR schema above using SQL SELECT statement and the
DESCRIBE command.
For example, to retrieve all the data (all rows) from employees table, the SQL statement would be:
SELECT * FROM employees;
and to display the structure (i.e. attribute or column names, datatypes) of the employees table, the
SQL command would be:
DESCRIBE employees; OR
DESC employees;
Now repeat the above for the remaining tables: departments, locations, countries, jobs, job_history,
regions. Understand the structure and data in each of the tables.
1.2 There are at least three errors in the following SELECT statement. Correct the errors and run the
statement.
SELECT fname, job, salary,
FROM employees;
1.3 The following SQL statement execute successfully: True/False
SELECT job_title, max_salary, min_salary
FROM jobs;
1.4 The following SQL statement execute successfully: True/False
SELECT last_name "Name" , salary + commission_pct Total Salary
FROM employees;
1.5 Execute the following query which will retrieve the last_name, salary, commission, and AnnSal
(which is a calculated column for 12*salary*commission_pct) of all employees. Why is AnnSal NULL
in some rows? Explain.
SELECT last_name, salary, commission_pct, 12*salary*commission_pct AnnSal
FROM employees;
1.6 Create SQL queries that meet the following reporting needs. Save your queries in a file (e.g.
w1task.sql) in a folder in your preferred location.
1. Display employee number, employee first name, employee last name, job title and the
department number for each employee. Rename the employee_id, job_id, and department_id
columns headings to: Emp#, Job Title and Department ID, respectively.
2. Display unique list of job titles (i.e. without duplicates) from the EMPLOYEES table.
3. Display the following two columns (see the required format shown in the sample output below):
a.
employee last and first names concatenated and displayed as “Full Name”, and
b. employee emails and phone numbers concatenated and displayed as “Contact Details”
4. Display all details of all employees who work in department 60.
5. Display the last name and job id of all employees who work as programmers (i.e. IT_PROG).
Download