Вы находитесь на странице: 1из 8

CLASS XI

EMPLOYEE TABLE TABLE STRUCTURE Name ------------------------------EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RECORDS EMPNO ENAME --------- ---------7369 SMITH 7499 ALLEN 7521 WARD 7566 JONES 7654 MARTIN 7698 BLAKE 7782 CLARK 7788 SCOTT 7839 KING 7844 TURNER 7876 ADAMS 7900 JAMES 7902 FORD 7934 MILLER JOB --------CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK MGR HIREDATE SAL --------- --------- --------- - -------7902 17-DEC-80 800 7698 20-FEB-81 1600 7698 22-FEB-81 1250 7839 02-APR-81 2975 7698 28-SEP-81 1250 7839 01-MAY-81 2850 7839 09-JUN-81 2450 7566 19-APR-87 3000 17-NOV-81 5000 7698 08-SEP-81 1500 7788 23-MAY-87 1100 7698 03-DEC-81 950 7566 03-DEC-81 3000 7782 23-JAN-82 1300 COMM 300 500 1400 DEPTNO --------20 30 30 20 30 30 10 20 10 30 20 30 20 10 Null? -------NOT NULL Type ---NUMBER(4) VARCHAR2(10) VARCHAR2(9) NUMBER(4) DATE NUMBER(7,2) NUMBER(7,2) NUMBER(2)

DEPARTMENT TABLE TABLE STRUCTURE Name Null? -------------------------- -------DEPTNO NOT NULL DNAME LOC RECORDS DEPTNO --------10 20 30 40 DNAME -------------ACCOUNTING RESEARCH SALES OPERATIONS LOC ---------NEW YORK DALLAS CHICAGO BOSTON Type ---------NUMBER(2) VARCHAR2(14) VARCHAR2(13)

PRACTICAL FILE (SQL QUERIES) S. NO. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. QUESTION DISPLAY ALL THE RECORDS (ALL COLUMNS ) FROM TABLE EMP. DISPLAY EMPNO AND ENAME OF ALL EMPLOYEES FROM TABLE EMP. DISPLAY ENAME,SAL AND SAL ADDED WITH COMM FROM TABLE EMP DISPLAY ENAME JOINED WITH JOB WITH HEADING EMPLOYEE, SAL *12 AS TOTAL SALARY FROM TABLE EMP DISPLAY DISTINCT SAL OF EMPLOYEES FROM TABLE EMP SHOW THE STRUCTURE OF TABLE DEPT WRITE A QUERY TO DISPLAY ENAME AND SAL OF EMPLOYEES WHOSE SALARY IS GREATER THAN OR EQUAL TO 3000 FORM TABLE EMP WRITE A QUERY TO DISPLAY EMPLOYEE NAME, SALARY AND DEPARTMENT NUMBER WHO ARE NOT GETTING COMMISSION FROM TABLE EMP WRITE A QUERY TO DISPLAY EMPLOYEE NUMBER, NAME, SAL AND SAL*12 AS ANNUAL SALARY WHOE COMMISSION IS NOT NULL FROM TABLE EMP WRITE A QUERY TO DISPLAY EMPLOYEE NAME AND SALARY OF THOSE EMPLOYEE WHO DONT HAVE THERE SALARY IN RANGE OF 1500 TO 2000 WAQ TO DISPLAY NAME, JOB, SALARY AND HIREDATE OF EMPLOYEES WHO ARE HIRED BETWEEN FEB 20, 1981 AND MAY 1, 1981, ORDER OFDER THE QUERY IN ASCENDING ORDER OF HIREDATE WAQ TO DISPLAY NAME, JOB, SALARY OF THOSE EMPLOYEE WHO ARE HIRED IN 1981 WRITE A QUERY TO DISPLAY NAME, JOB TITLE AND SALARY WHO DO NOT HAVE MANAGER ANSWER 1 SELECT * 2 FROM EMP ; 1 SELECT EMPNO, ENAME 2 FROM EMP ; 1 SELECT ENAME, SAL, SAL+COMM "TOTAL SALARY" 2 FROM EMP ; 1 SELECT ENAME ||JOB "EMPLOYEE", SAL*12 "TOTAL SALARY" 2 FROM EMP SELECT DISTINCT SAL FROM EMP ; DESCRIBE DEPT ; OR DESC DEPT ; 1 SELECT ENAME, SAL 2 FROM EMP 3 WHERE SAL >=3000 ; 1 SELECT * 2 FROM EMP 3 WHERE COMM IS NULL ; 1 SELECT EMPNO, ENAME, SAL, SAL*12 "ANNUAL SALARY" 2 FROM EMP 3 WHERE COMM IS NOT NULL ; 1 SELECT ENAME, SAL 2 FROM EMP WHERE SAL NOT BETWEEN 1500 AND 2500 1 SELECT ENAME, JOB, SAL, HIREDATE 2 FROM EMP 3 WHERE HIREDATE BETWEEN '20-FEB-1981' AND '01MAY-1981' 4 ORDER BY HIREDATE ASC 1 SELECT ENAME, HIREDATE 2 FROM EMP 3 WHERE TO_CHAR(HIREDATE, 'YYYY') = '1982' ; 1 SELECT ENAME, JOB, SAL 2 FROM EMP 3 WHERE MGR IS NULL

12. 13.

S. NO. 14. 15. 16. 17. 18. 19. 20. 21.

QUESTION WRITE A QUERY TO DISPLAY THE NAME OF EMPLOYEE WHOSE NAME CONTAINS A AS THIRD ALPHABET . WRITE A QUERY TO DISPLAY THE NAME OF EMPLOYEE WHOSE NAME CONTAINS T AS THE LAST ALPHABE. WRITE A QUERY TO DISPLAY THE NAME OF EMP EMPLOYEE WHOSE NAME CONTANS M AS FIRST ALPHABET L AS THIRD ALPHABET WRITE A QUERY TO DISPLAY THE NAME OF EMPLOYEE WHO IS HAVING L AS ANY ALPHABET OF THE NAME WRITE A QUERY TO DISPLAY THE CURRENT SYSTEM DATE WRITE A QUERY TO DISPLAY EMPLOYEE NUMBER, NAME, SALARY , SALARY INCREASE BY 15% EXPRESSED AS A WHOLE NUMBER. LABEL THE COLUMN AS NEW SALARY WRITE A QUERY TO DISPLAY THE EMPLOYES NAME AND SALARY REVIEW DATE, WHICH IS THE DATE AFTER SIX MONTHS OF HIREDATE WRITE A QUERY TO DISPLAY THE EMPLOYEES NAME AND SALARY, REVIEW DATE , WHICH IS THE DATE AFTER SIX MONTHS OF HIREDATE IN FORMAT OF SUNDAY, 7, SEP, 1981. FOR EACH EMPLOYEE DISPLAY EMPLOYEE NAME AND TOTAL NUMBER OF DAYS LAPSED BETWEEN HIREDATE AND TODAY FOR EACH EMPLOYEE DISPLAY EMPLOYEE NAME AND TOTAL NUMBER OF WEEKS LAPSED BETWEEN HIREDATE AND TODAY CREATE A QUERY THAT PRODUCES DISPLAY IN THE FOLLOWING FORMAT <EMPLOYEE NAME> EARNS $ <SALARY> MONTHLY AND WORKING AS <JOB> WRITE A QUERY WHICH DISPLAYS THE EMPLOYEE NAME WITH

ANSWER 1 SELECT ENAME 2 FROM EMP 3 WHERE ENAME LIKE '_ _A%' ; 1 SELECT ENAME 2 FROM EMP 3 WHERE ENAME LIKE '%T' ; 1 SELECT ENAME 2 FROM EMP 3 WHERE ENAME LIKE 'M%L' ; 1 SELECT ENAME 2 FROM EMP 3 WHERE ENAME LIKE '%L%' 1 SELECT SYSDATE 2 FROM DUAL ; 1 SELECT EMPNO, ENAME, SAL, SAL+SAL*.15 "NEW SALARY" 2 FROM EMP ; 1 SELECT ENAME, SAL HIREDATE, ADD_MONTHS(HIREDATE,6) "REVIEW DATE" 2 FROM EMP ; 1 SELECT ENAME, SAL, TO_CHAR(ADD_MONTHS(HIREDATE,6), 'DAY, DD MON YYYY') 2 FROM EMP 1 SELECT ENAME, HIREDATE, ROUND( TO_DATE('12-JAN-2007')- HIREDATE ,0) "NUMBER OF DAYS" 2 FROM EMP 1 SELECT ENAME, HIREDATE, ROUND((TO_DATE('12-JAN-2007')- HIREDATE ) / 7,0) "NUMBER OF WEAK" 2 FROM EMP 1 SELECT ENAME || 'EARNS ' || '$', SAL || ' MONTHLY AND WORKING AS ' , JOB 2 FROM EMP 1 SELECT INITCAP(ENAME), LENGTH(ENAME) "LENGTH

22.

23.

24.

25.

S. NO.

QUESTION THE FIRST LETTER CAPITALIZED AND ALL OTHER LETTERS LOWER CASE AND LENGTH OF THERE NAME STRING WRITE A QUERY TO DISPLAY THE EMPLOYEE NAME AND COMMISSION AMOUNT. IF THE EMPLOYEE DOE NOT EARN COMMISSION , PUT NO COMMISSION WRITE A QUERY TO DISPLAY THE ENAME AND DEPTNO AND DNAME FOR ALL EMPLOYEES USING TABLES EMP AND DEPT WRITE A QUERY TO DISPLAY EMPLOYEE NAME, DEPARTMENT NAME AND LOCATION OF ALL EMPLOYEES WHO HAVE MANAGER NAME BETWEEN 7500 AND 7900 WRITE A QUERY TO DISPLAY THE EMPLOYEE NAME, DEPARTMENT NUMBER AND ALL THE EMPLOYEES THAT WORKED IN THE SAME DEPARTMENT AS A GIVEN EMPLOYEE WRITE A QUERY TO DISPLAY EMPLOYEE NAME AND HIREDATE OF EMPLOYEES WH ARE EMPLOYEED AFTER EMPLOYEE BLAKE WRITE A QUERY TO DISPLAY EMPLOYEE NUMBER, NAME, MANAGERS NAME WITH THEIR MANAGER NUMBER WRITE A QUERY TO DISPLAY THE SUM, AVERAGE, HIGHEST AND LOWEST SALARY OF THE EMPLOYEES GROUPED BY DEPARTMENT NUMBER AND SUB-GROUPED BY JOB WRITE A QUERY TO DISPLAY THE SUM, AVERAGE, HIGHEST AND LOWEST SALARY OF THE EMPLOYEES GROUPED BY DEPARTMENT NUMBER WRITE A QUERY TO DISPLAY THE SUM, AVERAGE, HIGHEST AND LOWEST SALARY OF THE EMPLOYEES GROUPED BY DEPARTMENT AND SUB GROUPED BY JOB 2 1

ANSWER OF NAME" FROM EMP; SELECT ENAME, NVL( TO_CHAR(COMM), 'NO COMMISSION') 2 FROM EMP; 1 SELECT ENAME, DEPT.DEPTNO, DNAME 2 FROM EMP, DEPT 3 WHERE EMP.DEPTNO = DEPT.DEPTNO ; 1 SELECT ENAME,DNAME, MGR 2 FROM EMP, DEPT 3 WHERE MGR BETWEEN 7500 AND 7900 4 AND EMP.DEPTNO = DEPT.DEPTNO ; 1 SELECT ENAME,DEPTNO 2 FROM EMP 3 WHERE DEPTNO = (SELECT DEPTNO FROM EMP WHERE ENAME = &ENAME) 1 SELECT ENAME, HIREDATE 2 FROM EMP 3 WHERE HIREDATE > (SELECT HIREDATE FROM EMP WHERE ENAME ='BLAKE') 1 SELECT E1.EMPNO "EMPNO", E1.ENAME "EMPNAME", E2.ENAME "MANAGER NAME", E2.EMPNO "MGR NO" 2 FROM EMP E1, EMP E2 3 WHERE E1.MGR = E2.EMPNO 1 SELECT DEPTNO, JOB, SUM(SAL), AVG(SAL), MIN(SAL), MAX(SAL) 2 FROM EMP 3 GROUP BY DEPTNO, JOB 1 SELECT DEPTNO, SUM(SAL), AVG(SAL), MAX(SAL), MIN(SAL) 2 FROM EMP 3 GROUP BY DEPTNO 1 SELECT DEPTNO, JOB , SUM(SAL), AVG(SAL), MIN(SAL), MAX(SAL) 2 FROM EMP 3 GROUP BY DEPTNO, JOB

26. 27. 28.

29

30

31

32

33

34

S. NO. 35 36 37 38

QUESTION WRITE A QUERY TO DISPLAY THE NUMBER OF EMPLOYEE WITH THE SAME JOB WRITE A QUERY TO DISPLAY THE NUMBER OF EMPLOYEE WITH THE SAME JOB WRITE A QUERY TO DISPLAY THE AVERAGE OF HIGHEST AND LOWEST SALARY OF EACH DEPARTMENT BEFORE THE FOLLOWING EXERCISE PLEASE ENSURE THAT YOU ARE PROVIDED WITH A TABLE EMPLOYEE WITH FOLLOWING DESCRIPTION

ANSWER 1 SELECT JOB, COUNT(*) 2 FROM EMP 3 GROUP BY JOB ; SELECT JOB, COUNT(JOB) FROM EMP GROUP BY JOB; SELECT AVG( MIN(SAL) + MAX(SAL) ) FROM EMP GROUP BY DEPTNO NAME OF COLUMN TYPE ID NUMBER(4) FIRST_NAME VARCHAR2(30) LAST_NAME VARCHAR2(30) USER_ID VARCHAR(10) SALARY NUMBER (9) CREATE TABLE EMPLOYEE ( ID NUMBER(4), FIRSTNAME VARCHAR(30), LASTNAME VARCHAR2(30), USERID VARCHAR2(10), SALARY NUMBER (9) );

39 40 41

USE DESCRIBE COMMAND TO ENSURE THE TABLE STRUCTURE ADD THE FOLLOWING DATA IN THE ABOVE TABLE AS INSTRUCTED POPULATE TABLE WITH FIRST RECORD MENTIONED THE COLUMN LIST IN THE INSERT CLAUSE POPULATE TABLE WITH NEXT TWO RECORDS WITHOUT MENTIONING THE COLUMN LIST IN THE INSERT CLAUSE

DESCRIBE EMPLOYEE ADD THE DATE AS PER INSTRUCTIONS IN QUERY NO INSERT INTO EMPLOYEE (ID, FIRSTNAME, LASTNAME, USERID) VALUES (1, Dim, Joseph, Jdim, 5000) ; INSERT INTO EMPLOYEE VALUES (2, Jagannath, Mishra, jmishra, 4000) ; INSERT INTO EMPLOYEE VALUES (3, Siddharth, Mishra, smishra, 8000) ;

42

43 44

POPULATE WITH 4TH RECORD AND ENTER ONLY ID AND FIRST_NAME FOR RECORD WITH ID=4 UPDATE RECORD WITH LAST_NAME,

INSERT INTO EMPLOYEE (ID, FIRSTNAME) VALUES (4, 'SHANKAR') ; UPDATE EMPLOYEE

S. NO. USERID AND SALARY 45 46 47 48 49 50 51

QUESTION

ANSWER SET LASTNAME='Giri', USERID = 'sgiri', SALARY = 7000 ; WHRE ID = 4 COMMIT ; UPDATE EMPLOYEE SET LASTNAME=Goutam WHERE ID = 3 ; UPDATE EMPLOYEE SET SALARY = SALRY + 1000 WHERE SALARY < 5000 DELETE FROM EMPLOYEE WHERE FIRSTNAME = Siddharth COMMIT ; DELETE FROM EMPLOYEE ; CREATE TABLE EMPLOYEE1 AS (SELECT ID, FIRSTNAME FROM EMPLOYEE) ; ALTER TABLE EMPLOYEE1 ADD(DEPTID NUMBER(2)) ;

MAKE THE CHANGES PERMANENT MODIFY THE LASTNAME, OF EMPLOYEE 3 TO GOUTAM MODIFY THE SALARY AND INCREASE IT BY 1000, FOR ALL WHO GET SALARY LESS THEN 5000 DELETE THE EMPLOYEE RECORD HAVING FIRSTNAME = Siddharth Make the changes permanent REMOVE entire contents of the table CREATE A TABLE EMPLOYEE1 WITH COLUMNS ID, FIRSTNAME, AND DEPTID FROM TABLE EMPLOYEE AND ALSO CONFIRM THE EXISTENCE OF TABLE EMPLOYEE1

52 53

54

CREATE A VIEW VUEMP1 WHICH SHOULD INCLUDE COLUMN EMPNO, ENAME, AND DEPTNO FROM TABLE EMP CREATE A VIEW VUEMP2 WHICH SHOULD INCLUDE COLUMN EMPNO, ENAME, AND DEPTNO FROM TABLE EMP AND CHANGE THE COLUMN HEADING AS EMPNUMBER, EMPLOYEE, DEPARTMENT. SELECT VIEWNAME AND TEXT FROM THE DATA DICTIONARY USER_VIEWS

DESCRIBE EMPLOYEE1 ; CREATE VIEW VUEMP1 AS (SELECT EMPNO, ENAME, DEPTNO FROM EMP) ; CREATE VIEW VUEMP2(EMPNUMBER, EMPLOYEE, DEPARTMENT) AS (SELECT EMPNO, ENAME, DEPTNO FROM EMP) SELECT VIEW_NAME, TEXT FROM USER_VIEWS ; RESULT VIEW_NAME -----------------------------TEXT ------------------------------------------------------------------------------VUEMP1 (SELECT EMPNO, ENAME, DEPTNO FROM EMP) VUEMP2 (SELECT EMPNO, ENAME, DEPTNO FROM EMP)

S. NO. 55

QUESTION CREATE THE TABLE DEPARTMENT TABLE BASED ON THE FOLLOWING INSTANCE CHART

ANSWER SELECT VIEW_NAME, TEXT FROM USER_VIEWS ; RESULT VIEW_NAME TEXT ----------------------------------------------------VUEMP1 (select empno, ename, deptno from emp) VUEMP2 (select empno, ename, deptno from emp) CREATE TABLE DEPARTMENT (ID NUMBER(8), DNAME VARCHAR2(25)) ; DESCRIBE DEPARTMENT ;

56

CREATE THE TABLE DEPARTMENT TABLE BASED ON THE FOLLOWING TABLE INSTANCE CHART COL.NAME ID NAME DATA TYPE NUMBER VARCHAR2 LENGTH 8 25 AND CONFIRM THE TABLE STRUCTURE POPULATE THE TABLE DEPARTMENT WITH DATA FROM TABLE DEPT. INCLUDING ONLY REQUIRED COLUMNS CREATE THE TABLE EMPLOYEE BASED ON THE FOLLOWING TABLE INSTANCE CHART COL.NAME DATA TYPE LENGTH ID NUMBER 8 FIRSTNAME VARCHAR2 25 LASTNAME VARCHAR2 25 DEPTID NUMBER 8 RENAME TABLE EMPLOYEE TO EMPLOYEE1 DROP TABLE EMPLOYEE2 DROP TABLE EMPLOYEE AND DEPARTMENT CHANGE THE DATA TYPE OF COLUMN PINCODE TO VARCHAR2(10) IN THE TABLE CUSTOMER ADD ONE MORE COLUMN CUSTOMERINGROUP OF DATATYPE VARCHAR2(10)

57 58

INSERT INTO DEPARTMENT SELECT DEPTNO, DNAME FROM DEPT ; CREATE TABLE EMPLOYEE ( ID NUMBER(8), FIRSTNAME VARCHAR2(25), LASTNAME VARCHAR(25), DEPTID NUMBER(8) ) ;

59 60 61 62 63

RENAME EMPLOYEE TO EMPLOYEE1 ; DROP TABLE EMPLOYEE2 ; DROP TABLE EMPLOYEE ; DROP TABLE DEPARTMENT ; ALTER TABLE CUSTOMER MODIFY (PINCODE VARCHAR2(10) ; ALTER TABLE CUSTOMER ADD(CUSTOMERINGROUP VARCHAR2(10) ;