Activity 2: (Select clause, Arithmetic Operators)
Database: employee
Create Following tables and insert tuples with suitable constraints
EMPLOYEE
EMPID FIRSTANAME LASTNAME Hire_Date ADDRESS CITY
1001 George Smith 11-May-06 83 first street Paris
1002 Mary Jones 25-Feb-08 842 Vine Ave Losantiville
1012 Sam Tones 12-Sep-05 33 Elm St. Paris
1015 Peter Thompson 19-Dec-06 11 Red Road Paris
1016 Sarath Sharma 22-Aug-07 440 MG Road New Delhi
1020 Monika Gupta 07-Jun-08 9 Bandra Mumbai
EMPSALARY
EMPID SALARY BENEFITS DESIGNATION
1001 10000 3000 Manager
1002 8000 1200 Salesman
1012 20000 5000 Director
1015 6500 1300 Clerk
1016 6000 1000 Clerk
1020 8000 1200 Salesman
Write queries for the following
1. To display FIRSTNAME, LASTNAME, ADDRESS AND CITY of all employees living in PARIS.
2. To display the content of employee table in descending order of FIRSTNAME
3. Select FIRSTNAME and SALARY of salesman
4. To display the FIRSTNAME,LASTNAME, AND TOTAL SALARY of all employees from the table
EMPLOYEE and EMPSALARY. Where TOTAL SALARY is calculated as SALARY+BENEFITS
5. List the Names of employees, who are more than 1 year old in the organization
6. Count number of distinct DESINGATION from EMPSALARY
7. List the employees whose names have exactly 6 characters
8. Add new column PHONE_NO to EMPLOYEE and update the records
9. List employee names, who have joined before 15-Jun-08 and after 16-Jun-07
10. Generate Salary slip with Name, Salary, Benefits, HRA-50%, DA-30%, PF-12%, Calculate gross. Order
the result in descending order of the gross.
QUERIES
To create table Employee:
1
SQL> create table Employee(empid number(10), firstname varchar(20), lastname varchar(20), Hire_date date,
address varchar(20), city varchar(20), primary key(empid));
Table created.
To create table Empsalary:
SQL> create table Empsalary(eid number(10), salary number, benefits number, designation varchar(20),
foreign key(eid) references Employee(empid));
Table created.
To insert values into the table Employee:
SQL> insert into Employee values(1001,'George','Smith','11-May-06','83 first street', 'Paris');
1 row created.
SQL> insert into Employee values(1002,'Mary', 'Jones', '25-Feb-08','842 Vine Avenue', 'Losantiville');
1 row created.
SQL> insert into Employee values(1012, 'Sam','Tones','12-Sep-05','33 Elm St.','Paris');
1 row created.
SQL> insert into Employee values(1015, 'Peter','Thompson','19-Dec-06','11 Red Road', 'Paris');
1 row created.
SQL> insert into Employee values(1016, 'Sarath', 'Sharma','22-Aug-07','440 MG Road','New Delhi');
1 row created.
SQL> insert into Employee values(1020, 'Monika', 'Gupta','07-Jun-08','9 Bandra','Mumbai');
1 row created.
To insert values into the table Empsalary:
SQL> insert into Empsalary values(&eid,&salary,&benefits,'&designation');
Enter value for eid: 1001
Enter value for salary: 10000
Enter value for benefits: 3000
Enter value for designation: Manager
1 row created.
SQL> /
2
Enter value for eid: 1002
Enter value for salary: 8000
Enter value for benefits: 1200
Enter value for designation: Salesman
1 row created.
SQL>/
Enter value for eid: 1012
Enter value for salary: 20000
Enter value for benefits: 5000
Enter value for designation: Director
1 row created.
SQL> /
Enter value for eid: 1015
Enter value for salary: 6500
Enter value for benefits: 1300
Enter value for designation: Clerk
1 row created.
SQL> /
Enter value for eid: 1016
Enter value for salary: 6000
Enter value for benefits: 1000
Enter value for designation: Clerk
1 row created.
SQL> /
Enter value for eid: 1020
Enter value for salary: 8000
Enter value for benefits: 1200
3
Enter value for designation: Salesman
1 row created.
SQL> select * from employee;
EMPID FIRSTNAME LASTNAME HIRE_DATE ADDRESS CITY
------------ ------------------ ------------------ ---------------- ------------- ----------
1001 George Smith 11-MAY-06 83 first street Pari
1002 Mary Jones 25-FEB-08 842 Vine Avenue Losantiville
1012 Sam Tones 12-SEP-05 33 Elm St. Paris
1015 Peter Thompson 19-DEC-06 11 Red Road Paris
1016 Sarath Sharma 22-AUG-07 440 MG Road New Delhi
1020 Monika Gupta 07-JUN-08 9 Bandra Mumbai
6 rows selected.
SQL> select * from empsalary;
EID SALARY BENEFITS DESIGNATION
--------- --------- --------- - -------------------
1001 10000 3000 Manager
1002 8000 1200 Salesman
1012 20000 5000 Director
1015 6500 1300 Clerk
1016 6000 1000 Clerk
1020 8000 1200 Salesman
6 rows selected.
Queries:-
1. To display FIRSTNAME,LASTNAME,ADDRESS and CITY of all
employees
living in PARIS.
SQL> select firstname,lastname,address,city from employee where city='Paris';
FIRSTNAME LASTNAME ADDRESS CITY
-------------------- -------------------- -------------------- --------------------
George Smith 83 first street Paris
Sam Tones 33 Elm St. Paris
Peter Thompson 11 Red Road Paris
2. To display the content of employee table in descending order of FIRSTNAME.
SQL> select * from employee order by firstname desc;
EMPID FIRSTNAME LASTNAME HIRE_DATE ADDRESS CITY
4
--------- -------------------- -------------------- --------- -------------------- ------------
1016 Sarath Sharma 22-AUG-07 440 MG Road New Delhi
1012 Sam Tones 12-SEP-05 33 Elm St. Paris
1015 Peter Thompson 19-DEC-06 11 Red Road Paris
1020 Monika Gupta 07-JUN-08 9 Bandra Mumbai
1002 Mary Jones 25-FEB-08 842Vine Avenue Losantiville
1001 George Smith 11-MAY-06 83 first street Paris
3. Select FIRSTNAME and SALARY of salesman;
SQL> select firstname,salary from employee,empsalary where employee.empid=empsalary.eid
and designation='salesman';
FIRSTNAME SALARY
-------------------- ---------
Mary 8000
Monika 8000
4. To display the FIRSTNAME,LASTNAME and TOTALSALARY of all employees
from the table EMPLOYEE and EMPSALARY. Wher total salary is calculated as
SALARY+BENEFITS.
SQL> select firstname,lastname,(salary+benefits) as totalsalary from employee,empsalary where
employee.empid=empsalary.eid;
FIRSTNAME LASTNAME TOTALSALARY
-------------------- -------------------- -----------
George Smith 13000
Mary Jones 9200
Sam Tones 25000
Peter Thompson 7800
Sarath Sharma 7000
Monika Gupta 9200
6 rows selected.
4. list the names of employees, who are more than 1 year old in the
organization.
SQL> select firstname from employee where months_between(sysdate,hire_date)>12;
FIRSTNAME
--------------------
5
George
Mary
Sam
Peter
Sarath
Monika
6 rows selected.
6. Count number of distinct DESIGNATION from EMPSALARY;
SQL> select count(distinct designation) as no_of_designation from empsalary;
NO_OF_DESIGNATION
-----------------
4
7. list the employees whose names have exactly 6 characters.
SQL> select firstname from employee where length(firstname)=6;
FIRSTNAME
-------------------
George
Sarath
Monika
8. Add new column PHONE_NO to EMPLOYEE and update the records.
SQL> alter table employee add ph_no varchar(10);
Table altered.
SQL> desc employee;
Name Null? Type
------------------------------- -------- ----
EMPID NOT NULL NUMBER
FIRSTNAME VARCHAR2(20)
LASTNAME VARCHAR2(20)
HIRE_DATE DATE
ADDRESS VARCHAR2(20)
CITY VARCHAR2(20)
PH_NO VARCHAR2(10)
SQL> update employee set ph_no=9998887775 where empid=1001;
1 row updated.
6
SQL> update employee set ph_no=9986625485 where empid=1002;
1 row updated.
SQL> update employee set ph_no=9741737354 where empid=1012;
1 row updated.
SQL> update employee set ph_no=9481067721 where empid=1015;
1 row updated.
SQL> update employee set ph_no=9345785423 where empid=1016;
1 row updated.
SQL> update employee set ph_no=8775666345 where empid=1020;
1 row updated.
SQL> select * from employee;
EMPID FIRSTNAME LASTNAME HIRE_DATE ADDRESS CITY PH_NO
--------- -------------------- -------------------- --------- -------------------- --------------- ----------
1001 George Smith 11-MAY-06 83 first street Paris 9998887775
1002 Mary Jones 25-FEB-08 842 Vine Avenue Losantiville 9986625485
1012 Sam Tones 12-SEP-05 33 Elm St. Paris 9741737354
1015 Peter Thompson 19-DEC-06 11 Red Road Paris 9481067721
1016 Sarath Sharma 22-AUG-07 440 MG RoadNew Delhi 9345785423
1020 Monika Gupta 07-JUN-08 9 Bandra Mumbai 8775666345
6 rows selected.
9. list employee names,who have joined before 15-jun-08 and 16-jun-07.
SQL> select firstname from employee where hire_date between '16-jun-07' and '15-jun-08';
FIRSTNAME
--------------------
Mary
Sarath
Monika
7
10. Generate salary slip with FIRSTNAME,SALARY,BENEFITS,HRA-50%,DA-
30%,PF-12%,Calculate GROSS order the result in descending order of the
GROSS.
SQL> select firstname,salary,benefits,salary*0.5 hra,salary*0.3 da,salary*0.12
pf,salary+salary*0.5+salary*0.3-salary*0.12 as gross from employee,empsalary where
employee.empid=empsalary.eid order by gross desc;
FIRSTNAME SALARY BENEFITS HRA DA PF GROSS
-------------------- --------- --------- --------- --------- --------- ---------
Sam 20000 5000 10000 6000 2400 33600
George 10000 3000 5000 3000 1200 16800
Mary 8000 1200 4000 2400 960 13440
Monika 8000 1200 4000 2400 960 13440
Peter 6500 1300 3250 1950 780 10920
Sarath 6000 1000 3000 1800 720 10080
6 rows selected.