Assignments

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 11

ORACLE ASSIGNMENT QUESTIONS

SET OPERATORS
1. List the department IDs for departments that do not contain the job ID
ST_CLERK, using SET operators.

2. Display the country ID and the name of the countries that have no
departments located in them, using SET operators.

3. Produce a list of jobs for departments 10, 50, and 20, in that order. Display
job ID and department ID, using SET operators.

4. List the employee IDs and job IDs of those employees who currently have the
job title that they held before beginning their tenure with the company.

5. Write a compound query that lists the following: • Last names and
department ID of all the employees from the EMPLOYEES table, regardless of
whether or not they belong to any department • Department ID and
department name of all the departments from the DEPARTMENTS table,
regardless of whether or not they have employees working in them

RELATIONAL OPERATORS
1. Write a query to display the last name, department number, and salary of
any employee whose department number and salary both match the
department number and salary of any employee who earns a commission.

2. Display the last name, department name, and salary of any employee whose
salary and commission match the salary and commission of any employee
located in location ID1700.

3. Create a query to display the last name, hire date, and salary for all
employees who have the same salary and commission as Kochhar. Note: Do not
display Kochhar in the result set.

4. Create a query to display the employees who earn a salary that is higher
than the salary of all of the sales managers (JOB_ID = ’SA_MAN’). Sort the
results on salary from highest to lowest.

5. Display the details of the employee ID, last name, and department ID of
those employees who live in cities whose name begins with T.

6. Write a query to find all employees who earn more than the average salary
in their departments. Display last name, salary, department ID, and the
average salary for the department. Sort by average salary. Use aliases for the
columns retrieved by the query as shown in the sample output.

7. Find all employees who are not supervisors. a. First do this by using the NOT
EXISTS operator. b. Can this be done by using the NOT IN operator? How, or
why not?

8. Write a query to display the last names of the employees who earn less than
the average salary in their departments.

9. Write a query to display the last names of employees who have one or more
coworkers in their departments with later hire dates but higher salaries.

10. Write a query to display the employee ID, last names, and department
names of all employees. Note: Use a scalar subquery to retrieve the
department name in the SELECT statement.

11. Write a query to display the department names of those departments


whose total salary cost is above one-eighth (1/8) of the total salary cost of the
whole company. Use the WITH clause to write this query. Name the query
SUMMARY.

SQL PLUS COMMANDS


DETERMINE WHETHER THE FOLLOWING STATEMENTS ARE TRUE OR FALSE:

1. The following statement is correct: DEFINE & p_val = 100

2. The DEFINE command is a SQL command.

3. Write a script to display the employee last name, job, and hire date for all
employees who started between a given range. Concatenate the name and job
together, separated by a space and comma, and label the column Employees.
In a separate SQL script file, use the DEFINE command to provide the two
ranges. Use the format MM/DD/YYYY. Save the script files as lab7_3a.sql and
lab7_3b.sql.

4. Write a script to display the employee last name, job, and department name
for a given location. The search condition should allow for case-insensitive
searches of the department location. Save the script file as lab7_4.sql.

5. Modify the code in lab7_4.sql to create a report containing the department


name, employee last name, hire date, salary, and each employee’s annual
salary for all employees in a given location. Label the columns DEPARTMENT
NAME, EMPLOYEE NAME, START DATE, SALARY, and ANNUAL SALARY, placing
the labels on multiple lines. Resave the script as lab7_5.sql and execute the
commands in the script.

SINGLE ROW FUNCTIONS


1. 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.

2. Write a query that produces the following for each employee: earns monthly
but wants <3 times salary>. Label the column Dream Salaries.

3. 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.

4. 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.”

5. 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.

6. 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.

7. 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.

CONSTRAINTS
1. Add a table-level PRIMARY KEY constraint to the EMP table on the ID column.
The constraint should be named at creation. Name the constraint
my_emp_id_pk

2. Create a PRIMARY KEY constraint to the DEPT table using the ID column. The
constraint should be named at creation. Name the constraint my_dept_id_pk.

3. Add a column DEPT_ID to the EMP table. Add a foreign key reference on the
EMP table that ensures that the employee is not assigned to a nonexistent
department. Name the constraint my_emp_dept_id_fk.

4. Confirm that the constraints were added by querying the


USER_CONSTRAINTS view. Note the types and names of the constraints. Save
your statement text in a file called lab10_4.sql.

5. Display the object names and types from the USER_OBJECTS data dictionary
view for the EMP and DEPT tables. Notice that the new tables and a new index
were created.

6. Modify the EMP table. Add a COMMISSION column of NUMBER data type,
precision 2, scale 2. Add a constraint to the commission column that ensures
that a commission value is greater than zero.

DCL
1. What privilege should a user be given to log on to the Oracle Server? Is this a
system or an object privilege?

2. What privilege should a user be given to create tables?

3. If you create a table, who can pass along privileges to other users on your
table?

4. You are the DBA. You are creating many users who require the same system
privileges. What should you use to make your job easier?

5. What command do you use to change your password?

6. Grant another user access to your DEPARTMENTS table. Have the user grant
you query access to his or her DEPARTMENTS table.

7. Query all the rows in your DEPARTMENTS table.

8. Add a new row to your DEPARTMENTS table. Team 1 should add Education as
department number 500. Team 2 should add Human Resources department
number 510. Query the other team’s table.

9. Create a synonym for the other team’s DEPARTMENTS table.

10. Query all the rows in the other team’s DEPARTMENTS table by using your
synonym.
11. Query the USER_TABLES data dictionary to see information about the tables
that you own.

12. Query the ALL_TABLES data dictionary view to see information about all the
tables that you can access. Exclude tables that you own.

13. Revoke the SELECT privilege from the other team.

14. Remove the row you inserted into the DEPARTMENTS table in step 8 and
save the changes.

DDL
1. Create the DEPT table based on the following table instance chart. Place the
syntax in a script called lab9_1.sql, then execute the statement in the script to
create the table. Confirm that the table is created.

2. Populate the DEPT table with data from the DEPARTMENTS table. Include
only columns that you need.

3. Create the EMP table based on the following table instance chart. Place the
syntax in a script called lab9_3.sql, and then execute the statement in the
script to create the table. Confirm that the table is created.

4. Modify the EMP table to allow for longer employee last names. Confirm your
modification.

5. Confirm that both the DEPT and EMP tables are stored in the data dictionary.
(Hint: USER_TABLES)

6. 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.

7. Drop the EMP table.

8. Rename the EMPLOYEES2 table to EMP.

9. Add a comment to the DEPT and EMP table definitions describing the tables.
Confirm your additions in the data dictionary. COMMENT ON TABLE emp IS
’Employee Information’; COMMENT ON TABLE dept IS ’Department
Information’;

10. Drop the FIRST_NAME column from the EMP table. Confirm your
modification by checking the description of the table.

11. In the EMP table, mark the DEPT_ID column in the EMP table as UNUSED.
Confirm your modification by checking the description of the table.

12. Drop all the UNUSED columns from the EMP table. Confirm your
modification by checking the description of the table.

DML
INSERT DATA INTO THE MY_EMPLOYEE TABLE.

1. Run the statement in the lab8_1.sql script to build the MY_EMPLOYEE table
that will be used for the lab.

2. Describe the structure of the MY_EMPLOYEE table to identify the column


names.

3. 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 sample data from
the preceding list. This time, list the columns explicitly in the INSERT clause.

5. Confirm your addition to the table.

6. Write an insert statement in a text file named loademp.sql to load rows into
the MY_EMPLOYEE table. Concatenate the first letter of the first name and the
first seven characters of the last name to produce the userid.

7. Populate the table with the next two rows of sample data by running the
insert statement in the script that you created.

8. Confirm your additions to the table.

9. Make the data additions permanent.

10. Change the last name of employee 3 to Drexler. UPDATE my_employee

11. Change the salary to 1000 for all employees with a salary less than 900.

12. Verify your changes to the table.

13. Delete Betty Dancs from the MY_EMPLOYEE table.

14. Confirm your changes to the table.


15. Commit all pending changes.

16. Populate the table with the last row of sample data by modifying the
statements in the script that you created in step 6. Run the statements in the
script.

17. Confirm your addition to the table.

18. Mark an intermediate point in the processing of the transaction.

19. Empty the entire table.

20. Confirm that the table is empty.

21. Discard the most recent DELETE operation without discarding the earlier
INSERT operation.

22. Confirm that the new row is still intact.

23. Make the data addition permanent.

GROUPING SETS
1. Write a query to display the following for those employees whose manager ID
is less than 120:

Manager ID

Job ID and total salary for every job ID for employees who report to the same
manager

Total salary of those managers

Total salary of those managers, irrespective of the job IDs

2. Observe the output from question 1. Write a query using the GROUPING
function to determine whether the NULL values in the columns corresponding
to the GROUP BY expressions are caused by the ROLLUP operation.

3. Write a query to display the following for those employees whose manager ID
is less than 120 :

Manager ID

Job and total salaries for every job for employees who report to the same
manager
Total salary of those managers

Cross-tabulation values to display the total salary for every job, irrespective of
the manager

Total salary irrespective of all job titles

4. Observe the output from question 3. Write a query using the GROUPING
function to determine whether the NULL values in the columns corresponding
to the GROUP BY expressions are caused by the CUBE operation.

5. Using GROUPING SETS, write a query to display the following groupings :

department_id, manager_id, job_id

department_id, job_id

Manager_id, job_id

The query should calculate the sum of the salaries for each of these groups.

JOINS
1. Write a query to display the last name, department number, and department
name for all employees.

2. Create a unique listing of all jobs that are in department 80. Include the
location of the department in the output.

3. Write a query to display the employee last name, department name,


location ID, and city of all employees who earn a commission.

4. Display the employee last name and department name for all employees who
have an a (lowercase) in their last names. Place your SQL statement in a text
file named test4.sql.

5. Write a query to display the last name, job, department number, and
department name for all employees who work in Toronto.

6. Display the employee last name and employee number along with their
manager’s last name and manager number. Label the columns Employee ,
Emp#, Manager, Mgr#, respectively. Place your SQL statement in a text file
named test6.sql.

7. Modify test6.sql to display all employees including King, who has no


manager. Place your SQL statement in a text file named test7.sql. Run the
query in lab4_7.sql

8. Create a query 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.

9. Show the structure of the JOB_GRADES table. Create a query that displays
the name, job, department name, salary, and grade for all employees.

10. Create a query to display the name and hire date of any employee hired
after employee Davies.

11. Display the names and hire dates for all employees who were hired before
their managers, along with their manager’s names and hire dates. Label the
columns Employee, Emp Hired, Manager, and Mgr Hired, respectively.

SUB QUERY
1. Write a query to display the last name and hire date of any employee in the
same department as Zlotkey. Exclude Zlotkey.

2. Create a query to display the employee numbers and last names of all
employees who earn more than the average salary. Sort the results in
ascending order of salary.

3. Write a query that displays the employee numbers and last names of all
employees who work in a department with any employee whose last name
contains a u. Place your SQL statement in a text file named lab6_3.sql. Run
your query.

4. Display the last name, department number, and job ID of all employees
whose department location ID is 1700.

5. Display the last name and salary of every employee who reports to King.

6. Display the department number, last name, and job ID for every employee in
the Executive department.

7. Modify the query in lab6_3.sql to display the employee numbers, last names,
and salaries of all employees who earn more than the average salary and who
work in a department with any employee with a u in their name. Resave
lab6_3.sql to lab6_7.sql. Run the statement in lab6_7.sql.

VIEWS
1. Create a view called EMPLOYEES_VU based on the employee numbers,
employee names, and department numbers from the EMPLOYEES table. Change
the heading for the employee name to EMPLOYEE.

2. Display the contents of the EMPLOYEES_VU view.

3. Select the view name and text from the USER_VIEWS data dictionary view.
Note: Another view already exists. The EMP_DETAILS_VIEW was created as part
of your schema. Note: To see more contents of a LONG column, use the
iSQL*Plus command SET LONG n, where n is the value of the number of
characters of the LONG column that you want to see.

4. Using your EMPLOYEES_VU view, enter a query to display all employee


names and department numbers.

5. Create a view named DEPT50 that contains the employee numbers,


employee last names, and department numbers for all employees in
department 50. Label the view columns EMPNO, EMPLOYEE, and DEPTNO. Do
not allow an employee to be reassigned to another department through the
view.

6. Display the structure and contents of the DEPT50 view.

7. Attempt to reassign Matos to department 80.

8. Create a view called SALARY_VU based on the employee last names,


department names, salaries, and salary grades for all employees. Use the
EMPLOYEES, DEPARTMENTS, and JOB_GRADES tables. Label the columns
Employee, Department, Salary, and Grade, respectively.

SEQUENCE
1. Create a sequence to be used with the primary key column of the DEPT
table. The sequence should start at 200 and have a maximum value of 1000.
Have your sequence increment by ten numbers. Name the sequence
DEPT_ID_SEQ.

2. Write a query in a script to display the following information about your


sequences: sequence name, maximum value, increment size, and last number.
Name the script lab12_2.sql. Run the statement in your script.

3. Write a script to insert two rows into the DEPT table. Name your script
lab12_3.sql. Be sure to use the sequence that you created for the ID column.
Add two departments named Education and Administration. Confirm your
additions. Run the commands in your script.

4. Create a nonunique index on the foreign key column (DEPT_ID) in the EMP
table.

5. Display the indexes and uniqueness that exist in the data dictionary for the
EMP table. Save the statement into a script named lab12_5.sql.

You might also like