SQL

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

1 SQL EXERCISE PROGRAM NO.

. 21 Consider the given tables Employee, Job and answer the following questions with SQL commands Table - Employee Empno Name 123 127 124 125 128 126 122 129 130 131 Amit Manoj Abay Arjun Pooja Ashwini Ramesh Keshav Krithika Dob Nativeplace Hobby music writing music Sex m m m

23-jan-65 delhi 12-dec-76 mumbai 11-aug-75 allahabad 10-mar-74 pune 18-oct-81 chennai 28-oct-81 chennai 09-jan-77 bangalore 24-sep-76 agra Table - Job

Shiksha jain 13-dec-77 mumbai

swimming f gardening m f f chess

14-jun-80 hyderabad sports

swimming m gardening m music f

Sno

Area

Aptdate

Basic Allowance Retdate

Dept

123 agra 127 mathura 124 agra 125 delhi 128 pune 122 chennai 129 mumbai

25-jan-06 5000 1500 22-dec-06 6000 1700 19-aug-07 5500 1200 14-apr-04 8500 2200 13-mar-08 7500 1400 23-apr-03 12000 2000 12-jul-03 9000 2000

27-jan-26 marketing 22-dec-26 finance 19-aug-27 marketing 14-apr-18 sales 13-mar-28 sales 15-jun-25 hr 23-apr-23 hr 12-jul-25 finance

126 bangalore 15-jun-05 3500 1000

130 hyderabad 19-dec-03 14000 2500 19-dec-23 sales 1. Display all the employees whose nativeplace is Mumbai in the descending order of their hobby. select * from employee where nativeplace='mumbai' order by hobby desc; Empno Name 127 125 Manoj shiksha jain Dob Nativeplace Hobby writing Sex m

12-dec-76 mumbai 13-dec-77 mumbai

swimming f

2. Print the name, basic, allowance and the netsalary for those employees where the name starts with A where the netsalary is calculated as basic + allowance. select name, basic, allowance, basic+allowance "net sal" from employee, job where empno=sno and name like "A%"; Name Amit Abay Basic 5000 5500 Allowance 1500 1200 Net sal 6500 6700

2 Arjun 7500 1400 8900

Ashwini 12000 2000 14000 3. Give the total salary spent for the male and female employees. select sex, sum(basic+allowance) from employee, job where empno=sno group by sex; Sex Sum(basic+allowance) f 29200 m 57300 4. Display the name, nativeplace and date of birth of the eldest employee in the organization. select name, nativeplace, dob from employee where dob = (select min(dob) from employee); Name Nativeplace dob amit delhi 23-jan-65 5. Find the total salary expenses of those employees whose retired date is after 20-jan2023. select sum(basic+allowance) "Salary Expense" from job where retdate >'20jan-23'; Salary Expenses 75800 6. Print the name, native place and basic of those employees whose area is the native place. select name, nativeplace, basic from employee, job where empno=sno and area = nativeplace; Name Nativeplace Basic arjun pune 7500 12000 ashwini chennai

7.Display the department names of those employees who joined in the month of april. select dept from job where to_char(aptdate, mon)=apr; Dept Sales Hr 8. Display the number of employees department wise, if the basic salary of that departments employees are more than 100000. select count(*), dept from job group by dept having sum(basic)>10000; Count(*) Dept 2 2 2 finance Hr marketing

3 Sales 9. Count the number of employees appointed month-wise. select count(*), to_char(aptdate,mon) "month" from job group by to_char(aptdate,mon);

3 Count(*) Month 1 1 2 1 1 1 2 Jan Mar Apr Jun Jul Aug Dec

10. Find the appointment date of the first employee, the retirement date of the last employee and the number of records of every department where the basic salary is more than 8000. select dept, min(aptdate), max(retdate), count(*) from job where basic > 8000 group by dept; Dept Min(aptdate) Max(retdate) Count(*) finance 12-jul-03 hr sales 23-apr-03 19-dec-03 12-jul-25 23-apr-23 19-dec-23 1 1 2

11. Add a new record in the employee table. insert into employee values(132, 'mahesh','12-oct-80', 'haridwar', 'chess','m'); Empno Name 123 127 124 125 128 126 122 129 130 131 132 Amit Manoj Abay Arjun Pooja Ashwini Ramesh Keshav Krithika Mahesh Dob Nativeplace Hobby music writing music Sex m m m

23-jan-65 Delhi 12-dec-76 Mumbai 11-aug-75 allahabad 10-mar-74 Pune 18-oct-81 Chennai 28-oct-81 Chennai 09-jan-77 bangalore 24-sep-76 Agra 12-oct-80 Haridwar

Shiksha jain 13-dec-77 Mumbai

swimming f gardening m f f chess

14-jun-80 hyderabad sports

swimming m gardening m music chess f m

12. Delete all the records of the employee whose hobby is music delete from employee where hobby='music'; Empno Name 127 125 128 126 122 manoj arjun pooja ashwini Dob Nativeplace Hobby writing Sex m

12-dec-76 Mumbai 10-mar-74 pune 14-jun-80 hyderabad 18-oct-81 chennai

shiksha jain 13-dec-77 Mumbai

swimming f gardening m sports chess f f

4 129 130 132 ramesh keshav mahesh 28-oct-81 chennai 09-jan-77 bangalore 12-oct-80 haridwar swimming m gardening m chess m

13. Add a new attribute PF to the job table. Assign the value for all the employees as 12% of basic to the attribute PF. alter table job add pf int; update job set pf = basic *0.12; Sno Area 123 agra 127 mathura 124 agra 125 delhi 128 pune 122 chennai 129 mumbai Aptdate Basic Allowance Retdate Dept PF 720 1020 900 420 1440 1080 1680

25-jan-06 5000 1500 22-dec-06 6000 1700 19-aug-07 5500 1200 14-apr-04 8500 2200 13-mar-08 7500 1400 23-apr-03 12000 2000 12-jul-03 9000 2000

27-jan-26 marketing 600 22-dec-26 finance 14-apr-18 sales 13-mar-28 sales 15-jun-25 hr 23-apr-23 hr 12-jul-25 finance 19-dec-23 sales 19-aug-27 marketing 660

126 bangalore 15-jun-05 3500 1000

130 hyderabad 19-dec-03 14000 2500

14. Create a view named v1 which contains name, dob, hobby from the table employee. create view v1 as (select name, dob,hobby from employee); Name manoj Arjun Pooja ashwini ramesh keshav mahesh Dob Hobby

12-dec-76 writing 10-mar-74 gardening 14-jun-80 sports 18-oct-81 chess 28-oct-81 swimming 09-jan-77 gardening 12-oct-80 Chess

shiksha jain 13-dec-77 swimming

15. create a table sales which contains only the records of the sales department. create table sales as (select * from job where dept='sales'); Sno Area 125 delhi 128 pune Aptdate Basic Allownance Retdate Dept PF

14-apr-04 8500 2200 13-mar-08 7500 1400

14-apr-18 sales 1020 13-mar-28 sales 900 19-dec-23 sales 1680

130 hyderabad 19-dec-03 14000 2500

Give the outputs for the following commands: 1. select distinct hobby from employee; Distinct hobby writing swimming gardening sports Chess 2. select count(*), dept from job group by dept; Count(*) Dept 2 2 2 3 finance hr marketing Sales

3. select name, sno, basic from employee, job where eno=sno and basic between 5000 and 10000; Name manoj arjun ramesh Sno Basic 127 6000 128 7500 129 9000

shiksha jain 125 8500

4. select avg(basic) from employee , job where eno=sno and area in ('agra', 'delhi'); Avg(basic) 8500.0000 5.select nativeplace, count(*)from employee group by nativeplace; Nativeplace Count(*) bangalore chennai haridwar hyderabad mumbai pune 1 2 1 1 2 1

You might also like