Dec 8, 2014 - Write SQL statement to retrieve last name and hire date for all employees who were hired ... Note: sysdate
Islamic University of Gaza Faculty of Engineering Computer Engineering Department Database Lab (ECOM 4113)
Midterm Exam December 8, 2014 Time: 60 Minutes Eng. Mohammed Alokshiya Student Name:
Solutions
ID:
^_^
Question 1 [18 points]
1. Write SQL statement to unlock “HR” database account, and set password “pass” for it. ALTER USER HR IDENTIFIED BY pass ACCOUNT UNLOCK;
2. Write SQL statement to retrieve last name and salary for employees whose salary is not in range 5000 to 12000. SELECT LAST_NAME, SALARY FROM EMPLOYEES WHERE SALARY NOT BETWEEN 5000 AND 12000;
3. Write SQL statement to raise the salary of all employees in department 80 by 15%. UPDATE EMPLOYEES SET SALARY = SALARY + SALARY * 15 / 100 WHERE DEPARTMENT_ID = 80;
4. Write SQL statement to retrieve last name and hire date for all employees who were hired in 1994. SELECT LAST_NAME, HIRE_DATE FROM EMPLOYEES WHERE TO_CHAR(HIRE_DATE, 'YYYY') = 1994;
1
5. Write SQL statement to retrieve all Jobs from Employees’ table without duplication. SELECT DISTINCT JOB_ID FROM EMPLOYEES;
6. Write SQL statement to delete all employees whose salary is greater than their managers’ salaries. DELETE FROM EMPLOYEES E WHERE SALARY > (SELECT SALARY FROM EMPLOYEES M WHERE E.MANAGER_ID = M.EMPLOYEE_ID);
7. Write SQL statement to retrieve last name, salary, and commission of all employees who earn commissions. Sort data in descending order of salary and commissions. SELECT LAST_NAME, SALARY, COMMISSION_PCT FROM EMPLOYEES WHERE COMMISSION_PCT IS NOT NULL ORDER BY 2 DESC, 3 DESC;
8. Write SQL statement to displays the first eight characters of the employees’ last names and indicates number of years between today and their hire date with asterisks. Each asterisk signifies a year. SELECT RPAD(SUBSTR(LAST_NAME, 1, 8) || ' ', LENGTH(LAST_NAME) + 1 + TRUNC((SYSDATE - HIRE_DATE) / 365), '*') "Name" FROM EMPLOYEES;
9. Write SQL statement to retrieve last name, job, and department number for all employees, except employees who work in Toronto. SELECT LAST_NAME, JOB_ID, E.DEPARTMENT_ID FROM EMPLOYEES E JOIN DEPARTMENTS D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID JOIN LOCATIONS L ON D.LOCATION_ID = L.LOCATION_ID WHERE L.CITY 'Toronto';
10. Write a query to retrieve departments’ names and cities for all departments that has more than 5 employees. SELECT DEPARTMENT_NAME, CITY FROM DEPARTMENTS D JOIN LOCATIONS USING (LOCATION_ID) WHERE (SELECT COUNT(*) FROM EMPLOYEES E WHERE D.DEPARTMENT_ID = E.DEPARTMENT_ID) > 5;
2
Question 2 [10 points] Evaluate the result of the following queries:
1. SELECT INITCAP('SQL Course') FROM DUAL Sql Course
2. SELECT RPAD('SQL ' || LPAD(123456, 10, '$'), 15, '#') FROM DUAL SQL $$$$123456#
3. SELECT TO_CHAR(sysdate, 'DD Month YYYY HH:MM:SS AM') FROM DUAL Note: sysdate return the server’s current date/time. 08 December
2014 03:12:xx PM
Note: xx can be any value from 00 to 59
4. SELECT NULLIF(LENGTH('Java'), LENGTH('Oracle')) result FROM DUAL 4
5. SELECT UPPER(CONCAT(SUBSTR ('Oracle Database', 1, 7), 'LAB')) FROM DUAL ORACLE LAB
EMPLOYEES EMPLOYEEID FNAME LNAME JOB_ID HIRE_DATE SALARY MANAGER_ID DEPT_ID
DEPARTMENTS DEPARTMENTID DEPARTMENT_NAME MANAGER_ID LOCATION_ID
JOBS JOBID
JOB_TITLE
MIN_SAL
MAX_SAL
POSTAL_CODE
CITY
COUNTRY_ID
LOCATIONS LOCATIONID
COUNTRIES COUNTRYID
COUNTRY_NAME
3
Question 3 [12 points] for the schema given in the previous page:
1. Write DDL statement to create a new user “HumanR” with password “HR” in tablespace “Users” with Quota 10M. 2. Provide sufficient privileges for user “HumanR” so that he can connect to database and create new schema objects like tables, views, etc. 3. Write DDL statements to create the first three tables in the schame. CREATE USER HUMANR IDENTIFIED BY HR DEFAULT TABLESPACE USERS QUOTA 10M ON USERS; ----------------------------------------------------------------------GRANT CONNECT, RESOURCE TO HUMANR; ----------------------------------------------------------------------CREATE TABLE JOBS ( JOBID NUMBER(6) PRIMARY KEY, JOB_TITLE VARCHAR2(20), MIN_SAL NUMBER(6), MAX_SAL NUMBER(6) ); CREATE TABLE EMPLOYEES ( EMPLOYEEID NUMBER(6) PRIMARY KEY, FNAME VARCHAR2(20) NOT NULL, LNAME VARCHAR2(20) NOT NULL, JOB_ID NUMBER(6) REFERENCES JOBS(JOBID), HIRE_DATE DATE, SALARY NUMBER(6), MANAGER_ID NUMBER(6) REFERENCES EMPLOYEES (EMPLOYEEID), DEPT_ID NUMBER(6) ); CREATE TABLE DEPARTMENTS ( DEPARTMENTID NUMBER(6) PRIMARY KEY, DEPARTMENT_NAME VARCHAR2(20), MANAGER_ID NUMBER(6) REFERENCES EMPLOYEES(EMPLOYEEID), LOCATION_ID NUMBER(6) REFERENCES LOCATIONS(LOCATIONID) ); ALTER TABLE EMPLOYEES ADD ( FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENTS(DEPARTMENTID) );
4