8-Adv SQL Eval Inges2 1 Sols
Total Page:16
File Type:pdf, Size:1020Kb
8-adv_SQL_eval_inges2_1_sols
Practice 1 Solutions
1. Display the department that has no employees.
SQL> SELECT deptno,dname 2 FROM dept 3 MINUS 4 SELECT e.deptno,d.dname 5 FROM emp e,dept d 6 WHERE e.deptno = d.deptno;
2. Find the job that was filled in the last half of 1981 and the same job that was filled during the same period in 1982.
SQL> SELECT job 2 FROM emp 3 WHERE hiredate BETWEEN '01-JUN-81' AND '30-DEC-81' 4 INTERSECT 5 SELECT job 6 FROM emp 7 WHERE hiredate BETWEEN '01-JUN-82' AND '30-DEC-82';
Practice 1 Solutions (continued) 4. Produce a list of jobs for departments 10, 30, and 20 in that order. Display job and department number.
COLUMN dummy NOPRINT
SELECT job,deptno,'x' dummy FROM emp WHERE deptno = 10 UNION SELECT job,deptno,'y' FROM emp WHERE deptno = 30 UNION SELECT job,deptno,'z' FROM emp WHERE deptno = 20 ORDER BY 3 /
5. List the department number for departments without the job title ANALYST.
SQL> SELECT deptno 2 FROM dept 3 MINUS 4 SELECT deptno 5 FROM emp 6 WHERE job = 'ANALYST'; 8-adv_SQL_eval_inges2_1_sols
6. List all job titles in department 10 and 20 that do not occur in both departments.
SQL> SELECT job 2 FROM emp 3 WHERE deptno in (20,10) 4 MINUS 5 (SELECT job 6 FROM emp 7 WHERE deptno = 10 8 INTERSECT 9 SELECT job 10 FROM emp 11 WHERE deptno = 20);
Practice 2 Solutions
2. Find all employees who are not a supervisor. a. Do this using the EXISTS operator first.
SQL> SELECT outer.ename 2 FROM emp outer 3 WHERE EXISTS (SELECT empno 4 FROM emp inner 5 WHERE inner.mgr = outer.empno);
b. Can this be done using the IN operator? Why, or why not?
SQL> SELECT outer.ename 2 FROM emp outer 3 WHERE outer.empno NOT IN (SELECT inner.mgr FROM emp inner);
This alternative solution is not a good one. The subquery picks up a NULL value, and hence the entire query returns no rows. The reason is that all conditions that compare a NULL value, result in NULL. So, whenever NULL values are likely to be part of the value set, do not use NOT IN as a substitute for NOT EXISTS.
Practice 2 Solutions (continued)
3. Write a query to find all employees who make more than the average salary in their department. Display employee number, salary, department number, and the average salary for the department. Sort by average salary.
SQL> SELECT e.ename ename, e.sal salary, 2 e.deptno deptno, AVG(a.sal) dept_avg 3 FROM emp e, emp a 4 WHERE e.deptno = a.deptno 5 AND e.sal > (SELECT AVG(sal) 6 FROM emp 7 WHERE deptno = e.deptno) 8-adv_SQL_eval_inges2_1_sols
8 GROUP BY e.ename, e.sal, e.deptno 9 ORDER BY AVG(a.sal);
4. Write a query to display employees who earn less than half the average salary in their department.
SQL> SELECT ename 2 FROM emp outer 3 WHERE outer.sal < (SELECT avg(inner.sal/2) 4 FROM emp inner 5 WHERE inner.deptno = 6 outer.deptno);
5. Write a query to display employees who have one or more co-workers in their department with later hiredates but higher salaries.
SQL> SELECT ename 2 FROM emp outer 3 WHERE EXISTS (SELECT inner.empno 4 FROM emp inner 5 WHERE inner.deptno = outer.deptno 6 AND inner.hiredate > 7 outer.hiredate 8 AND inner.sal > outer.sal);
3. Hierarchical Retrieval
2. Write a hierarchical query showing employee number, manager number, and employee name for all employees who are two levels below JONES (employee 7566).
SQL> SELECT empno, mgr, level, ename 2 FROM emp 3 WHERE LEVEL = 3 4 CONNECT BY mgr = PRIOR empno 5 START WITH empno = 7566;
4. Generate Scripts to Generate Scripts 1. Write a SQL script file to drop all objects (tables, views, indexes, sequences, synonyms and so on) that you own. The output shown is just a guideline.
SET HEADING OFF ECHO OFF FEEDBACK OFF TERMOUT OFF SET PAGESIZE 0
SPOOL dropall.sql
SELECT 'drop ' || OBJECT_TYPE || ' ' || OBJECT_NAME || ';' FROM user_objects ORDER BY object_type /
SPOOL off 8-adv_SQL_eval_inges2_1_sols
SET HEADING ON ECHO ON FEEDBACK ON TERMOUT ON SET PAGESIZE 24
2. Write a SQL script to produce a CREATE TABLE statement for every table you own. Hints: • The data dictionary view USER_TAB_COLUMNS contains all the information that you need. • Assume that you have only relational tables in your database and no LOBs. • Write three SELECT statements combined with the UNION operator. – The first one builds the first part of the CREATE TABLE statement. – The second one picks up the column definition for all columns but the last column. – The third one picks up the column definition for the last column.
4. Generate Scripts to Generate Scripts (continued)
SET FEEDBACK OFF ECHO OFF HEAD OFF SET PAGESIZE 9999 COLUMN sort1 NOPRINT COLUMN sort2 NOPRINT BREAK ON sort1 skip1
SPOOL c:\create.sql
SELECT DISTINCT table_name sort1, 0 sort2, 'CREATE TABLE ' || table_name ||' (' FROM user_tab_columns UNION SELECT table_name sort1, column_id sort2, ' '|| column_name || DECODE(data_type,'DATE',' DATE', 'LONG',' LONG', 'CHAR',' CHAR('||data_length||')', 'VARCHAR2',' VARCHAR2('||data_length||')', 'NUMBER', DECODE(data_precision,null,' NUMBER', ' NUMBER('||data_precision||','|| data_scale||') '), '*****Invalid datatype******')|| DECODE(nullable,'N',' NOT NULL',null)||',' FROM user_tab_columns c WHERE column_id < (SELECT MAX(column_id) FROM user_tab_columns WHERE table_name = c.table_name) UNION SELECT table_name sort1, column_id sort2, 8-adv_SQL_eval_inges2_1_sols
' '|| column_name || DECODE(data_type,'DATE',' DATE', 'LONG',' LONG', 'CHAR',' CHAR('||data_length||')', 'VARCHAR2',' VARCHAR2('||data_length||')', 'NUMBER', DECODE(data_precision,null,' NUMBER', ' NUMBER('||data_precision||','|| data_scale||') '), '*****Invalid datatype*****')|| DECODE(nullable,'N',' NOT NULL',null)||') /' FROM user_tab_columns c WHERE column_id = (SELECT max(column_id) FROM user_tab_columns WHERE table_name = c.table_name) ORDER BY 1,2 /
SPOOL OFF COLUMN sort1 CLEAR COLUMN sort2 CLEAR SET FEEDBACK ON HEAD ON ECHO ON SET PAGESIZE 24