Venkat - SQL: Create The Following Tables
Venkat - SQL: Create The Following Tables
Create the following Tables: LOCATION Location_ID Regional_Group 122 NEW YORK 123 124 167 DALLAS CHICAGO BOSTON DEPARTMENT Name ACCOUNTING RESEARCH SALES OPERATIONS
Department_ID 10 20 30 40
JOB Job_ID 667 668 669 670 671 672 Function CLERK STAFF ANALYST SALESPERSON MANAGER PRESIDENT
EMPLOYEE EMPLO YEE_I D 7369 7499 7505 7506 7507 7521 LAST_NA ME SMITH ALLEN DOYLE DENNIS BAKER WARK FIRST_ NAME JOHN KEVIN JEAN LYNN LESLIE CYNTHI A MIDD LE_N AME Q J K S D D JOB_ID 667 670 671 671 671 670 MANA GER_ID 7902 7698 7839 7839 7839 7698 HIRED ATE 17-DEC84 20-FEB85 04-APR85 15MAY-85 10-JUN85 22-FEB85 SALAR Y 800 1600 2850 2750 2200 1250 COMM NULL 300 NULL NULL NULL 500 DEPAR TMENT _ID 20 30 30 30 40 30
Queries based on the above tables: Simple Queries: 1. 2. 3. 4. List all the employee details List all the department details List all job details List all the locations List out first name, last name, salary, commission for all employees
5.
Where Conditions: 8. 9. 10. 11. 12. 13. 14. 15. 16. List the details about SMITH List out the employees who are working in department 20 List out the employees who are earning salary between 3000 and 4500 List out the employees who are working in department 10 or 20 Find out the employees who are not working in department 10 or 30 List out the employees whose name starts with S List out the employees whose name start with S and end with H List out the employees whose name length is 4 and start with S List out the employees who are working in department 10 and draw the salaries more than 3500 List out the employees who are not receiving commission.
17.
Order By Clause: 18. List out the employee id, last name in ascending order based on the employee id. 19. List out the employee id, name in descending order based on salary column 20. list out the employee details according to their last_name in ascending order and salaries in descending order 21. List out the employee details according to their last_name in ascending order and then on department_id in descending order. Group By & Having Clause: 22. How many employees who are working in different departments wise in the organization 23. List out the department wise maximum salary, minimum salary, average salary of the employees 24. List out the job wise maximum salary, minimum salary, and average salaries of the employees.
25. List out the no. of employees joined in every month in ascending order. 26. List out the no. of employees for each month and year, in the ascending order based on the year, month. 27. List out the department id having at least four employees.
28. How many employees in January month. 29. How many employees who are joined in January or September month. 30. How many employees who are joined in 1985. 31. How many employees joined each month in 1985? 32. How many employees who are joined in March 1985. 33. Which is the department id, having greater than or equal to 3 employees joined in April 1985? Sub-Queries 34. 35. 36. 37. Display the employee who got the maximum salary. Display the employees who are working in Sales department Display the employees who are working as Clerk. Display the employees who are working in New York Find out no. of employees working in Sales department.
38. 39. Update the employees salaries, who are working as Clerk on the basis of 10%.
40. Delete the employees who are working in accounting department. 41. Display the second highest salary drawing employee details. 42. Display the Nth highest salary drawing employee details Sub-Query operators: (ALL, ANY, SOME, EXISTS)
Co-Related Sub Queries: 47. Find out the employees who earn greater than the average salary for their department. Joins Simple join 48. List our employees with their department names 49. Display employees with their designations (jobs) 50. Display the employees with their department name and regional groups. 51. How many employees who are working in different departments and display with department name. 52. How many employees who are working in sales department. 53. Which is the department having greater than or equal to 5 employees and display the department names in ascending order? 54. How many jobs in the organization with designations. 55. How many employees working in New York. Non Equi Join: 56. Display employee details with salary grades. 57. List out the no. of employees on grade wise. 58. Display the employ salary grades and no. of employees between 2000 to 5000 range of salary. Self Join: 59. Display the employee details with their manager names. 60. Display the employee details who earn more than their managers salaries. 61. Show the no. of employees working under every manager. Outer Join: 61. Display employee details with all departments. 62. Display all employees in sales or operation departments. Set Operators: 63. List out the distinct jobs in Sales and Accounting Departments. 64. List out the ALL jobs in Sales and Accounting Departments. 65. List out the common jobs in Research and Accounting Departments in ascending order. Answers 1. 2. 3. 4. 5. 6. SQL > Select * from employee; SQL > Select * from department; SQL > Select * from job; SQL > Select * from loc; SQL > Select first_name, last_name, salary, commission from employee; SQL > Select employee_id id of the employee, last_name name", department id as department id from employee;
10. SQL > Select * from employee where salary between 3000 and 4500 11. SQL > Select * from employee where department_id in (20,30) 12. SQL > Select last_name, salary, commission, department_id from employee where department_id not in (10,30) 13. SQL > Select * from employee where last_name like S% 14. SQL > Select * from employee where last_name like S%H 15. SQL > Select * from employee where last_name like S___ 16. SQL > Select * from employee where department_id=10 and salary>3500 17. SQL > Select * from employee where commission is Null 18. SQL > Select employee_id, last_name from employee order by employee_id 19. SQL > Select employee_id, last_name, salary from employee order by salary desc 20. SQL > Select employee_id, last_name, salary from employee order by last_name, salary desc 21. SQL > Select employee_id, last_name, salary from employee order by last_name, department_id desc 22. SQL > Select department_id, count(*), from employee group by department_id 23. SQL > Select department_id, count(*), max(salary), min(salary), avg(salary) from employee group by department_id 24. SQL > Select job_id, count(*), max(salary), min(salary), avg(salary) from employee group by job_id 25. SQL > Select to_char(hire_date,month)month, count(*) from employee group by to_char(hire_date,month) order by month 26. SQL > Select to_char(hire_date,yyyy) Year, to_char(hire_date,mon) Month, count(*) No. of employees from employee group by to_char(hire_date,yyyy), to_char(hire_date,mon) 27. SQL > Select department_id, count(*) from employee group by department_id having count(*)>=4 28. SQL > Select to_char(hire_date,mon) month, count(*) from employee group by to_char(hire_date,mon) having to_char(hire_date,mon)=jan 29. SQL > Select to_char(hire_date,mon) month, count(*) from employee group by to_char(hire_date,mon) having to_char(hire_date,mon) in (jan,sep) 30. SQL > Select to_char(hire_date,yyyy) Year, count(*) from employee group by to_char(hire_date,yyyy) having to_char(hire_date,yyyy)=1985 31. SQL > Select to_char(hire_date,yyyy)Year, to_char(hire_date,mon) Month, count(*) No. of employees from employee where to_char(hire_date,yyyy)=1985 group by to_char(hire_date,yyyy),to_char(hire_date,mon) 32. SQL > Select to_char(hire_date,yyyy)Year, to_char(hire_date,mon) Month, count(*) No. of employees from employee where to_char(hire_date,yyyy)=1985 and to_char(hire_date,mon)=mar group by to_char(hire_date,yyyy),to_char(hire_date,mon) 33. SQL > Select department_id, count(*) No. of employees from employee where to_char(hire_date,yyyy)=1985 and to_char(hire_date,mon)=apr group by to_char(hire_date,yyyy), to_char(hire_date,mon), department_id having count(*)>=3
32.Count the totalsa deptno wise where more than 2 employees exist. SELECT deptno, sum(sal) As totalsal FROM emp GROUP BY deptno HAVING COUNT(empno) > 2