You are on page 1of 13

1. Initiate a SQL*Plus session using the user ID and password for the user you created. 2. True or False.

SQL*Plus commands access the database? FALSE 3. True or False. Will the SELECT statement execute successfully? SQL>SELECT ename, job, sal Salary 2 FROM emp; TRUE 4. True or False. Will the SELECT statement execute successfully? SQL>SELECT * 2 FROM salgrade; TRUE 5. There are four coding errors in this statement. Identify them. SQL>SELECT empno,ename salary X 12 ANNUAL SALARY 2 FROM emp; A. The EMP table does not contain a column called salary. The column is called sal. B. The multiplication operator is *, not x as shown in line 2. C. The ANNUAL SALARY alias cannot include spaces. The alias should read ANNUAL_SALARY or be enclosed in double quotation marks. D. A comma is missing after the column name ENAME. 6. Show the structure of the DEPT table. Select all data from the DEPT table. SQL> DESCRIBE dept SQL> SELECT * 2 FROM dept; 7. Show the structure of the EMP table. Create a query to display the name, jo b, hire date and employee number for each employee with employee number appearing first. Sa ve your SQL statement to a file named plq7.sql. SQL> DESCRIBE emp SQL> SELECT empno, ename, job, hiredate 2 FROM emp; SQL> SAVE p1q7.sql 8. Run your query in the plq7.sql file. SQL> START p1q7.sql 9. Create a query to display unique jobs from the EMP table. SQL> SELECT DISTINCT job 2 FROM emp; 10. Load plq7.sql into the SQL buffer. Name the column headings Emp #, Employe e, Job and Hire Date, respectively. Show how you did this.

SQL> GET p1q7.sql SQL> 1 SELECT empno "Emp #", ename "Employee", SQL> i 2i job "Job", hiredate "Hire Date" 3i SQL> SAVE p1q7.sql REPLACE SQL> START p1q7.sql 11. Display the name concatenated with the job, separated by a comma and space, and name the column Employee and Title. Show how you did this. SQL> SELECT ename ', ' job "Employee and Title" 2 FROM emp. 12. Create a query to display all data from the EMP table. Separate each colum n by a comma. Name the column THE_OUTPUT. SQL> SELECT empno ',' ename ',' job ',' 2 mgr ',' hiredate ',' sal ',' 3 comm ',' deptno THE_OUTPUT 4 FROM emp; Practice 2: 1. Create a query to display the name and salary of employees earning more than $2,850. Save your SQL statement to a file named p2ql.sql. Run your query. 2. Create a query to display the employee name and department number for employ ee number 7566. 3. Modify p2ql.sql to display the name and salary for all employees whose salar y is not in the range of $1500 and $2850. Resave your SQL statement to file named p2q3.sql. Rerun your query. 4. Display the employee name, job, and start date of employees hired between February 20, 1981 and May 1, 1981. Order the query in ascending order by start date. 5. Display the employee name and department number of all employee in departmen ts 10 and 30 in alphabetical order by name. 6. Modify p2q3.sql to list the name and salary of employees who earn more than $1500 and are in department 10 or 30. Label the column Employee and Month Salary, respectively. Resave your SQL statement to a file named p2q6.sql. Rerun your query. 7. Display the name and hire date of every employee who was hired in 1982. 8. Display the name and title of all employees who do not have a manager. 9. Display the name, salary and commission for all employees who ear commission s. Sort data in descending order of salary and commissions. 10. Display the names of all employees where the third letter of their name is

an A. 11. Display the names of all employees that have two Ls in their name and are i n department 30 or their manager is 7782. 12. Display the name, job and salary for all employees whose job is Clerk or Analyst and their salary is not equal to $1000, $3000 or $5000. 13. Modify p2q6.sql to display the name, salary and commission for all employee s whose commission amount is greater than their salary increased by 10%. Rerun your query. Resave your query as p2q13.sql. Practice 3: 1. Write a query to display the current date. Label the column Date. 2. Display the employee number, name, salary and salary increase by 15% express ed as a whole number. Label the column New Salary. Save your SQL statement to a f ile named p3q2.sql. 3. Run the query in the file p3q2.sql. 4. Modify your query p3q2.sql to add a column that will subtract the old salary from the new salary. Label the column Increase. Rerun your query. 5. Display the employee's name, hire date and salary review date which is the f irst Monday after six months of service. Label the column REVIEW. Format the dates to appea r in the format similar to "Sunday, the Seventh of September, 1981". 6. For each employee display the employee name and calculate the number of mont hs 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. 7. Write a query that produces the following for each employee: <employee name > earns <salary> monthly but wants <3 times salary>. Label the column Dream Salaries. 8. Create a query to display name and salary for all employees. Format the sal ary to be 15 characters long, left-padded with $. Label the column SALARY. 9. Write a query that will display the employee's name with the first letter ca pitalized and all others letters lowercase and the length of their name, for all employees who se name starts with J, A, or M. Give each column an appropriate label. 10. Display the name, hire date and day of the week on which the employee start ed. Label

the column DAY. Order the results by the day of the week starting with Mon day. 11. Create a query that will display the employee name and commission amount. If the employee does not earn commission, put "No Commission". Label the column COMM. 12. Create a query that displays the employees' names and indicates the amount of their salaries through asterisks. Each asterisk signifies a hundred dollars. Sort the da ta in descending order of salary. Label the column EMPLOYEE_AND_THEIR_SALARIES. 13. Write a query that displays the grade of all employees based on the value o f the column JOB, as per the table shown below JOB GRADE PRESIDENT A MANAGER B ANALYST C SALESMAN D CLERK E None of the above O Practice 4: 1. Write a query to display the name, department number, and department name f or all employees. 2. Create a unique listing of all jobs that are in department 30. Include the location of department 30 in the output. 3. Write a query to display the employee name, department name and location of all employees who earn a commission. 4. Display the employee name and department name for all employees who have a A in their name. Save your SQL statement in a file called p4q4.sql. 5. Write a query to display the name, job, department number, and department na me for all employees who work in DALLAS. 6. Display the employee name and employee number along with their manager's nam e and manager number. Label the columns Employee, Emp#, Manager and Mgr#, respec tively. Save your SQL statement in a file called p4q6.sql. 7. Modify p4q6.sql to display all employees including King, who has no manager. Resave as p4q7.sql. Run p4q7.sql. 8. Create a query that will display the employee name, department number, and a ll the employees that work in the same department as a given employee. Give each column an appropriate label.

9. Show the structure of the SALGRADE table. Create a query that will display 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 afte r the employee Blake. 11. Display all employees' names and hire dates along with their manager's name and hire date for all employees who were hired before their managers. Label the columns Empl oyee, Emp Hiredate, Manager, and Mgr Hiredate, respectively. Answer 2: 1. SQL> SELECT ename, sal 2 FROM emp 3 WHERE sal> 2850; SQL> SAVE p2q1.sql 2. SQL> SELECT ename, deptno 2 FROM emp 3 WHERE empno=7566; 3. SQL> EDIT p2q1.sql SELECT ename, sal FROM emp WHERE sal NOT BETWEEN 1500 AND 2850 / SQL> START p2q3.sql 4. SQL> SELECT ename, job, hiredate 2 FROM emp 3 WHERE hiredate BETWEEN 4 TO_DATE('20-Feb-1981','DD-MON-YYYY') AND 5 TO_DATE('01-May-1981','DD-MON-YYYY') 6 ORDER BY hiredate; 5. SQL> SELECT ename, deptno 2 FROM emp 3 WHERE deptno IN (10,30) 4 ORDER BY ename; 6. SQL> EDIT p2q3.sql SELECT ename "Employees", sal "Monthly Salary" FROM emp WHERE sal > 1500 AND deptno IN (10,30) / SQL> START p2q6.sql 7.

SQL> SELECT ename, hiredate 2 FROM emp 3 WHERE hiredate LIKE '%82'; 8. SQL> SELECT ename, job 2 FROM emp 3 WHERE mgr IS NULL; 9. SQL> SELECT ename, sal, comm 2 FROM emp 3 WHERE comm IS NOT NULL 4 ORDER BY sal DESC, comm DESC; 10. SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '__A%'; 11. SQL> SELECT ename 2 FROM emp 3 WHERE ename LIKE '%L%L%' 4 AND deptno=30 5 OR mgr=7782; 12. SQL> SELECT ename, job, sal, 2 FROM emp 3 WHERE job IN ('CLERK', 'ANALYST') 4 AND sal NOT IN (1000, 3000, 5000); 13. SQL> EDIT p2q6.sql SELECT ename "Employees", sal "Monthly Salary", comm FROM emp WHERE comm > sal*1.1 / SQL> START p2q13.sql Answer 3: 1. SQL> SELECT sysdate "Date" 2 FROM dual; 2. SQL> SELECT empno, ename, sal, 2 ROUND(sal*1.15,0) "New Salary" 3 FROM emp SQL> SAVE p3q2.sql 3. SQL> START p3q2.sql 4. SQL> EDIT p3q2.sql SELECT empno, ename, sal, ROUND(sal*1.15,0) "New Salary",

ROUND(sal*1.15,0) -sal "Increase" FROM emp / SQL> START p3q2.sql 5. SQL> SELECT ename, hiredate 2 TO_CHAR(NEXT_DAY(ADD_MONTHS(hiredate,6), 3 'MONDAY'), 4 'fmDay, "the" Ddspth "of" Month, YYYY') REVIEW 5 FROM emp 6. SQL> SELECT ename, ROUND(MONTHS_BETWEEN 2 (SYSDATE, hiredate)) MONTHS_WORKED 3 FROM emp 4 ORDER BY MONTHS_BETWEEN(SYSDATE, hiredate); 7. SQL> SELECT ename 2 3 4 5 6 FROM emp; ' earns' TO_CHAR(sal, 'fm$99,999.00') ' monthly but wants' TO_CHAR(sal*3,'fm$99,999.00') '.' "Dream Salaries"

8. SQL> SELECT ename, job 2 3 FROM emp;

LPAD(sal, 15, '$') SALARY

9. SQL> SELECT INITCAP(ename) "Name", 2 LENGTH(ename) "Length" 3 FROM emp 4 WHERE ename LIKE 'J%' 5 OR ename LIKE 'M%' 6 OR ename LIKE 'A%' 10. SQL> SELECT ename, hiredate 2 TO_CHAR(hiredate, 'DAY') DAY 2 FROM emp 3 ORDER BY TO_CHAR(hiredate-1,'d');; 11. SQL> SELECT ename 2 3 FROM emp

NVL(TO_CHAR(comm),'No Commission') COMM

12. SQL> SELECT RPAD(ename,8) ' ' RPAD(' ',sal/100+1,'*') 2 EMPLOYEE_AND_THEIR_SALARIES 3 FROM emp 4 ORDER BY sal DESC; 13. SQL> SELECT job DECODE(job, 'CLERK', 2 3 'E', 'SALESMAN', 'D', 'ANALYST', 'C',

4 5 6 '0') GRADE 7 FROM emp; Answer 4: 1. SQL> SELECT e.ename, e.deptno, d.dname 2 FROM emp e, dept d 3 WHERE e.deptno=d.deptno; 2. SQL> SELECT DISTINCT e.job, d.loc 2 FROM emp e, dept d 3 WHERE e.deptno=d.deptno 4 AND e.deptno=30; 3. SQL> SELECT e.ename, d.dname, d.loc 2 FROM emp e, dept d 3 WHERE e.deptno=d.deptno 4 AND e.comm IS NOT NULL; 4. SQL> SELECT e.ename, d.dname 2 FROM emp e, dept d 3 WHERE e.deptno=d.deptno 4 AND e.ename LIKE '%A%'; 5. SQL> SELECT e.ename, e.job, e.deptno, d.dname 2 FROM emp e, dept d 3 WHERE e.deptno=d.deptno 4 AND d.loc='DALLAS';

'MANAGER', 'B', 'PRESIDENT', 'A',

6. SQL> SELECT e.ename "Employee", e.empno "Emp#", 2 m.ename "Manager", m.empno "Mgr#" 3 FROM emp e, emp m 4 WHERE e.mgr=m.empno; SQL> SAVE p4q6.sql 7. SQL>EDIT p4q6.sql SELECT e.ename "Employee", e.empno "Emp#", m.ename "Manager", m.empno "Mgr#" FROM emp e, emp m WHERE e.mgr=m.empno(+); SQL> SAVE p4q7.sql 8. SQL> SELECT e.deptno department, e.ename employee 2 c.ename colleague 3 FROM emp e, emp c 4 WHERE e.deptno=c.deptno 5 AND e.empno<>c.empno 6 ORDER BY e.deptno, e.ename, c.ename;

9. SQL> DESCRIBE salgrade SQL> SELECT e.ename, e.job, d.dname, e sal, s.grade 2 FROM emp e, dept d, salgrade s 3 WHERE e.deptno=d.deptno 4 AND e.sal BETWEEN s.losal AND s.hisal; 10. SQL> SELECT emp.ename, emp.hiredate 2 FROM emp, emp blake 3 WHERE blake.deptno='BLAKE' 4 AND blake.hiredate<emp.hiredate; 11. SQL> SELECT e.ename "Employee", e.hiredate "Emp Hiredate" 2 m.ename "Manager", m.hiredate "Mgr Hiredate" 3 FROM emp e, emp m 4 WHERE e.mgr=m.empno; 5 AND e.hiredate< m.hiredate; Sample Test (study guide)

1) Organizing tables into groups is good for performance, security and ease of maintenance. In Oracle, when you arrange tables into logical groups, it is c alled:

a) Segments d) Tablespaces

b) Extents e) Partitions

c) Block

2) a)

Smallest unit of Disk I/O in a Database Management System Segment b) Extent e) Record c) Bit

d) Block

3) a)

One or more contiguous Blocks Segment b) Extent e) Record c) Bit

d) Cursor

4)

In JDeveloper a ___________ contains several Projects

5) To Establish an Oracle connection in Jdeveloper, we need to know _______ ______ And _________________________. We can obtain these names By querying the view V$Instance.

6) Difference between Stored Procedure, Stored Function, Trigger and Anonym ous Blocks. ___________________________________________ 7) What is a cursor in PL/SQL and Embedded SQL with C ? Why do I need it ?

What is the corresponding object in Java that performs the same function ? 8) If your Database is slow, describe all the mechanisms that you know that the DBA should investigate to speed it up. Describe them in the order that you would try them.

9) :What is the difference between 3NF and Boyce-Codd Normal Form ? Answer by giving an example of a Table that is in 3NF but is not in 4NF. 10) Give an example of a Table that is in 3NF and BCN, but not in 4NF. Then conv ert the table to 4NF. 11) Can a Foreign Key have Null Values Yes No

12) Can we update the value of a Primary Key ? Explain when and when not ? 13) For Employee and Department Tables, In what order should we create the tabl es ? 14) In what order should we drop the tables ? 15) A view in Oracle corresponds to what in MS-Access ?

16) Give two reasons why one would want to create a view ?

17) What is the difference between a view and a materialized view ? When do you want one or another ?

18) Concurrency and SQL questions in: http://science.kennesaw.edu/~mguimara/3310/tfinal.doc

Concurrency questions in the sample test at coffee.Kennesaw.edu

19) What are three the conditions that would make a field interesting to index o n ?

20) List 3 different types of indexes and describe when to use it and when to no t use it.

21) Explain the Clause ON DELETE/UPDATE RESTRICT/CASCADE/SET NULL. In other words, where and when is this clause applied ?

22) Explain the clause For Update and NOWAIT.

23) What are the three blocks (parts) of an SQL program ?

24) Convert the code on page 649 to Object-Relational DBMS. 25) The purpose of this assignment is to give you experience in the use of objec t-relational database (ORDBMS) concepts. Using the UML diagram of the Art Museum enterprise on the following page, you are to develop a database application usi ng Oracle 8i that allows you to add objects to object tables, modify attribute v alues of objects, establish and modify relationships between objects, execute tr iggers and methods, and query information about objects. To simplify the assignm ent, you will be using the SQL Plus environment to create the appropriate databa se schema, populate the database, and execute queries. The database application that you create must meet the following requirements: 1. Exhibition, ArtObject and Artist must be implemented as object tables. 2. ArtObject must store a REF to Artist for the createdBy relationship (note tha t artist is a required value of ArtObject). 3. Artist must store a VARRAY of REFs to ArtObjects for the inverse of the creat edBy relationship. 4. A trigger must be implemented on the ArtObject side of the createdBy relation ship to update the inverse of the relationship when the artist of an object is i nitialized or modified. You can assume that the createdBy relationship will only be set on the ArtObject side of the relationship. 5. Exhibition must store a nested table of REFs to ArtObjects. You will need to implement procedures to add and remove individual objects to/from the nested tab le. 6. displayExhibit must be implemented as a function that returns an array of Exh ibition object REFs as a derived inverse of the onExhibit relationship. 7. objectCount must be implemented as a function that returns the number of art

objects in an exhibition. 8. For this assignment, you can assume that none of the objects will be deleted (otherwise you would have to write triggers to maintain inverses for delete oper ations). 9. For this assignment, you do not have to be concerned with implementing constr aints on dates for exhibitions and artists. Similar to the School Database example provided to you on the class Object Relat ional web page, you will create sql scripts to demonstrate the functionality of your project using the test sequence on the following page. Test Script (must be executed in the specified order): 1. Create an artist with name Joe. 2. Create an artist with name Bob. 3. Create an artist with name Sue. 4. Create an exhibition with name EX1. 5. Create an exhibition with name EX2. 6. Create at art object with id AO1 (artist is Joe). 7. Create an art object with id AO2 (artist is Joe). 8. Create an art object with id AO3 (artist is Bob). 9. Create an art object with id AO4 (artist is Sue). 10. Create an art object with id AO5 (artist is Sue). 11. Create an art object with id AO6 (artist is Sue). 12. Schedule AO1, AO2, AO3 for EX1 (i.e., add these objects to the nested table in EX1). 13. Schedule AO3, AO4, AO5, AO6 for EX2. 14. Query: Display the idNo, title, year, type, and artist name for each art obj ect. 15. Query: Display the name of each artist together with the idNo of their art o bjects. 16. Query: Display the name, startDate, and endDate of each exhibition together with the idNo of the art objects in the exhibit. 17. Query: For each exhibit, execute objectCount. 18. Query: For each artobject, execute displayExhibits. 19. Change the artist of AO4 to Bob. 20. Repeat the query in step 15. 21. Remove AO6 from the nested table in EX2.

22. Schedule AO6 for EX1 23. Repeat the query in 16. 24. Repeat the query in 17. 25. Repeat the query in 18.

Deliverables: 1. A disk with the .sql files needed to create the database and demonstrate the items above. 2. A hardcopy of the .sql files. 3. A hardcopy of the execution of your test script.

You might also like