Sei sulla pagina 1di 4

PROMPT PROMPT PROMPT Practice 3(Tema 2) PROMPT PROMPT --------------------------------------------------------------------------PROMPT 1. Write a query to display the current date.

Label the column Date . PROMPT SELECT SYSDATE AS "Date" FROM SYS.DUAL; PROMPT PROMPT SQL> SELECT SYSDATE AS "Date" PROMPT 2 FROM SYS.DUAL;; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 2. Display the employee number, name, salary, and salary increase b y 15% expressed as a whole number. Label the column New Salary. Save your SQL st atement to a file named p3q2.sql. PROMPT PROMPT SQL> SELECT empno,ename,sal, PROMPT 2 ROUND(115/100*sal) AS "New Salary" PROMPT 3 FROM emp;; PROMPT SQL> SAVE p3q2 PROMPT PAUSE Waiting ... PROMPT --------------------------------------------------------------------------PROMPT 3. Run your query in the file p3q2.sql. PROMPT PROMPT SQL> @p3q2 PAUSE Press ENTER SELECT empno,ename,sal, ROUND(115/100*sal) AS "New Salary" FROM emp; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 4. Modify your query p3q2.sql to add an additional column that will subtract the old salary from the new salary. Label the column Increase. Rerun y our query. PROMPT PROMPT SQL> EDIT p3q2 PROMPT PROMPT SELECT empno, ename, sal, PROMPT ROUND(115/100*sal) AS "New Salary", PROMPT ROUND(15/100*sal) AS "Increase" PROMPT FROM emp;; PROMPT

PROMPT SQL> START p3q2 PROMPT PAUSE Press ENTER SELECT empno, ename, sal, ROUND(115/100*sal) AS "New Salary", ROUND(15/100*sal) AS "Increase" FROM emp; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 5. Display the employee's name, hire date, and salary review date, which is the first Monday after six months of service. Label the column REVIEW. Format the dates to appear in the format similar to "Sunday, the Seventh of Sept ember, 1981". PROMPT PROMPT SQL> SELECT ename,hiredate, PROMPT 2 TO_CHAR(NEXT_DAY(ADD_MONTHS(hiredate,6),'MONDAY'),'Day, "the" Ddspth "of" Month, YYYY') AS REVIEW PROMPT 3 FROM emp;; PROMPT PAUSE Press ENTER SELECT ename,hiredate, TO_CHAR(NEXT_DAY(ADD_MONTHS(hiredate,6),'MONDAY'),'Day, "the" Ddspth "of" Month, YYYY') AS REVIEW FROM emp; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 6. For each employee display the employee name and calculate the nu mber of months between today and the date the employee was hired. Label the colu mn MONTHS_WORKED. Order your results by the number of months employed. Round the number of months up to the closest whole number. PROMPT PROMPT SQL> SELECT ename, PROMPT 2 ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE)) AS months_worked PROMPT 3 FROM emp PROMPT 4 ORDER BY months_worked;; PROMPT PAUSE Press ENTER SELECT ename, ROUND(MONTHS_BETWEEN(SYSDATE,HIREDATE)) AS months_worked FROM emp ORDER BY months_worked; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 7. Write a query that produces the following for each employee: <em

ployee name> earns <salary> monthly but wants <3 times salary>. Label the column Dream Salaries. PROMPT PROMPT SQL> SELECT ename||' earns '||TO_CHAR(sal,'$99,999.99')||' monthly but wa nts '||TO_CHAR(sal*3,'$99,999.99')||'.' PROMPT 2 AS "Dream Salaries" PROMPT 3 FROM emp;; PROMPT PAUSE Press ENTER SELECT ename||' earns '||TO_CHAR(sal,'$99,999.99')||' monthly but wants '||TO_CH AR(sal*3,'$99,999.99')||'.' AS "Dream Salaries" FROM emp; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 8. Create a query to display name and salary for all employees. For mat the salary to be 15 characters long, left-padded with $. Label the column SA LARY. PROMPT PROMPT SQL> SELECT ename, LPAD(sal,15,'$') AS salary PROMPT 2 FROM emp;; PROMP PAUSE Press ENTER SELECT ename, LPAD(sal,15,'$') AS salary FROM emp; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 9. Write a query that will display the employee s name with the first letter capitalized and all other letters lowercase and the length of their name , for all employees whose name starts with J, A, or M. Give each column an appro priate label. PROMPT PROMPT SQL> SELECT INITCAP(ename) AS "Name", LENGTH(TRIM(ename)) AS "Length" PROMPT 2 FROM emp PROMPT 3 WHERE ename LIKE 'J%' OR ename LIKE 'A%' OR ename LIKE 'M%';; PROMPT PAUSE Press ENTER SELECT INITCAP(ename) AS "Name", LENGTH(TRIM(ename)) AS "Length" FROM emp WHERE ename LIKE 'J%' OR ename LIKE 'A%' OR ename LIKE 'M%'; PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 10. Display the name, hire date, and day of the week on which the em ployee started. Label the column DAY. Order the results by the day of the week s tarting with Monday.

PROMPT PROMPT SQL> PROMPT 2 PROMPT 3 PROMPT PAUSE Press

SELECT ename, hiredate, TO_CHAR(hiredate,'DAY') AS day FROM emp ORDER BY TO_NUMBER(TO_CHAR(hiredate,'D'));; ENTER

SELECT ename, hiredate, TO_CHAR(hiredate,'DAY') AS day FROM emp ORDER BY TO_NUMBER(TO_CHAR(hiredate,'D')); PROMPT PAUSE Waiting ... PROMPT PROMPT --------------------------------------------------------------------------PROMPT 11. Create a query that will display the employee name and commissio n amount. If the employee does not earn commission, put No Commission . Label the c olumn COMM. PROMPT PROMPT SQL> SELECT ename, NVL(TO_CHAR(comm),'No Commision') AS comm PROMPT 2 FROM emp;; PROMPT PAUSE Press ENTER SELECT ename, NVL(TO_CHAR(comm),'No Commision') AS comm FROM emp; PROMPT PROMPT Sfarsit fisier.

Potrebbero piacerti anche