Lab Manual SQL
Lab Manual SQL
Lab Manual SQL
Determine the validity of the following three statements. Circle either True or False.
1. Group functions work across many rows to produce one result per group.
True/False
4. Find the highest, 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 as lab_05_04.sql. Run the query.
5. Modify the query in lab_05_04.sql to display the minimum, maximum, sum, and
average salary for each job type. Resave lab_05_04.sql as lab_05_05.sql. Run
the statement in lab_05_05.sql.
6. Write a query to display the number of people with the same job.
Generalize the query so that the user in the HR department is prompted for a job title. Save
the script to a file named lab_05_06.sql. Run the query. Enter IT_PROG when
prompted and click OK.
Practice 5: Reporting Aggregated Data Using the Group Functions (continued)
7. Determine the number of managers without listing them. Label the column as Number
of Managers. Hint: Use the MANAGER_ID column to determine the number of
managers.
8. Find the difference between the highest and lowest salaries. Label the column as
DIFFERENCE.
9. Create a report to display the manager number and the salary of the lowest-paid employee for
that manager. Exclude anyone whose manager is not known. Exclude any groups where the
minimum salary is $6,000 or less. Sort the output in descending order of salary.
10. Create a query that will display the total number of employees and, of that total, the number
of employees hired in 1995, 1996, 1997, and 1998. Create appropriate column headings.
Practice 5: Reporting Aggregated Data Using the Group Functions (continued)
11. Create a matrix query to display the job, the salary for that job based on the department
number, and the total salary for that job, for departments 20, 50, 80, and 90, giving each
column an appropriate heading.
Practice 6: Displaying Data from Multiple Tables
1. Write a query for the HR department to produce the addresses of all the departments. Use
the LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state
or province, and country in the output. Use a NATURAL JOIN to produce the results.
2. The HR department needs a report of all employees. Write a query to display the last
name, department number, and department name for all the employees.
3. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and department name for all employees who work in Toronto.
4. Create a report to display employees’ last names and employee number along with their
managers’ last names and manager number. Label the columns Employee, Emp#,
Manager, and Mgr#, respectively. Save your SQL statement as lab_06_04.sql. Run
the query.
5. Modify lab_06_04.sql to display all employees including King, who has no manager.
Order the results by the employee number. Save your SQL statement as lab_06_05.sql.
Run the query in lab_06_05.sql.
Practice 6: Displaying Data from Multiple Tables (continued)
6. Create a report for the HR department that displays employee last names, department
numbers, and all the employees who work in the same department as a given employee. Give
each column an appropriate label. Save the script to a file named lab_06_06.sql. Run
the query.
7. The HR department needs a report on job grades and salaries. To familiarize yourself with
the JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create
a query that displays the name, job, department name, salary, and grade for all employees.
8. The HR department wants to determine the names of all employees who were hired after
Davies. Create a query to display the name and hire date of any employee hired after
employee Davies.
9. The HR department needs to find the names and hire dates for all employees who were hired
before their managers, along with their managers’ names and hire dates. Save the script to a
file named lab_06_09.sql.
Practice 7: Using Subqueries to Solve Queries
1. The HR department needs a query that prompts the user for an employee last name. The
query then displays the last name and hire date of any employee in the same department as
the employee whose name they supply (excluding that employee). For example, if the user
enters Zlotkey, find all employees who work with Zlotkey (excluding Zlotkey).
2. Create a report that displays the employee number, last name, and salary of all employees
who earn more than the average salary. Sort the results in order of ascending salary.
3. Write a query that displays the employee number and last name of all employees who work
in a department with any employee whose last name contains a “u.” Save your SQL
statement as lab_07_03.sql. Run your query.
4. The HR department needs a report that displays the last name, department number, and job
ID of all employees whose department location ID is 1700.
Modify the query so that the user is prompted for a location ID. Save this to a file named
lab_07_04.sql.
Solutions for Practice 7: Using Subqueries to Solve Queries (continued)
5. Create a report for HR that displays the last name and salary of every employee who reports
to King.
6. Create a report for HR that displays the department number, last name, and job ID for every
employee in the Executive department.
7. Modify the query in lab_07_03.sql to display the employee number, last name, and
salary of all employees who earn more than the average salary and who work in a department
with any employee whose last name contains a “u.” Resave lab_07_03.sql to
lab_07_07.sql. Run the statement in lab_07_07.sql.
Practice 8: Using the Set Operators
1. The HR department needs a list of department IDs for departments that do not contain the job
ID ST_CLERK. Use the set operators to create this report.
2. The HR department needs a list of countries that have no departments located in them.
Display the country ID and the name of the countries. Use the set operators to create this
report.
3. Produce a list of jobs for departments 10, 50, and 20, in that order. Display job ID and
department ID using the set operators.
Practice 8: Using the Set Operators (continued)
4. Create a report that lists the employee IDs and job IDs of those employees who currently
have a job title that is the same as their job title when they were initially hired by the
company (that is, they changed jobs but have now gone back to doing their original job).
The HR department wants you to create SQL statements to insert, update, and delete employee
data. As a prototype, use the MY_EMPLOYEE table before giving the statements to the HR
department.
Note: For all the DML statements, click the Run Script icon (or press [F5]) to execute the query.
This way you get to see the feedback messages on the Script Output tabbed page. For SELECT
queries, continue to click the Execute Statement icon or press [F9] to get the formatted output on
the Results tabbed page.
1. Run the statement in the lab_09_01.sql script to build the MY_EMPLOYEE table used
in this practice.
a. From File menu, select Open. In the Open dialog box, navigate to
D:\labs\sql1\labs folder, double-click lab_09_01.sql.
b. After the statement is opened in a SQL Worksheet, click the Run Script icon to run
the script. You get a Create Table succeeded message on the Script Output tabbed
page.
2. Describe the structure of the MY_EMPLOYEE table to identify the column names.
3. Create an INSERT statement to add the first row of data to the MY_EMPLOYEE table
from the following sample data. Do not list the columns in the INSERT clause.
4. Populate the MY_EMPLOYEE table with the second row of the sample data from
the preceding list. This time, list the columns explicitly in the INSERT clause.
6. Write an insert statement in a dynamic reusable script file to load the remaining rows into the
MY_EMPLOYEE table. The script should prompt for all the columns (ID, LAST_NAME,
FIRST_NAME, USERID and SALARY). Save this script to a file named
lab_09_06.sql.
7. Populate the table with the next two rows of sample data listed in step 3 by running the
INSERT statement in the script that you created.
11. Change the salary to $1,000 for all employees with a salary less than $900.
16. Populate the table with the last row of the sample data listed in step 3 by using the statements
in the script that you created in step 6. Run the statements in the script.
Practice 9: Manipulating Data (continued)
21. Discard the most recent DELETE operation without discarding the earlier INSERT operation.
24. Modify the lab_09_06.sql script such that the USERID is generated automatically
by concatenating the first letter of the first name and the first seven characters of the last
name. The generated USERID must be in lowercase. Hence, the script should not prompt
for the USERID. Save this script to a file named lab_09_24.sql.
Practice 9: Manipulating Data (continued)
26. Confirm that the new row was added with the correct USERID.
Practice 10: Using DDL Statements to Create and Manage Tables
Note: For all the DDL and DML statements, click the Run Script icon (or press [F5]) to execute
the query. This way you get to see the feedback messages on the Script Output tabbed page. For
SELECT queries, continue to click the Execute Statement icon or press [F9] to get the
formatted output on the Results tabbed page.
1. Create the DEPT table based on the following table instance chart. Save the statement in
a script called lab_10_01.sql, and then execute the statement in the script to create
the table. Confirm that the table is created.
a. To confirm that the table was created and to view its structure, issue the following
command:
2. Populate the DEPT table with data from the DEPARTMENTS table. Include only
those columns that you need.
3. Create the EMP table based on the following table instance chart. Save the statement in a
script called lab_10_03.sql, and then execute the statement in the script to create the
table. Confirm that the table is created.
a. To confirm that the table was created and to view its structure:
4. Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table. Include
only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID
columns. Name the columns in your new table ID, FIRST_NAME, LAST_NAME, SALARY,
and DEPT_ID, respectively.
Practice 10: Using DDL Statements to Create and Manage Tables (continued)
a. Note, you will get “Update operation not allowed on table” error message. Hence,
you will not be allowed to insert any row into the table because it is assigned a
read only status.
7. Revert the EMPLOYEES2 table to the read/write status. Now try to insert the same row again.
a. Now, because the table is assigned a READ WRITE status, you will be allowed
to insert a row into the table.
a. Note: You can even drop a table that is in the READ ONLY mode. To test
this, alter the table again to READ ONLY status and then issue the DROP
TABLE command. The table EMPLOYEES2 will be dropped.
Practice 11: Creating Other Schema Objects Part 1
1. The staff in the HR department wants to hide some of the data in the EMPLOYEES table.
They want a view called called EMPLOYEES_VU based on the employee numbers,
employee last names, and department numbers from the EMPLOYEES table. They want the
heading for the employee name to be EMPLOYEE.
2. Confirm that the view works. Display the contents of the EMPLOYEES_VU view.
3. Using your EMPLOYEES_VU view, write a query for the HR department to display
all employee names and department numbers.
4. Department 50 needs access to its employee data. Create a view named DEPT50 that
contains the employee numbers, employee last names, and department numbers for all
employees in department 50. They have requested that you label the view columns
EMPNO, EMPLOYEE, and DEPTNO. For security purposes, do not allow an employee to be
reassigned to another department through the view.
7. You need a sequence that can be used with the primary key column of the DEPT table. The
sequence should start at 200 and have a maximum value of 1,000. Have your sequence
increment by 10. Name the sequence DEPT_ID_SEQ.
8. To test your sequence, write a script to insert two rows in the DEPT table. Name your script
lab_11_08.sql. Be sure to use the sequence that you created for the ID column. Add two
departments: Education and Administration. Confirm your additions. Run the commands in
your script.
1. Write a query for the HR department to produce the addresses of all the departments. Use the
LOCATIONS and COUNTRIES tables. Show the location ID, street address, city, state or
province, and country in the output. Run the query.
2. The HR department needs a report of all employees. Write a query to display the last
name, department number, and department name for all employees. Run the query.
3. The HR department needs a report of employees in Toronto. Display the last name, job,
department number, and department name for all employees who work in Toronto.
4. Create a report to display the employee last name and the employee number along with the
last name of the employee’s manager and manager number. Label the columns Employee,
Emp#, Manager, and Mgr#, respectively. Save your SQL statement as lab_c_04.sql.
6. Create a report for the HR department that displays employee last names,
department numbers, and all the employees who work in the same department
as a given employee. Give each column an appropriate label. Save the script
to a file named lab_c_06.sql.
9. The HR department needs to find the names and hire dates for all employees
who were hired before their managers, along with their managers’ names and
hire dates. Label the columns Employee, Emp Hired, Manager, and Mgr
Hired, respectively. Save the script to a file named lab_c_09.sql.