DBMS
DBMS
1. Create a query to display the last name and salary of employees earning more than 12000.
2. Create a query to display the employee last name and department number for employee number
176.
3. Display the last name and salary for all employees whose salary is not in the range of 5000 and
12000.
SELECT last_name, salary FROM employees WHERE salary NOT BETWEEN 5000 AND 12000;
4. Display the employee last name, job ID and start date of employees hired between February
20,1998 and May 01,1998. Order the query in ascending order by start date.
SELECT last_name, job_id, hire_date FROM employees WHERE hire_date BETWEEN '20-FEB-98' AND
'01-MAY-98' ORDER BY hire_date;
5. Display the last name and the department number of all the employees in department 50 and 60
in alphabetical order by name.
6. Display the last name and hire date of every employee who was hired in 1998.
SELECT last_name, hire_date FROM employees WHERE hire_date BETWEEN '01-JAN-98' AND '31-
DEC-98';
OR
7. Display the last name and job ID of all employees who do not have a manager.
SELECT last_name, salary, commission_pct FROM employees WHERE commission_pct IS NOT NULL
ORDER BY salary DESC, commission_pct DESC;
9. Display the last name of all employees where the third letter of the name is 'A'.
10. Display the last name of all employees who have an 'a' and an 'e' in their name.
SELECT last_name FROM employees WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';
11. Display the last name, job and salary for all employees whose job is sales representative or stock
clerk and whose salary is not equal to 2500, 3500 and 7000.
SELECT last_name, job_id, salary FROM employees WHERE job_id IN ('SA_REP','ST_CLERK') AND
salary NOT IN (2500,3500,7000);
12. Display the last name, salary and commission for all the employees whose commission amount is
20%. Label the columns as 'employee' and 'monthly salary'.
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.
4. Display the employee last name and department name for all employees who have on a lowercase
in their last names.
5. Display the employee last name and employee number along with their manager's last name and
manager number. Label the columns Employee, Emp#, Manager and Mgr# respectively.
6. Create a query to display the name and hire date of any employee hired after employee Davis.
7. Display the names and hire dates for all the employees who were hired before their managers,
along with their manager's name and hire dates. Label the columns Employee, Emp Hired, Manager,
and Mgr Hired, respectively.
8. Write a query to display the number of people with the same job.
9. Write a query that displays the difference between the lowest and highest salaries. Label the
column DIFFERENCE.
11. Display the highest, lowest, sum and average salary for each job type. Label the columns as
Maximum, Minimum, Sum and Average respectively. Round your results to the nearest whole
number.
12. 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 6000
or less. Sort the output in descending order of salary.
select manager_id, min(salary) from employees where manager_id is not null group by manager_id
having min(salary)>6000 order by min(salary) desc;
1. Create Products table (Combine the composite key mfr id and product id into a single column)
2. Create salesreps table (without making rep office and manager as foreign key)
create table salesreps(emp_no number(5) primary key, name varchar2(25), age number(3),
rep_office number(3), title varchar2(15), hire_date date, manager number(5), quota number(10),
sales number(10));
create table customers(cust_num char(5) primary key, company varchar2(25), cust_rep number(5)
references salesreps(emp_no), credit_limit number(10));
4. Create Orders table (Combine the composite key and prod into a single column)
create table orders(order_no number(5) primary key, order_date date, cust char(5) references
customers(cust_num), rep number(5) references salesreps(emp_no), mp_id number(10) references
products(mfr_prod_id), qty number(5), amt number(10));
5. Create offices table
create table offices(office number(5) primary key, city varchar2(10), region varchar2(20), mgr
number(5) references salesreps(emp_no), target number(10), sales number(10));
6. Alter salesreps table to now make rep office and manager as foreign key.
2. Insert the rows into Salesreps table (Insert null in rep office and manager)
6. Update the Null values in rep offices and manager column in salesreps table to actual values.
PRACTICAL 5 (SUBQUERIES)
1. Write a query to display the last name and hire date of any employee in the same department as
Zlotkey. Exlude Zlotkey.
2. Create a query to display the employee numbers and last names of all the employees who earn
more than the average salary. Sort the results in ascending order of salary.
select employee_id, last_name from employees where salary>(select avg(salary) from employees)
order by 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'.
5. Display the last name and salary of every employee who reports to King.
select last_name, salary from employees where manager_id=(select employee_id from employees
where last_name='King');
6. Display the department number, last name and job ID for every employee in the Executive
Department.
7. Create a query to display the employees who earn a salary that is higher than the salary of all the
sales managers (JOB_ID = 'ST_MAN'). Sort the results on salary from highest to lowest.
select last_name, salary from employees where salary>ALL(select salary from employees where
job_id='ST_MAN') order by salary desc;
8. Display the details of the employee ID, last name and department ID of those employees who live
in cities whose name begins with O. (Nested Query)
9. Write a query to display the last names of the employees who earn less than the average salary in
their departments. (Correlated Subquery)
1. Create a view called EMPLOYEES_VU based on the employee numbers, employee names and
department numbers from EMPLOYEES table.
3. Using your EMPLOYEES_VU view, enter a query a display all employee names and department
numbers.
4. Create a view named DEPT50 that contains the employee numbers, employee last names and
department numbers for all employees in departments50. Label the view columns EMPNO,
EMPLOYEE and DEPTNO. Do not allow an employee to be reassigned to another department.
describe dept50;
7. 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 incremented by ten
numbers. Name the sequence DEPT_IN_SEQ.
8. Write a script to insert two rows into the DEPT table. Be sure to use the sequence that you
created for the ID column. Add two departments named Education and Administration. Confirm your
additions.
10. Create a nonunique index on the foreign key column (department_id) in the EMP table.
PRACTICAL 7 (PL/SQL)
1. Create a PL/SQL block that selects the maximum department number in the departments table.
Print the results on the screen.
declare
v_max_deptno number;
begin
end;
2. Write a PL/SQL block to return the sum of salaries for all employees in the specified department.
declare
v_sum_sal number(10,2);
begin
dbms_output.put_line('The sum of the salary for department ' || v_deptno || ' is ' || v_sum_sal);
end;
3. Write a PL/SQL block to increase the salary of all employees by 1000, who are stock clerks.
declare
begin
end;
4. Write a PL/SQL block to demonstrate a basic loop. Insert three new locations IDs for the country
code of CA and the city of Montreal.
declare
v_location_id locations.location_id%TYPE;
v_counter number(2) := 1;
begin
loop
v_counter := v_counter + 1;
end loop;
end;
5. Write a PL/SQL block to demonstrate a while loop. Insert three new locations IDs for the country
code of CA and the city of Montreal.
declare
v_location_id locations.location_id%TYPE;
v_counter number(2) := 1;
begin
v_counter := v_counter + 1;
end loop;
end;
6. Write a PL/SQL block to demonstrate a for loop. Insert three new locations IDs for the country
code of UK and the city of Manchester.
declare
v_location_id locations.location_id%TYPE;
begin
insert into locations(location_id, city, country_id) values ((v_location_id + i), v_city, v_country_id);
end loop;
end;
1. Create the table, top_emp with two field names name and salary. Create a PL/SQL block using a
cursor to determine the top 'n' employees with respect to their salaries.
DECLARE
v_num NUMBER(3):=5;
v_sal employees.salary%TYPE;
CURSOR emp_cursor IS
BEGIN
OPEN emp_cursor;
FETCH emp_cursor INTO v_sal;
LOOP
END LOOP;
CLOSE emp_cursor;
COMMIT;
END;
b) In a PL/SQL block, retrieve the last_name,salary and manager_id of the employees working in that
department.
c) If the salary is less than 5000 and if the manager id is either 101 or 124, display the message,
display the message <<last_name>> due for a raise. Otherwise display the message<<last_name>>
not due for a raise.
DECLARE
v_deptno NUMBER(4):=10;
v_ename employees.last_name%TYPE;
v_sal employees.salary%TYPE;
v_manager employees.manager_id%TYPE;
CURSOR emp_cursor IS
SELECT last_name,salary,manager_id
BEGIN
OPEN emp_cursor;
ELSE
END IF;
END LOOP;
CLOSE emp_cursor;
END;
3. Write a cursor for loop to retrieve employees one by one and print out a list of those employees
currently working in the sales department (DEPARTMENT_ID = 80).
DECLARE
CURSOR emp_cursor IS
BEGIN
IF emp_record.department_id = 80 THEN
DBMS_OUTPUT.PUT_LINE ('Employee ' || emp_record.last_name || ' works in the Sales Dept. ');
END IF;
END;
4. Write a PL/SQL block to select the name of the employee with a given salary value. Use a variable
to provide the salary.
a. If the salary entered returns more than one row, handle the exception with an appropriate
exception handler and insert into the messages table, The message "More than one employee with a
salary of <salary>.
b. If the salary entered does not return any rows, handle the exception with an appropriate
exceptional handler and insert into the messages table. the message
d. Handle any other exception handler with an appropriate exception handler and insert into the
messages table, the message "Some other error occurred."
DECLARE
v_ename employees.last_name%TYPE;
BEGIN
EXCEPTION
END;
5. Write a PL/SQL block to update the departments table to a given location for a given dept_id.
Write a user defined exception to pass a message to the user that the specified department_id does
not exist. Execute the PL/SQL block by entering a department that does not exist.
DECLARE
e_invalid_dept EXCEPTION;
BEGIN
IF SQL%NOTFOUND THEN
raise e_invalid_dept;
END IF;
EXCEPTION
END;
1. Create the table, top_emp with two field names name and salary. Create a PL/SQL block using a
cursor to determine the top 'n' employees with respect to their salaries.
IS
BEGIN
END add_job;
b) In a PL/SQL block, retrieve the last_name,salary and manager_id of the employees working in that
department.
c) If the salary is less than 5000 and if the manager id is either 101 or 124, display the message,
display the message <<last_name>> due for a raise. Otherwise display the message<<last_name>>
not due for a raise.
IS
BEGIN
IF SQL%NOTFOUND THEN
END IF;
END upd_job;
3. Write a cursor for loop to retrieve employees one by one and print out a list of those employees
currently working in the sales department (DEPARTMENT_ID = 80).
(p_jobid IN jobs.job_id%TYPE)
IS
BEGIN
IF SQL%NOTFOUND THEN
END IF;
END del_job;
EXECUTE del_job('IT_DBA');
4. Write a PL/SQL block to select the name of the employee with a given salary value. Use a variable
to provide the salary.
a. If the salary entered returns more than one row, handle the exception with an appropriate
exception handler and insert into the messages table, The message "More than one employee with a
salary of <salary>.
b. If the salary entered does not return any rows, handle the exception with an appropriate
exceptional handler and insert into the messages table. the message "No employee with a salary of
<salary>.
c. If the salary entered returns only one row, insert into the messages table, the employee name and
the salary amount.
d. Handle any other exception handler with an appropriate exception handler and insert into the
messages table, the message "Some other error occurred."
(p_empid IN employees.employee_id%TYPE,
IS
BEGIN
WHERE employee_id=p_empid;
END query_emp;
EXECUTE query_emp(100);
5. Write a PL/SQL block to update the departments table to a given location for a given dept_id.
Write a user defined exception to pass a message to the user that the specified department_id does
not exist. Execute the PL/SQL block by entering a department
(p_jobid IN job_emp.job_id%TYPE)
RETURN VARCHAR2
IS
v_jobtitle job_emp.job_title%TYPE;
BEGIN
SELECT job_title INTO v_jobtitle FROM job_emp WHERE job_id=p_jobid;
RETURN (v_jobtitle);
END ;
6. Create and invoke a function annual_comp to return the annual salary by accepting two
parameters and employees monthly salary and commission. The function should address NULL
value.
Use the function in a SELECT statement against the employees table for department no 80.
(p_sal IN employees.salary%TYPE,
p_comm IN employees.commission_pct%TYPE)
RETURN NUMBER
IS
BEGIN
END annual_comp;
7. Create a trigger to restrict the data manipulation events on the employee table in business hours
from monday to friday.
BEGIN
IF (TO_CHAR(SYSDATE,'DY') IN ('SAT','SUN')) OR
THEN
IF DELETING THEN
hours');
ELSE
END IF;
END IF;
END;
8. Create a trigger to allow only certain employees (AD_PRES,AD_VP) to be able to earn a salary of
more than 50,000.
BEGIN
THEN
END IF;
END;
--Error:-
WHEN(NEW.salary<OLD.salary)
BEGIN