SQL Submitted By: Lelia Winstead

Total Page:16

File Type:pdf, Size:1020Kb

SQL Submitted By: Lelia Winstead

1

SQL> -- Submitted by: Lelia Winstead SQL> SQL> set linesize 80 SQL> set pagesize 35 SQL> SQL> -- √ 1. Create a query to display the name and salary of employees SQL> -- earning a salary of more than $2850. Save your SQL statement SQL> -- to a file named p2q1.sql. Run your query. SQL> SQL> SELECT ename, sal 2 FROM EMP 3 WHERE sal > 2850;

ENAME SAL ------JONES 2975 SCOTT 3000 KING 5000 FORD 3000

SQL> save p2q1.sql Created file p2q1.sql SQL> START p2q1.sql

ENAME SAL ------JONES 2975 SCOTT 3000 KING 5000 FORD 3000

SQL> --√ 2. Create a query to display the employee name and department SQL> -- number for employee number 7566. SQL> SQL> SELECT ename,deptno 2 FROM EMP 3 WHERE empno = 7566;

ENAME DEPTNO ------JONES 20

SQL> --√ 3. Modify p2q1.sql to display the name and salary for all SQL> -- employees whose salary is not in the range of $1500 and SQL> -- $2850. Resave your SQL statement to a file named p2q3.sql. SQL> -- Rerun your query. SQL> SQL> edit p2q1.sql

SQL> get p2q3.sql 1 SELECT ename, sal 2 FROM EMP 3* WHERE sal NOT BETWEEN 1500 AND 2850 SQL> / 2

ENAME SAL ------SMITH 800 WARD 1250 JONES 2975 MARTIN 1250 SCOTT 3000 KING 5000 ADAMS 1100 JAMES 950 FORD 3000 MILLER 1300

10 rows selected.

SQL> -- √ 4. Display the employee name, job, and start date of employees SQL> -- hired between February 20, 1981, and May 1, 1981. Order the SQL> -- query in ascending order by start date. SQL> SQL> SELECT ename, job, hiredate 2 FROM EMP 3 WHERE hiredate BETWEEN '20-FEB-81' AND '01-MAY-81' 4 ORDER BY hiredate;

ENAME JOB HIREDATE ------ALLEN SALESMAN 20-FEB-81 WARD SALESMAN 22-FEB-81 JONES MANAGER 02-APR-81 BLAKE MANAGER 01-MAY-81

SQL> -- √ 5. Display the employee name and department number of all SQL> -- employees in departments 10 and 30 in alphabetical order SQL> -- by name. SQL> SQL> SELECT ename, deptno 2 FROM EMP 3 WHERE deptno IN ('10', '30') 4 ORDER BY ename;

ENAME DEPTNO ------ALLEN 30 BLAKE 30 CLARK 10 JAMES 30 KING 10 MARTIN 30 MILLER 10 TURNER 30 WARD 30

9 rows selected. 3

SQL> -- √ 6. Modify p2q3.sql to list the name and salary of employees who SQL> -- earn more than $1500 and are in department 10 or 30. Label the SQL> -- columns Employee and Monthly Salary, respectively. Resave your SQL> -- SQL statement to a file named p2q6.sql. Rerun your query. SQL> SQL> edit p2q3.sql

SQL> get p2q6.sql 1 SELECT ename AS "Employee", sal AS "Monthly Salary" 2 FROM EMP 3* WHERE sal > 1500 AND deptno IN ('10', '30') What happened to ORDER BY? SQL> /

Employee Monthly Salary ------ALLEN 1600 BLAKE 2850 CLARK 2450 KING 5000

SQL> SQL> --√ 7. Display the name and hire date of every employee who was SQL> -- hired in 1982. SQL> SQL> SELECT ename, hiredate 2 FROM EMP 3* WHERE hiredate LIKE '%82';

ENAME HIREDATE ------MILLER 23-JAN-82

SQL> SQL> --√ 8. Display the name and job title of employees who do not have SQL> -- a manager. SQL> SQL> SELECT ename, job 2 FROM EMP 3 WHERE mgr IS NULL;

ENAME JOB ------KING PRESIDENT

SQL> --√ 9. Display the name, salary, and commission for all employees SQL> -- who earn commissions. Sort the data in descending order SQL> -- of salary and commissions (commission within salary). SQL> SQL> SELECT ename, sal, comm 2 FROM EMP 3 WHERE comm IS NOT NULL 4* ORDER BY sal DESC, comm DESC;

ENAME SAL COMM 4

------ALLEN 1600 300 TURNER 1500 0 MARTIN 1250 1400 WARD 1250 500

SQL> --√ 10. Display the names of all employees where the third SQL> -- letter of their name is an A. SQL> SQL> SELECT ename 2 FROM EMP 3 WHERE ename LIKE '__A%';

ENAME ------BLAKE CLARK ADAMS

SQL> SQL> --√ 11. Display the name of all employees who have two Ls in their SQL> -- name and are in department 30 or their manager is 7782. SQL> SQL> SELECT ename 2 FROM EMP 3 WHERE ename LIKE '%L%L%' AND deptno = 30 OR mgr = 7782;

ENAME ------ALLEN MILLER

SQL> --√ 12. Display the name, job, and salary for all employees whose job SQL> -- is Clerk or Analyst and their salary is not equal to $1000, SQL> -- $3000, or $5000. SQL> SQL> SELECT ename, job, sal 2 FROM EMP 3* WHERE job IN ('CLERK', 'ANALYST') AND sal NOT IN (1000,3000,5000);

ENAME JOB SAL ------SMITH CLERK 800 ADAMS CLERK 1100 JAMES CLERK 950 MILLER CLERK 1300

SQL> --√ 13. Modify p2q6.sal to display the name, salary, and commission SQL> -- for all employees whose commission amount is greater than their SQL> -- salary increased by 10%. Rerun your query. Resave your query SQL> -- as p2q13.sql. SQL> SQL> edit p2q6.sql 5

SQL> get p2q13.sql 1 SELECT ename AS "Employee", sal AS "Monthly Salary", comm AS "Commission" 2 FROM EMP 3* WHERE comm > (sal + sal * .10) SQL> /

Employee Monthly Salary COMMISSION ------MARTIN 1250 1400

Recommended publications