1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| CREATE OR REPLACE PROCEDURE PROC_DROP_TAB_IF_EXISTS( v_table_name IN VARCHAR2 ) IS v_count NUMBER; BEGIN v_count := 0; SELECT COUNT(1) INTO v_count FROM USER_TABLES WHERE TABLE_NAME = v_table_name;
IF v_count > 0 THEN EXECUTE IMMEDIATE 'DROP TABLE '|| v_table_name ||' CASCADE CONSTRAINTS PURGE'; END IF; END;
EXECUTE PROC_DROP_TAB_IF_EXISTS('DEPARTMENT'); EXECUTE PROC_DROP_TAB_IF_EXISTS('EMPLOYEE');
CREATE TABLE DEPARTMENT ( DEPT_ID NUMBER NOT NULL , DEPT_NAME VARCHAR2(10 CHAR) , CONSTRAINT DEPARTMENT_PK PRIMARY KEY(DEPT_ID) );
CREATE TABLE EMPLOYEE ( EMPLOYEE_ID NUMBER NOT NULL , DEPT_ID NUMBER NOT NULL , NAME VARCHAR2(20 CHAR) , SALARY NUMBER , CONSTRAINT EMPLOYEE_PK PRIMARY KEY (EMPLOYEE_ID) , CONSTRAINT DEPARTMENT_FK FOREIGN KEY (DEPT_ID) REFERENCES DEPARTMENT(DEPT_ID) );
INSERT INTO DEPARTMENT VALUES( 1, 'IT'); INSERT INTO DEPARTMENT VALUES( 2, 'ACCT'); INSERT INTO DEPARTMENT VALUES( 3, 'MAINT' );
INSERT INTO EMPLOYEE VALUES( 1, 1, 'Terry' , 50000); INSERT INTO EMPLOYEE VALUES( 2, 1, 'Emily' , 50000); INSERT INTO EMPLOYEE VALUES( 3, 2, 'Joyce' , 38600); INSERT INTO EMPLOYEE VALUES( 4, 2, 'Bob' , 32800); INSERT INTO EMPLOYEE VALUES( 5, 2, 'Phoebe', 32800); INSERT INTO EMPLOYEE VALUES( 6, 3, 'Olive' , 28000); COMMIT;
SELECT EM.EMPLOYEE_ID AS EMPLOYEE_ID ,EM.NAME AS EMPLOYEE_NAME ,EM.SALARY AS EMPLOYEE_SALARY ,RANK() OVER (ORDER BY EM.SALARY DESC) AS SALARY_RANK FROM EMPLOYEE EM;
SELECT EM.EMPLOYEE_ID AS EMPLOYEE_ID ,EM.NAME AS EMPLOYEE_NAME ,EM.SALARY AS EMPLOYEE_SALARY ,ROW_NUMBER() OVER (ORDER BY EM.SALARY DESC) AS SALARY_RANK FROM EMPLOYEE EM;
SELECT EM.EMPLOYEE_ID AS EMPLOYEE_ID ,EM.NAME AS EMPLOYEE_NAME ,EM.SALARY AS EMPLOYEE_SALARY ,DENSE_RANK() OVER (ORDER BY EM.SALARY DESC) AS SALARY_RANK FROM EMPLOYEE EM;
SELECT EM.EMPLOYEE_ID AS EMPLOYEE_ID ,EM.NAME AS EMPLOYEE_NAME ,DP.DEPT_NAME AS DEPT_NAME ,EM.SALARY AS EMPLOYEE_SALARY ,DENSE_RANK() OVER (PARTITION BY EM.DEPT_ID ORDER BY EM.SALARY DESC) AS SALARY_RANK FROM EMPLOYEE EM INNER JOIN DEPARTMENT DP ON EM.DEPT_ID = DP.DEPT_ID;
|