0% found this document useful (0 votes)
158 views

Key and Forigen Key: 1. Create A Table and Specific The Primary

1. The document describes creating tables for employees and departments with columns like employee code, name, designation, department code etc. and inserting sample data. 2. It then lists 17 queries to retrieve information from the tables like listing department names and locations, employees not in certain departments, average salaries by department and more. The queries use joins, aggregation, sorting and other SQL features.

Uploaded by

Rajesh Saharan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
158 views

Key and Forigen Key: 1. Create A Table and Specific The Primary

1. The document describes creating tables for employees and departments with columns like employee code, name, designation, department code etc. and inserting sample data. 2. It then lists 17 queries to retrieve information from the tables like listing department names and locations, employees not in certain departments, average salaries by department and more. The queries use joins, aggregation, sorting and other SQL features.

Uploaded by

Rajesh Saharan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

1.

CREATE A TABLE AND SPECIFIC THE PRIMARY


KEY AND FORIGEN KEY

TABLE OF “EMPLOYEE”

Column Name Type Description


EMP_CODE NUMBER(4) Unique code assigned to each
employee
EMP_NAME VARCHAR(25) Name of employee
DESIG CHAR(10) Designation of the employee
HEAD NUMBER(4) EMP_CODE of the manager under
whom the employee is working
DOJ DATE Date of joining of the employee
BASIC NUMBER(7,2) Salary of the employee
DEPT_CODE NUMBER(2) Code of the department in which
emploee is working

TABLE OF “DEPARTMENT”

Column Name Type Description

DEPT_CODE NUMBER(2) Unique code assigned to


each department
DEPT_NAME VARCHAR2(10) Department name
FLOOR NUMBER(1) Location of the department

Create table DEPARTMENT


(
DEPT_CODE NUMBER(2) PRIMARY KEY,DEPT_NAME
VARCHAR2(10) NOT NULL UNIQUE CHECK(DEPT_NAME
IN(‘ACCOUNT’,’RESERCH’,’SALES’,’OPERATIONS’)),
FLOOR NUMBER(1) NOT NULL
);

create table”EMPLOYEE”
EMP_CODE NUMBER(4) PRIMARY KEY,EMP_NAME
(VARCHAR2(25)NOT NULL,DESIG CHAR(10) NOT NULL,
HEAD NUMBER(4),DOJ DATE NOT NULL,
BASIC NUMBER(6,2) DEFAULT 0,
DEPT_CODE NUMBER(2)REFRENCES
DEPARTMENT(DEPT_CODE)
);
2. OPERATION ON TABLE

(1). INSERT ATABLE:-

SQL>INSERT INTO DEPARTMENT


VALUES(10,’ACCOUNT’,1);
SQL>INSERT INTO DEPARTMENT
VALUES(20,’RESEARCH’,2);
SQL>INSERT INTO DEPARTMENT
VALUES(30,’SALES’,3);

TABLE OF “DEPARTMENT”
DEPT_CODE DEPT_NAME FLOOR
10 Accounts 1
20 Research 2
30 Sales 3
40 Operations 4

SQL>INSERT INTO EMPLOYEES


VALUES(101,’RAJU’,’MANAGER’,NULL,’17-DEC-90’,
6000,20);
SQL>INSERT INTO EMPLOYEE
VALUES(102,’AMAR’,’SALESMAN’,105,’20-FEB-91’,
2000,30);
SQL>INSERT INTO EMPLOYEE
VALUES(103,’RAJ’,’ASSISTANT’,101,’22-FEB-91’,
2500,20);

TABLE OF “EMPLOYEE”

Emp_Code EMP_NAME DESIG HEAD DOJ BASIC Dept_Code


101 Raju Manager 17-dec-90 6000 20
102 Amar Saleman 105 20-feb-91 2000 30
103 Raj Assistant 101 22-feb-93 2500 20
104 Rajiv As.eng. 107 02-apr-91 4200 10
105 Suraj patel As.mng. 28-sep-91 5500 30
106 Binay Saleman 105 01-oct-91 2270 30
107 Ram Engineer 09-jun-91 4500 10
108 Jog lyer Assistant 105 09-dec-92 2250 30
109 Iman khan Steno 105 17-nov-91 2500 30
110 Tinu As.eng. 107 12-jan-93 4000 10
WRITE THE FOLLOWING QUERIES IN SQL:-

QUERY 1:-List all department names and their location from the
“department”table.
SQL>SELECT DEPT_NAME, FLOOR FROM
DEPARTMENT;

QUERY 2:-List the name of the assistant working in department 30,

SQL>SELECT EMP_NAME FROM EMPLOYEE


WHERE DESIG=’Assistant’ AND DEPT_CODE 30;

QUERY 3:-List name of the employees not belonging to the department


10 and 40.
SQL>SELECT EMP_NAME FROM EMPLOYEE
WHERE DEPT_CODE NOT IN(10,40);

QUERY 4:-List the different position available in the EMPLOYEE TABLE.

SQL>SELECT DISTINCT DESIG FROM EMPLOYEE;

QUERY 5:-List the name of the employees whose name either starts
Or ends with ‘R’.
SQL>SELECT EMP_NAMEFROM EMPLOYEE
WHERE EMP_NAME LIKE ’R%’ OR EMP_NAME
LIKE’%R’;

QUERY 6:-List the names,salary and pf(provident fund) of all the


Employees in the EMPLOYEE TABLE.pf is calculated as
10% of the Basic salary.
SQL>SELECT EMP_NAME,BASIC,BASIC*0.1 FROM EMPLOYEE;

QUERY 7:-List the average salary and number of employees working


In each department.
SQL>SELECT DEPT_CODE, COUNT(EMP_NAME),
AVG(BASIC) FROM EMPLOYEE GROUP BY
DEPT_CODE
QUERY 8:-List the designation and the number of employees having
That designation .The result should be in ascending order
Of the number of employees.
SQL>SELECT DESIG,COUNT(EMP_CODE)
FROM EMPLOYEE GROUP BY DESIG ORDER BY
COUNT(EMP_CODE);

QUERY 9:-List average salaries of those departments which are


Employing at least 4 people.
SQL>SELECT DEPT_CODE,AVG(BASIC) FROM EMPLOYEE
GROUP BY DEPT_CODE HAVING COUNT(EMP_CODE)>=4;

QUERY 10:-List the name of the employees along with the name of the
People under whom they are working.
SQL>SELECT WORKER.EMP_NAME,HEADTABLE.EMP_NAME
FROM EMPLOYEE”WORKER”,EMPLOYEE”DEADTABLE”
WHERE WORKER.HEAD=HEADTABLE.EMP_CODE;

QUERY 11:-List the names of the employees who joined the company
Before there respective head did.
SQL>SELECT WORKER. EMP_NAME,WORKER.DOJ,
HEADTABLE.EMP_NAME,HEADTABLE.DOJ
FROM EMPLOYEE”WORKER”,
EMPLOYEE”HEADTABLE”WHERE
WORKER.HEAD=HEADTABLE.EMP_CODE AND WORKER
.DOJ<HEADTABLE.DOJ;

QUERY 12:-List the designation with highest average salary.


SQL> SELECT DESIG FROM EMPLOYEE WHERE DESIG=
(SELECT DESIG FROM EMPLOYEE GROUP BY
DESIG HAVING AVG(BASIC)=(SELECT MAX(AVG
(BASIC))FROM EMPLOYEE
GROUP BY DESIG));

QUERY 13:-List the details of the department which is headed by


Employee105.
SQL> SELECT*FROM DEPARTMENT WHERE DEPT_CODE=
(SELECT DISTINCT DEPT_CODE FROM EMPLOYEE
WHERE HEAD=105);
QUERY 14:-List the name of the employees who are earning the lowest
Salary in each department.
SQL>SELECT EMP_NAME FROM EMPLOYEE WHERE BASIC IN
(SELECT MIN(BASIC)FROM EMPLOYEE GROUP BY
DEPT_CODE);

QUERY 15:-Increase the salary of the employees with emp_code


102 by 500.
SQL>UPDATE EMPLOYEE
SET BASIC=BASIC+500
WHERE EMP_CODE=102;

QUERY 16:-List the name of the employees who are do not head anyone
But still get highest salary in the department.
SQL>SELECT EMP_NAME FROM EMPLOYEE
WHERE BASIC>ANY(SELECT BASIC FROM EMPLOYEE
WHERE DEPT_CODE=30);

QUERY 17:-List the name and designation of those employees who


Work on FLOOR2 of the building.
SQL>SELECT EMP_NAME,DESIG FROM EMPLOYEE,
DEPARTMENT WHERE EMPLOYEE.DEPT_CODE=
DEPARTMENT.DEPT_CODE AND FLOOR=2;

You might also like