web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in...

18
DISPLAY TABLES 1.DEPARTMENT TABLE Select * from depart4_4162 Creating table: create table depart4_4162 ( department_id number(10) primary key, department_name varchar2(30) not null, manager_id number(6), location_id number(6)) Inserting data:insert into depart4_4162 values ( 10,'Administration',200,1700); insert into depart4_4162 values ( 20,'Marketing',201,1800); insert into depart4_4162 values ( 50,'Shipping',124,1500); insert into depart4_4162 values ( 60,'IT',103,1400); insert into depart4_4162 values ( 80,'Sales',149,2500); insert into depart4_4162 values ( 90,'Executive',100,1700); insert into depart4_4162 values ( 110,'Accounting',205,1700); insert into depart4_4162 values ( 190,'Contracting',null ,2500); 3.JOB GRADE TABLE Select * from jobgrade_4162 Creating table:create table jobgrade_4162(Grade_Level varchar2(3), Lowest_sal number, highest_sal number) Inserting data:insert into jobgrade_4162 values('A',1000,2999); insert into jobgrade_4162 values('B',3000,5999); insert into jobgrade_4162 values('C',6000,9999);

Upload: tranthu

Post on 30-Jan-2018

222 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

DISPLAY TABLES

1.DEPARTMENT TABLE

Select * from depart4_4162Creating table: create table depart4_4162

( department_id number(10) primary key, department_name varchar2(30) not null, manager_id number(6), location_id number(6))

Inserting data:insert into depart4_4162 values( 10,'Administration',200,1700);insert into depart4_4162 values( 20,'Marketing',201,1800);insert into depart4_4162 values( 50,'Shipping',124,1500);insert into depart4_4162 values( 60,'IT',103,1400);insert into depart4_4162 values( 80,'Sales',149,2500);insert into depart4_4162 values( 90,'Executive',100,1700);insert into depart4_4162 values( 110,'Accounting',205,1700);insert into depart4_4162 values( 190,'Contracting',null ,2500);

3.JOB GRADE TABLE

Select * from jobgrade_4162

Creating table:create table jobgrade_4162(Grade_Level varchar2(3),Lowest_sal number,highest_sal number)

Inserting data:insert into jobgrade_4162 values('A',1000,2999);insert into jobgrade_4162 values('B',3000,5999);insert into jobgrade_4162 values('C',6000,9999);insert into jobgrade_4162 values('D',10000,14999);insert into jobgrade_4162 values('E',1500,24999);insert into jobgrade_4162 values('F',25000,40000);

Page 2: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

4.LOCATION TABLE

Select * from locaion_4162

create table location_4162(location_id number(4),street_address varchar2(40),postal_code varchar2(12),city varchar2(30),state_province varchar2(25),country_id char(2))

Insert into location_4162 values(1400,'2014 Jabberwocky Rd','26192','Southlake','Texas','US');

Insert into location_4162 values(1500,'2011 Interiors Blvd','99236','South San Francisco','California','US');

Insert into location_4162 values(1700,'2004 Charade Rd','98199','Seattle','Washington','US');

Insert into location_4162 values(1800,'460 Bloor St.W.','ON M5S 1X8','Toronto','Ontario','CA');

Insert into location_4162 values(2500,'Magdalen Center The Oxford Science Park ','OX9

9ZB','Oxford','Oxford','UK');

3.JOB GRADE TABLE

Select * from jobgrade_41623.JOB GRADE TABLE

Select * from jobgrade_4162

Page 3: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

4.LOCATION TABLE

Select * from locaion_4162

create table emp7_4162( employee_id number(10) not null, first_name varchar2(20), last_name varchar2(25) not null, email_id varchar2(25) not null, phone_number varchar2(20), hire_date date not null, job_id varchar2(10) not null, salary number(8,2), commission_pct number(2,2), manager_id number(6), department_id number(10) references depart4_4162(department_id))

insert into emp7_4162 values(100,'Steven','King','SKING','515.123.4567','17-JUN-87','AD_PRES',24000,null, null,90);insert into emp7_4162 values(101,'Neena','Kochhar','NKOCHHAR','515.123.4568','21-SEP-89','AD_vp',17000,null, 100,90);insert into emp7_4162 values(102,'Lex','De Haan','LDEHAAN','515.123.4569','13-JAN-93','AD_VP',17000,null,100,90);insert into emp7_4162 values(103,'Alexander','Hunold','AHUNOLD','590.423.4567','03-JAN-90','IT_PROG',9000,null, 102,60);insert into emp7_4162 values(104,'Bruce','Ernst','BERNST','590.423.4568','21-MAY-91','IT_PROG',6000,null ,103,60);insert into emp7_4162 values(107,'Diana','Lorentz','DLORENTZ','590.423.5567','07-FEB-99','IT_PROG',4200,null,103,60);insert into emp7_4162 values(124,'Kevin','Mourgos','KMOURGOS','650.123.5234','16-NOV-99','ST_MAN',5800,null,100,50);insert into emp7_4162 values(141,'Trenna','Rajs','TRAJS','650.121.8009','17-OCT-95','ST_CLERK',3500,null ,124,50);insert into emp7_4162 values(142,'Curtis','Davies','CDAVIES','650.121.2994','29-JAN-97','ST_CLERK',3100,null, 124,50);insert into emp7_4162 values(143,'Randall','Matos','RMATOS','650.121.2874','15-MAR-98','ST_CLERK',2600,null ,124,50);insert into emp7_4162 values(144,'Peter','Vargas','PVARGAS','650.121.2004','09-JUL-98','ST_CLERK',2500,null ,124,50);insert into emp7_4162 values(149,'Eleni','Zlotkey','ZLOTKEY','011.44.1344.429018','29-JAN-00','SA_MAN',10500,0.2,100,80);insert into emp7_4162 values(174,'Ellen','Abel','EABEL','011.44.1644.429267','11-MAY-96','SA_REP',11000,0.3,149,80);insert into emp7_4162 values(176,'Jonathon','Taylor','JTAYLOR','011.44.1644.429265','24-MAR-98','SA_REP',8600,0.2,149,80);insert into emp7_4162 values(178,'Kimberely','Grant','KGRANT','011.44.1644.429263','24-MAY-99','SA_REP',7000,0.15,149,null );insert into emp7_4162 values

Page 4: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

(200,'Jenifer','Whalen','JWHALEN','515.123.4444','17-SEP-97','AD_ASST',4400, null,101,10);insert into emp7_4162 values(201,'Michael','Hartstein','MHARSTEIN','515.123.5555','17-FEB-96','MK_MAN',13000,null ,100,20);insert into emp7_4162 values(202,'Pat','Fay','PFAY','603.123.6666','17-AUG-97','MK_REP',6000,null ,201,20);insert into emp7_4162 values(205,'Shelley','Higgins','SHIGGINS','515.123.8080','07-JUN-94','AC_MGR',12000, null,101,110);insert into emp7_4162 values(206,'William','Gietz','WGIETZ','515.123.8181','07-JUN-94','AC_ACCOUNT',8300, null,205,110);

Query

Q1. The HR department has requested a report of all employees and their job IDs. Display thelast name concatenated with the job ID (separated by a comma and space) and name the column Employee and Title.

Ans. SELECT last_name||', '||job_id "Employee and Title" FROM emp7_4162

Page 5: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

Q2. To familiarize yourself with the data in the EMPLOYEES table, create a query to display allthe data from the EMPLOYEES table. Separate each column output with a comma. Name the column THE_OUTPUT.

Ans. SELECT employee_id ||','|| first_name || ',' || last_name || ',' || email_id || ',' || phone_number || ','|| job_id || ',' || manager_id || ',' || hire_date || ',' || salary || ',' || commission_pct || ',' || department_idTHE_OUTPUTFROM emp7_4162

Practice 2

Q3. The HR departments needs to find high-salary and low-salary employees. Modifylab_02_01.sql to display the last name and salary for all employees whose salary is not in the $5,000–$12,000 range. Place your SQL statement in a text file named lab_02_03.sql.

Ans.SELECT last_name, salaryFROM emp7_4162WHERE salary NOT BETWEEN 5000 AND 12000; Q4. Create a report to display the last name, job ID, and start date for the employees whose lastnames are Matos and Taylor. Order the query in ascending order by start date.

Ans. SELECT last_name, job_id, hire_dateFROM emp7_4162WHERE last_name IN ('Matos', 'Taylor') ORDER BY hire_date

Q5. Members of the HR department want to have more flexibility with the queries that you arewriting. They would like a report that displays the last name and salary of employees who earn more than an amount that the user specifies after a prompt. (You can use the query created in practice exercise 1 and modify it.) Save this query to a file named lab_02_10.sql.

Ans.SELECT last_name, salaryFROM emp7_4162WHERE salary > &sal_amt;

Page 6: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

Q.6 Display the last name, job, and salary for all employees whose job is either that of a salesrepresentative or a stock clerk, and whose salary is not equal to $2,500, $3,500, or $7,000.Ans.SELECT last_name, job_id, salaryFROM emp7_4162WHERE job_id IN ('SA_REP', 'ST_CLERK')AND salary NOT IN (2500, 3500, 7000);

Practice 3

Q7. Write a query that displays the last name (with the first letter uppercase and all other letterslowercase) and the length of the last name for all employees whose name starts with the letters J, A, or M. Give each column an appropriate label. Sort the results by the employees’ last names.Ans.SELECT INITCAP(last_name) "Name", LENGTH(last_name) "Length"FROM emp7_4162WHERE last_name LIKE 'J%'OR last_name LIKE 'M%'OR last_name LIKE 'A%'ORDER BY last_name ;\

Q8. Create a report that produces the following for each employee: earns monthly but wants . Label the column Dream Salaries.Ans. SELECT last_name || ' earns ' || TO_CHAR(salary, 'fm$99,999.00') || ' monthly but wants ' || TO_CHAR(salary * 3, 'fm$99,999.00') || '.' "Dream Salaries"

Page 7: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

FROM emp7_4162

Q9. Display the last name, hire date, and day of the week on which the employee started. Label the column DAY. Order the results by the day of the week, starting with Monday. Ans. SELECT last_name, hire_date, TO_CHAR(hire_date, 'DAY') DAY FROM emp7_4162

Page 8: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

ORDER BY TO_CHAR(hire_date - 1, 'd');

Q10. Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, using the following data: Job Grade AD_PRES A ST_MAN B IT_PROG C SA_REP D ST_CLERK E None of the above 0Ans. SELECT job_id, decode (job_id, 'ST_CLERK', 'E', 'SA_REP', 'D', 'IT_PROG', 'C',

Page 9: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

'ST_MAN', 'B', 'AD_PRES', 'A', '0')GRADEFROM emp7_4162

Practice 4Q. 11. Write a query to display the number of people with the same job.

Ans. SELECT job_id, COUNT(*)

Page 10: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

FROM emp7_4162GROUP BY job_id;

Q. 12 Create a report to display the manager number and the salary of the lowest-paid employee for that manager. Exclude anyone whose manager is not known. Exclude any groups where the minimum salary is $6,000 or less. Sort the output in descending order of salary.

Ans. SELECT manager_id, MIN(salary)FROM emp7_4162WHERE manager_id IS NOT NULLGROUP BY manager_idHAVING MIN(salary) > 6000

Page 11: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

ORDER BY MIN(salary) DESC

Q13. Create a query that displays the total number of employees and, of that total, the number of employees hired in 1995, 1996, 1997, and 1998. Create appropriate column headings.Ans. SELECT COUNT(*) total, SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1995,1,0))"1995", SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1996,1,0))"1996", SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1997,1,0))"1997", SUM(DECODE(TO_CHAR(hire_date, 'YYYY'),1998,1,0))"1998"FROM emp7_4162

Page 12: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

Practice 5Q14. The HR department needs a report of all employees. Write a query to display the last name, department number, and department name for all employees.

Ans. SELECT last_name, department_id, department_nameFROM emp7_4162JOIN depart4_4162USING (department_id);

Q15. The HR department needs a report of employees in Toronto. Display the last name, job, department number, and department name for all employees who work in Toronto.

Ans. SELECT e.last_name, e.job_id, e.department_id, d.department_nameFROM emp7_4162 e JOIN depart4_4162 d ON (e.department_id = d.department_id) JOIN location_4162 l ON (d.location_id = l.location_id) WHERE LOWER(l.city) = 'toronto';

Q16. Create a report to display the last name and employee number of employees along with theirmanager’s last name and manager number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively. Place your SQL statement in a text file named lab_05_04.sql.

Ans. SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager", m.employee_id "Mgr#" FROM emp7_4162 w join emp7_4162 m ON (w.manager_id = m.employee_id);Q17. Modify lab_05_04.sql to display all employees, including King, who has no manager.Order the results by the employee number. Place your SQL statement in a text file named

Page 13: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

lab_05_05.sql. Run the query in lab_05_05.sql.

Ans. SELECT w.last_name "Employee", w.employee_id "EMP#", m.last_name "Manager", m.employee_id "Mgr#" FROM emp7_4162 w LEFT OUTER JOIN emp7_4162 m ON (w.manager_id = m.employee_id);

Q18. Create a report for the HR department that displays employee last names, departmentnumbers, and all the employees who work in the same department as a given employee. Give each column an appropriate label. Save the script to a file named lab_05_06.sql.

Ans. SELECT e.department_id department, e.last_name employee, c.last_name colleague FROM emp7_4162 e JOIN emp7_4162 c ON (e.department_id = c.department_id) WHERE e.employee_id <> c.employee_id ORDER BY e.department_id, e.last_name, c.last_name;

Q19. The HR department needs a report on job grades and salaries. To familiarize yourself withthe JOB_GRADES table, first show the structure of the JOB_GRADES table. Then create a query that displays the name, job, department name, salary, and grade for all employees.

Ans.SELECT e.last_name, e.job_id, d.department_name, e.salary, j.grade_level FROM emp7_4162 e JOIN depart4__4162 d ON (e.department_id = d.department_id) JOIN jobgrades_4162 j ON (e.salary BETWEEN j.lowest_sal AND j.highest_sal);

Q. 20. The HR department wants to determine the names of all employees who were hired after Davies. Create a query to display the name and hire date of any employee hired after employee Davies.

Ans.SELECT e.last_name, e.hire_dateFROM emp7_4162 e JOIN emp7_4162 davies ON (davies.last_name = 'Davies') WHERE davies.hire_date < e.hire_date;

Q21. The HR department needs to find the names and hire dates for all employees who were hiredbefore their managers, along with their managers’ names and hire dates. Save the script to a

Page 14: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

file named lab_05_09.sql.

ans SELECT w.last_name, w.hire_date, m.last_name, m.hire_date FROM emp7_4162 w JOIN emp7_4162 mON (w.manager_id = m.employee_id) WHERE w.hire_date < m.hire_date;

Q22. Write a query that displays the employee number and last name of all employees who work in a department with any employee whose last name contains a u. Place your SQL statementin a text file named lab_06_03.sql. Run your query.

Ans. SELECT employee_id, last_nameFROM emp7_4162WHERE department_id IN (SELECT department_id FROM emp7_4162 WHERE last_name like '%u%');

Page 15: Web viewlab_02_01.sql to display the last name and salary for all employees whose salary is not . in the $5,000–$12,000 range

Q23. Create a report for the HR department that displays the last name and salary of everyemployee who reports to King.

Ans. SELECT last_name, salaryFROM emp7_4162WHERE manager_id = (SELECT employee_id FROM emp7_4162 WHERE last_name = 'King');