Create A Table Called 'EMPLOYEE' With The Following Structure

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

Create a table called 'EMPLOYEE' with the following structure:

+-------------------------+-----------------------+-----------+---------+
| Field
| Type
| Null
| Key |
+-------------------------+-----------------------+-----------+---------+
| Emp_No
| number(5)
| NO
| PRI |
| Emp_Name
| varchar(30)
| YES
|
|
| Designation
| varchar(10)
| YES
|
|
| Date_of_Join
| date
| YES
|
|
| Salary
| float
| YES
|
|
| Dept_No
| number(11)
| YES
|
|
+-------------------------+-----------------------+-----------+---------+

SQL> create table employee(


2 Emp_No int NOT NULL PRIMARY KEY,
3 Emp_Name varchar(30),
4 Designation varchar(20),
5 Date_Of_Join date,
6 Salary number,
7 Dept_No int);
Table created.

Create another table called 'DEPARTMENT' with the following structure:


+-------------------+------------------------+
| Field
| Type
|
+-------------------+------------------------+
| Dept_No
| number (11)(PRI) |
| Dept_Name | varchar (30)
|
| Dept_Loc
| varchar(20)
|
+-------------------+------------------------+

SQL> create table department(


2 Dept_NO int PRIMARY KEY,
3 Dept_Name varchar(30),
4 Dept_Loc varchar(20));
Table created.

Create another table called 'DEPARTMENT' with the following structure:

+------------------+----------------------------+
| Field
| Type
|
+------------------+----------------------------+
| Dept_No
| number(11)(PRI)
|
| Dept_Name | varchar(30)
|
| Dept_Loc
| varchar(20)
|
+------------------+----------------------------+

SQL> create table Allowance(


2 Desination varchar(10),
3 Sp_Allowance number,
4 Conveyance number);
Table created.

Insert data into these tables as shown below:


EMPLOYEE TABLE
+---------------+-------------------+--------------------+---------------------+------------+---------------+
| Emp_No | Emp_Name | Designation | Date_of_Join | Salary | Dept_No |
+--------------+--------------------+--------------------+----------------------+------------+---------------+
|
10001 | Robert
| Officer
| 01-dec-1985
|
1000 | 10
|
|
10002 | Allan
| Clerk
| 14-may-1982 |
5000 | 10
|
|
10003 | Martin
| Manager
| 23-dec-1984
|
3500 | 20
|
|
10004 | James
| Analyst
| 22-jul-1990
|
5000 | 30
|
|
10005 | John
| Analyst
| 22-jul-1990
|
4900 | 30
|
|
|
10006 | Jones
| Clerk
| 16-apr-1986
950 | 30
|
+--------------+--------------------+--------------------+----------------------+------------+---------------+

SQL> insert into employee values(10001,'Robert','officer','01-dec-1985',1000,10);


1 row created.
SQL> insert into employee values(10002,'Allan','clerk','14-may-1982',5000,10);
1 row created.
SQL> insert into employee values(10003,'Martin','Manager','23-dec-1984',3500,20);
1 row created.
SQL> insert into employee values(10004,'James','Analyst','22-july-1990',5000,30);

1 row created.
SQL> insert into employee values(10005,'John','Analyst','22-july-1990',4900,30);
1 row created.
SQL> insert into employee values(10006,'Johnes','clerk','16-apr-1986',950,30);
1 row created.
SQL> select * from employee;
EMP_NO EMP_NAME DESIGNATION DATE_OF_J SALARY
-------------- ------------------ -------------------- ----------------- -----------10001
Robert
officer
01-DEC-85
1000

DEPT_NO
-------------10

10002

Allan

clerk

14-MAY-82

5000

10

10003

Martin

Manager

23-DEC-84

3500

20

10004

James

Analyst

22-JUL-90

5000

30

10005

John

Analyst

22-JUL-90

4900

30

clerk

16-APR-86

950

30

10006
Johnes
6 rows selected.
DEPARTMENT TABLE

+------------+-------------------------+------------------+
| Dept_No | Dept_Name
| Dept_Loc
|
+------------+-------------------------+------------------+
| 10
| Marketing
| London
|
| 20
| Accounts
| America
|
| 30
| Sales
| New York
|
| 40
| Software
| Boston
|
| 50
| Production
| Boston
|
+-------------+------------------------+------------------+

SQL> insert into department values(10,'Marketing','london');


1 row created.
SQL> insert into department values(30,'Sales','New York');
1 row created.
SQL> insert into department values(40,'Software','Boston');
1 row created.

SQL> insert into department values(50,'Production','Boston');


1 row created.
SQL> select * from department;
DEPT_NO DEPT_NAME DEPT_LOC
-------------- ------------------- ---------------10
Marketing
London
20
Account
America
30
Sales
New York
40
Software
Boston
50
Production
Boston
ALLOWANCE TABLE
+-----------------------+-----------------------+----------------------+
| Designation
| Sp_Allowance
| Conveyance
|
+----------------------+------------------------+----------------------+
| Manager
|
1000 |
500 |
| Officer
|
800 |
400 |
| Analyst
|
1200 |
500 |
| Clerk
|
500 |
300 |
+-----------------------+------------------------+----------------------+

SQL> insert into Allowance values('manager',1000,500);


1 row created.
SQL> insert into Allowance values('Officer',800,400);
1 row created.
SQL> insert into Allowance values('Analyst',1200,500);
1 row created.
SQL> insert into Allowance values('Clerk',500,300);
1 row created.
SQL> select * from Allowance;
DESINATION SP_ALLOWANCE CONVEYANCE
-------------------- ------------------------ --------------------manager
1000
500
Officer
800
400
Analyst
1200
500
Clerk
500
300

Questions:
Q1. List the Employees belonging to department 20.
SQL> select emp_name from employee where dept_no=20;
EMP_NAME
---------------Martin
Q2. List the Employees who are earning more than 1200 but less than 4000.

SQL> select * from employee where salary between 1200 and 4000;
EMP_NO EMP_NAME DESIGNATION

DATE_OF_J

SALARY

DEPT_NO

------------- ------------------ --------------------10003


Martin
Manager

----------------23-DEC-84

-----------3500

-------------20

Q3. List the Employees who have joined after 1st Jan 84 in the order of
the joining date.

SQL> select * from employee where date_of_join > '01-jan-1934' order by date_of_join;
EMP_NO EMP_NAME DESIGNATION

DATE_OF_J

SALARY

DEPT_NO

------------- ------------------ ---------------------

-----------------

------------

--------------

10002

Allan

clerk

14-MAY-82

5000

10

10003

Martin

Manager

23-DEC-84

3500

20

10001

Robert

officer

01-DEC-85

1000

10

10006

Johnes

clerk

16-APR-86

950

30

10004

James

Analyst

22-JUL-90

5000

30

10005

John

Analyst

22-JUL-90

4900

30

6 rows selected.
Q4. List the Employees who are either in Officer or Manager position.

SQL> select * from employee where designation='Manager' or designation = 'officer';


EMP_NO EMP_NAME DESIGNATION

DATE_OF_J

SALARY

DEPT_NO

------------- ------------------ ---------------------

-----------------

------------

--------------

10001

Robert

officer

01-DEC-85

1000

10

10003

Martin

Manager

23-DEC-84

3500

20

Q5. List the Employees who are located at 'New York'.

SQL> select emp_name from employee e, department d where e.dept_no=d.dept_no and


dept_loc='New York';
EMP_NAME
--------------James
John
Johnes
Q6. List the Employees who are in the Sales department.

SQL> select e.emp_name from employee e, department d where e.dept_no=d.dept_no and


dept_name='Sales';
EMP_NAME
---------------James
John
Johnes
Q7. List the Departments that do not have any Employees.
SQL> select dept_name from department where dept_no not in (select dept_no from employee);
DEPT_NAME
-----------------Software
Production
Q8. List the Employees who are earning more than the Robert.

SQL> select emp_name from employee where salary>(select salary from employee where
emp_name='Robert');
EMP_NAME
----------------Allan
Martin
James
John

Q9. Find out how many employees are there in the organization.

SQL> select count(emp_name) as "total employee" from employee;


total employee
-------------------6
Q10. Find out how many employees are working in Sales department.

SQL> select count(emp_name) from employee e, department d where e.dept_no =


d.dept_no and dept_name='Sales';
COUNT(EMP_NAME)
--------------------------3
Q11. Find out the total salaries paid to the employees.

SQL> select sum(salary) from employee;


SUM(SALARY)
-------------------20350

Q12. What is the average salary paid to the Employees?

SQL> select avg(salary) from employee;


AVG(SALARY)
----------------3391.66667
Q13. What is the minimum salary paid in department 30?

SQL> select min(salary) from employee where dept_no=30;


MIN(SALARY)
------------------950

Q14. Display names and grades of Employees based on their designation.


Designation

Grade

Manager

Officer

Analyst

Clerk

SQL>alter table employee add grades varchar(3);


Table altered.
SQL>update employees
Set grades=A where designation=manager;
Table updated.
SQL>update employees
Set grades=B where designation=officer;
Table updated.
SQL>update employees
Set grades=C where designation=analyst;
Table updated.
SQL>update employees
Set grades=D where designation=clerk;
Q15. Display Employee names and date of join. Joining date should be displayed in the
following format: 26, January 1998

SQL> select emp_name, to_char(date_of_join, 'dd,monthyyyy') as DOJ from employee;


EMP_NAME

DOJ

-----------------

-----------------------------

Robert

01, december 1985

Allan

14, may

Martin

23, december 1984

James

22, july

1990

John

22, july

1990

1982

Johnes

16, april

1986

6 rows selected.
Q16. Find out how long an employee has worked in the organization in
terms of number of:
- Year
- Months
- Days
SQL>

Q17. Display the total salaries department wise.

SQL>select sum(salaries),dept_name
from employee e,department d
where e.emp_no=d.dept_no group by d.dept_name.
Q18. Display the maximum salaries in each department. Along with the
name of the department. The column headings should be like this:
Dept_Name

Max(sum)

SQL>select dept_name,max(salaries) as max(sum)


from employee e,department d
where e.emp_no=d.dept_no group by d.dept_name.
Q19. Display the total salary (salary + sp_allowance - conveyance) of
each Employee in the order of total salary.

SQL>select e.emp_name,e.salary+a.sp_allowance-a.conveyance as total salary


From employee e,allowance a
Where e.designation=a.designation;
Q20. Give the details of the employee with second highest salary.

SQL>

You might also like