Oracle Database 10g SQL Fundamentals II - Practice 6 6) Retrieving Data Using Subqueries
1. SELECT last_name, department_id, salary FROM employees WHERE (salary, department_id) IN (SELECT salary, department_id FROM employees WHERE commission_pct IS NOT NULL) ;
2. SELECT e.last_name, d.department_name, e.salary FROM employees e, departments d WHERE e.department_id = d.department_id AND (salary, NVL(commission_pct,0)) IN (SELECT salary, NVL(commission_pct,0) FROM employees e, departments d WHERE e.department_id = d.department_id AND d.location_id = 1700) ;
3. SELECT last_name, hire_date, salary FROM employees WHERE (salary, NVL(commission_pct,0)) IN (SELECT salary, NVL(commission_pct,0) FROM employees WHERE last_name = 'Kochhar') AND last_name != 'Kochhar' ;
4. SELECT last_name, job_id, salary FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE job_id = 'SA_MAN') ORDER BY salary DESC ;
5. SELECT employee_id, last_name, department_id FROM employees WHERE department_id IN ( SELECT department_id FROM departments WHERE location_id IN ( SELECT location_id FROM locations WHERE city LIKE 'T%')) ;
6. SELECT e.last_name ename, e.salary salary, e.department_id deptno, AVG(a.salary) dept_avg FROM employees e, employees a WHERE e.department_id = a.department_id AND e.salary > (SELECT AVG(salary) FROM employees WHERE department_id = e.department_id) GROUP BY e.last_name, e.salary, e.department_id ORDER BY AVG(a.salary);
7. SELECT outer.last_name FROM employees outer WHERE NOT EXISTS (SELECT 'X' FROM employees inner WHERE inner.manager_id = outer.employee_id);
SELECT outer.last_name FROM employees outer WHERE outer.employee_id NOT IN (SELECT inner.manager_id FROM employees inner);
8. SELECT last_name FROM employees outer WHERE outer.salary < (SELECT AVG(inner.salary) FROM employees inner WHERE inner.department_id = outer.department_id) ;
9. SELECT last_name FROM employees outer WHERE EXISTS (SELECT 'X' FROM employees inner WHERE inner.department_id = outer.department_id AND inner.hire_date > outer.hire_date AND inner.salary > outer.salary);
10. SELECT employee_id, last_name, (SELECT department_name FROM departments d WHERE e.department_id = d.department_id ) department FROM employees e ORDER BY department;
11. WITH summary AS ( SELECT d.department_name, SUM(e.salary) AS dept_total FROM employees e, departments d WHERE e.department_id = d.department_id GROUP BY d.department_name) SELECT department_name, dept_total FROM summary WHERE dept_total > ( SELECT SUM(dept_total) * 1/8 FROM summary ) ORDER BY dept_total DESC;
Oracle Database 10g SQL Fundamentals II - Practice 5 5) Managing Data in Different Time Zones 1. ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';
2. SELECT TZ_OFFSET ('US/Pacific-New') from dual; SELECT TZ_OFFSET ('Singapore') from dual; SELECT TZ_OFFSET ('Egypt') from dual;
ALTER SESSION SET TIME_ZONE = '-7:00';
SELECT CURRENT_DATE, CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
ALTER SESSION SET TIME_ZONE = '+8:00';
SELECT CURRENT_DATE, CURRENT_TIMESTAMP, LOCALTIMESTAMP FROM DUAL;
3. SELECT DBTIMEZONE,SESSIONTIMEZONE FROM DUAL;
4. SELECT last_name, EXTRACT (YEAR FROM HIRE_DATE) FROM employees WHERE department_id = 80;
5. ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
6. SELECT * FROM sample_dates;
ALTER TABLE sample_dates MODIFY date_col TIMESTAMP; SELECT * FROM sample_dates;
ALTER TABLE sample_dates MODIFY date_col TIMESTAMP WITH TIME ZONE;
7. SELECT e.last_name , (CASE extract(year from e.hire_date) WHEN 1998 THEN 'Needs Review' ELSE 'not this year!' END ) AS "Review " FROM employees e ORDER BY e.hire_date;
8. SELECT e.last_name, hire_date,sysdate , (CASE WHEN (sysdate -TO_YMINTERVAL('15-0'))>= hire_date THEN '15 years of service' WHEN (sysdate -TO_YMINTERVAL('10-0'))>= hire_date THEN '10 years of service' WHEN (sysdate - TO_YMINTERVAL('5-0'))>= hire_date THEN '5 years of service' ELSE 'maybe next year!' END) AS "Awards" FROM employees e;
Oracle Database 10g SQL Fundamentals II - Practice 4 4. Generating Reports by Grouping Related Data
1) SELECT MANAGER_ID , JOB_ID , SUM(SALARY) FROM EMPLOYEES WHERE MANAGER_ID < 120 GROUP BY ROLLUP(MANAGER_ID, JOB_ID) ;
2) SELECT MANAGER_ID , JOB_ID , SUM(SALARY) , GROUPING(MANAGER_ID) , GROUPING(JOB_ID) FROM EMPLOYEES WHERE MANAGER_ID < 120 GROUP BY ROLLUP(MANAGER_ID, JOB_ID) ;
3) SELECT MANAGER_ID , JOB_ID , SUM(SALARY) FROM EMPLOYEES WHERE MANAGER_ID < 120 GROUP BY CUBE(MANAGER_ID, JOB_ID) ;
4) SELECT MANAGER_ID AS MGR , JOB_ID AS JOB , SUM(SALARY) , GROUPING(MANAGER_ID) , GROUPING(JOB_ID) FROM EMPLOYEES WHERE MANAGER_ID < 120 GROUP BY CUBE(MANAGER_ID, JOB_ID) ;
5) SELECT DEPARTMENT_ID , MANAGER_ID , JOB_ID , SUM(SALARY) FROM EMPLOYEES GROUP BY GROUPING SETS ((DEPARTMENT_ID, MANAGER_ID, JOB_ID) , (DEPARTMENT_ID, JOB_ID) , (MANAGER_ID, JOB_ID) ) ;
Oracle Database 10g SQL Fundamentals II - Practice 3 3) Manipulating Large Data Sets
7. a) INSERT ALL WHEN SAL > 20000 THEN INTO SPECIAL_SAL VALUES(EMPID, SAL) ELSE INTO SAL_HISTORY VALUES(EMPID, HIREDATE, SAL) INTO MGR_HISTORY VALUES(EMPID, MGR, SAL) SELECT EMPLOYEE_ID AS EMPID , HIRE_DATE AS HIREDATE , SALARY AS SAL , MANAGER_ID AS MGR FROM EMPLOYEES WHERE EMPLOYEE_ID < 125 ;
8. g) INSERT ALL INTO SALES_INFO VALUES(EMPLOYEE_ID, WEEK_ID, SALES_MON) INTO SALES_INFO VALUES(EMPLOYEE_ID, WEEK_ID, SALES_TUE) INTO SALES_INFO VALUES(EMPLOYEE_ID, WEEK_ID, SALES_WED) INTO SALES_INFO VALUES(EMPLOYEE_ID, WEEK_ID, SALES_THUR) INTO SALES_INFO VALUES(EMPLOYEE_ID, WEEK_ID, SALES_FRI) SELECT EMPLOYEE_ID, WEEK_ID, SALES_MON, SALES_TUE, SALES_WED, SALES_THUR, SALES_FRI FROM SALES_SOURCE_DATA ;
9. 아직 잘 모르겠음.... CREATE TABLE emp_data (first_name VARCHAR2(20) ,last_name VARCHAR2(20) , email VARCHAR2(30) ) ORGANIZATION EXTERNAL ( TYPE oracle_loader DEFAULT DIRECTORY emp_dir ACCESS PARAMETERS ( RECORDS DELIMITED BY NEWLINE CHARACTERSET US7ASCII NOBADFILE NOLOGFILE FIELDS ( first_name POSITION ( 1:20) CHAR , last_name POSITION (22:41) CHAR , email POSITION (43:72) CHAR ) ) LOCATION ('emp.dat') ) ;
10. Alter table emp_hist modify email varchar(45);
MERGE INTO EMP_HIST f USING EMP_DATA h ON (f.first_name = h.first_name AND f.last_name = h.last_name) WHEN MATCHED THEN UPDATE SET f.email = h.email WHEN NOT MATCHED THEN INSERT (f.first_name , f.last_name , f.email) VALUES (h.first_name , h.last_name , h.email);
select * from EMP_HIST;
11. a) update emp3 set department_id =60 where last_name ='Kochhar'; commit; update emp3 set department_id =50 where last_name ='Kochhar'; commit;
b) SELECT VERSIONS_STARTTIME "START_TIME" , VERSIONS_ENDTIME "END_TIME" , DEPARTMENT_ID FROM EMP3 VERSIONS BETWEEN SCN MINVALUE AND MAXVALUE WHERE LAST_NAME ='Kochhar' ;
SQL> DROP TABLE T1; (만약, SQL> DROP TABLE T1 PURGE; 했다면 FLASHBACK을 사용해서 복구 불능)
SQL> SELECT * from tab;
BIN$.....
SQL> show recyclebin SQL> select * from "BIN$각자이름이 상이";
SQL> flashback table t1 to before drop;
SQL> select * from tab; SQL> show recyclebin
SQL> DESC USER_TABLES; Name Null? Type ----------------------------------------------------------------------------------------------------------------- -------- ---------------------------------------------------------------------------- TABLE_NAME NOT NULL VARCHAR2(30) TABLESPACE_NAME VARCHAR2(30) CLUSTER_NAME VARCHAR2(30) IOT_NAME VARCHAR2(30) STATUS VARCHAR2(8) PCT_FREE NUMBER PCT_USED NUMBER INI_TRANS NUMBER MAX_TRANS NUMBER INITIAL_EXTENT NUMBER NEXT_EXTENT NUMBER MIN_EXTENTS NUMBER MAX_EXTENTS NUMBER PCT_INCREASE NUMBER FREELISTS NUMBER FREELIST_GROUPS NUMBER LOGGING VARCHAR2(3) BACKED_UP VARCHAR2(1) NUM_ROWS NUMBER BLOCKS NUMBER EMPTY_BLOCKS NUMBER AVG_SPACE NUMBER CHAIN_CNT NUMBER AVG_ROW_LEN NUMBER AVG_SPACE_FREELIST_BLOCKS NUMBER NUM_FREELIST_BLOCKS NUMBER DEGREE VARCHAR2(10) INSTANCES VARCHAR2(10) CACHE VARCHAR2(5) TABLE_LOCK VARCHAR2(8) SAMPLE_SIZE NUMBER LAST_ANALYZED DATE PARTITIONED VARCHAR2(3) IOT_TYPE VARCHAR2(12) TEMPORARY VARCHAR2(1) SECONDARY VARCHAR2(1) NESTED VARCHAR2(3) BUFFER_POOL VARCHAR2(7) ROW_MOVEMENT VARCHAR2(8) GLOBAL_STATS VARCHAR2(3) USER_STATS VARCHAR2(3) DURATION VARCHAR2(15) SKIP_CORRUPT VARCHAR2(8) MONITORING VARCHAR2(3) CLUSTER_OWNER VARCHAR2(30) DEPENDENCIES VARCHAR2(8) COMPRESSION VARCHAR2(8) DROPPED VARCHAR2(3)
SQL> SELECT TABLE_NAME FROM USER_TABLES;
TABLE_NAME ------------------------------ T_MASTER T_TX T2 BONUS DEPARTMENTS DEPT DUMMY EMPLOYEES EMP JOBS JOB_GRADES JOB_HISTORY LOCATIONS REGIONS SALGRADE EMP2 T1 T_SIMIN T_BOOK T3 T_NEWS T4 T5 DEPT_TEST EMP_TEST EMPLOYEES2 EMP_TEST2 MY_EMPLOYEE COUNTRIES
29 rows selected.
SQL> SHOW RECYCLEBIN ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ------------ ------------------- DEPT_TEST BIN$Wc9zbTsjSWm48vAWRFfw9g==$0 TABLE 2012-11-13:13:52:58 EMP_TEST2 BIN$H1PFfYGRR/OqW8DQyWoFIQ==$0 TABLE 2012-11-13:15:05:50 SQL> DROP TABLE T1;
Table dropped.
SQL> SHOW RECYCLEBIN ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ------------ ------------------- DEPT_TEST BIN$Wc9zbTsjSWm48vAWRFfw9g==$0 TABLE 2012-11-13:13:52:58 EMP_TEST2 BIN$H1PFfYGRR/OqW8DQyWoFIQ==$0 TABLE 2012-11-13:15:05:50 T1 BIN$M+dwv6RYRrCyaU89JNpK4A==$0 TABLE 2012-11-13:19:01:06 SQL> FLASHBACK TABLE T1 TO BEFORE DROP;
Flashback complete.
SQL> SHOW RECYCLEBIN ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ------------ ------------------- DEPT_TEST BIN$Wc9zbTsjSWm48vAWRFfw9g==$0 TABLE 2012-11-13:13:52:58 EMP_TEST2 BIN$H1PFfYGRR/OqW8DQyWoFIQ==$0 TABLE 2012-11-13:15:05:50 SQL> SELECT * FROM TAB;