SQL Assignment: Employee Database Queries
SQL Assignment: Employee Database Queries
QUERY
LANGUAGE
(SQL)
STRUCTURE QUERY LANGUAGE:
1 Assignment 1
2 Assignment 2
3 Assignment 3
4 Assignment 4
5 Assignment 5
ASSIGNMENT-1
DATE-
PROBLEM:
EMPLOYEE(EMP_NO,EMP_NAME,MANAGER_NO,SALARY)
Create table through appropriate SQL command. Define all integrity constraints and
enter sufficient data through user friendly from design. Write SQL command and
output.
1. Retrieve the names of the employees and names of their
respective managers from the employee table.
2. Retrieve the names of the employee who is earning second
maximum salary.
3. Retrieve the names of the employees whose salary is greater than
the salary of all employees whose manager no. is 7.
4. Get the details of all employees whose salary is greater than the
average salary of the employee.
TABLE CREATION:
10 rows selected.
QUERIES:
(1) Retrieve The Names Of The Employees And Names Of Their Respective
Managers From The Employee Table.
EMP_NAME MANAGER_NAME
------------------ ---------------------------
A. Paul A. Paul
M. Deb A. Paul
A. Datta A. Paul
M. Banerjee A. Paul
D. Das T. Sikdar
K. Manna T. Sikdar
T. Sikdar T. Sikdar
M. Datta A. Jana
M. Bose A. Jana
A. Jana A. Jana
10 rows selected.
(2) Retrieve The Names Of The Employee Who Is Earning Second Maximum Salary.
SQL> SELECT EMP_NAME FROM EMPLOYEE WHERE SALARY= ( SELECT MAX(SALARY) FROM
EMPLOYEE WHERE SALARY<(SELECT MAX(SALARY) FROM EMPLOYEE));
EMP_NAME
------------------
M. Banerjee
(3) Retrieve the names of the employees whose salary is greater than the salary of
all employees whose manager no. is=7.
EMP_NAME
---------------
A. Paul M. Deb A. Datta
K. Manna M. Banerjee
(4) Get the details of all employees whose salary is greater than the average salary
of the employee.
________________
Teacher Signature
ASSIGNMENT-2
DATE-
PROBLEM:
Create table through appropriate SQL command. Define all integrity constraints
and enter sufficient data through user friendly from design. Write SQL command
and output.
27 rows selected.
SQL> SELECT * FROM PROJECT;
PRO_CODE PRO_NAME
PRO_NAMESTART_DATE
START_DATE END_DATE PRO_MANAGER
10 Visual Basic 25-JAN-07 30-SEP-07 A. D. K.
50 Flip Flop 15-OCT-08 01-JAN-09 A. Jana
22 C++ 10-MAR-08 13-DEC-08 A. Paul
76 Chemistry 04-MAR-09 15-NOV-09 R. Bose
100 Optics 03-APR-09 10-MAY-09 T. Paul
56 Environment 10-JAN-10 05-MAR-10 S. Paul
89 Astro Physics 24-MAY-10 30-JUL-10 A. Saha
60 GIS 10-SEP-10 25-JAN-11 D. Roy
15 Bonding 14-JAN-10 27-MAR-10 N. Mukherjee
32 Microprocessor 12-DEC-11 01-JAN-12 D. Das
10 rows selected.
QUERIES:
(1) Display the name of project manager who are also department
head.
SQL> SELECT P.PRO_MANAGER FROM PROJECT P, DEPARTMENT1 D WHERE
D.DEPT_HEAD=P.PRO_MANAGER;
PRO_MANAGER
----------------------
A. D. K.
(2) Display all projects sorted by project name. Specify who manages each
project, which employee work on it and what department the project
manager is in. Within a project sort employees by employee name.
10 rows selected.
EMP_NAME
----------------
A. Saha B. Roy K.
Majumder P. Saha S.
Hazra A. Roy
S. Bose A. Jana P.
Mishra T. Paul
N. Mukherjee
EMP_NAME
----------------
S. Sarkar R. Bose S.
Thakur J. Verma D.
Bindra J. Deb
17 rows selected.
_______________
Teacher Signature
ASSIGNMENT-3
DATE-
PROBLEM:
Consider the following schema of a relational database:
EMPLOYEE (E_CODE, E_NAME, STREET, CITY, SALARY)
COMPANY (C_CODE, C_NAME, CITY)
WORKS (E_CODE, C_CODE)
MANAGES (E_CODE, E_NAME, MANAGER_NAME)
Create table through appropriate SQL command. Define all integrity constraints and
enter sufficient data through user friendly from design. Write SQL command and
output.
1. Find the names of all employees who work for FIRST BANK
CORPORATION.
2. Find the names and cities of residence of all companies who work for
FIRST BANK CORPORATION.
3. Find the names, street and cities of residence of all employees who
work for FIRST BANK CORPORATION and earn more than 50000 per
month.
4. Find all employees who live in the city where the company for
which they work is located.
5. Find all employees who live in the same city and on the same street as
their manager.
6. Find the employees in the database who earn more than every
employee of SMALL BANK CORPORATION.
7. Find all employees in the database who don’t work for FIRST BANK
CORPORATION.
8. Assume that the companies may be located in several cities. Find all
companies located in every city in which FIRST BANK CORPORATION is
located.
TABLE CREATION:
E_CODE C_CODE
1 100
3 102
5 103
2 101
10 100
11 101
6 100
9 102
4 101
7 103
8 102
12 100
12 rows selected.
(1) Find the names of all employees who work for FIRST BANK
CORPORATION.
E_NAME
--------------
A. Paul D. Paul A.
Datta E. Sheik
(2) Find the names and cities of residence of all companies who work for FIRST
BANK CORPORATION.
E_NAME CITY
A. Paul Kolkata
D. Paul Kolkata
A. Datta Kolkata
E. Sheik Mumbai
(3) Find the names, street and cities of residence of all employees who work for
FIRST BANK CORPORATION and earn more than 50000 per month.
(4) Find all employees who live in the city where the company for
which they work is located.
SQL> SELECT E_NAME FROM EMPLOYEE, COMPANY, WORKS WHERE
EMPLOYEE.E_CODE=WORKS.E_CODE AND COMPANY.C_CODE=WORKS.C_CODE AND
[Link]=[Link];
E_NAME
-------------
A. Paul M. Deb D. Paul
M. Raut A. Datta J. Dey
6 rows selected.
(5) Find all employees who live in the same city and on the same street as their
manager.
E_NAME
---------------
A. Datta M. Deb
(6) Find the employees in the database who earn more than every employee of
SMALL BANK CORPORATION.
E_NAME
-------------
A. Datta N. Roy D.
Paul
(7) Find all employees in the database who don’t work for FIRST BANK
CORPORATION.
8 rows selected.
(8) Assume that the companies may be located in several cities. Find
all companies located in every city in which FIRST BANK CORPORATION
is located.
C_NAME
-------------
IBM
_______________
Teacher Signature
ASSIGNMENT-4
DATE-
PROBLEM:
1. List the name of all books where price is less than 300.00.
2. Print the name of all students who have not borrowed any books.
3. List the name of the author who has maximum number of books.
4. Print the name and number of copies of the books.
TABLE CREATION:
7 rows selected.
8 rows selected.
QUERIES:
(1) List the name of all books where price is less than 300.00.
TITLE
------------------------------
FIVE POINT SOMEONE FIVE POINT
SOMEONE
(2) Print the name of all students who have not borrowed any books.
SQL> SELECT NAME FROM STUDENT WHERE CARD_NO NOT IN(SELECT CARD_NO
FROM BORROW);
NAME
-------------------------
T. SIKDAR S. NAGRANI S.
BISWAS A. DATTA
(3) List the name of the author who has maximum number of books.
SQL> SELECT TITLE, COUNT (*) FROM BOOK GROUP BY TITLE, AUTHOR;
_________________
Teacher Signature
ASSIGNMENT-5
DATE-
PROBLEM:
Consider the following schema of a relational database:
SAILORS (S_ID, S_NAME, RATING, AGE)
S_ID Must Be Between 100 And 10000.
BOATS (B_ID, B_NAME, COLOUR)
B_ID Must Be Between 100 And 300, And Colour Must Be Red, Green, Yellow
And Blue.
RESERVED (S_ID, B_ID, DAY)
Create table through appropriate SQL command. Define all integrity constraints and
enter sufficient data through user friendly from design. Write SQL command and
output.
1. Find the average age of the sailor (who are at least 25 years old) for
each rating level that has at least two such sailors.
2. Find the S_ID’s of all sailors who have reserved RED boats but not
GREEN boats.
3. Finds the names of sailors who have reserved boat 203.
4. Find the name of the sailors who have reserved a red boat.
5. Find the sailors whose rating is better than some sailor named
ADITI.
6. Find the name of sailors who have reserved both RED and GREEN
boat.
7. Find the name of sailors who are older than the oldest sailor with a
rating of 8.
8. Find the name of sailors who have reserved all boats.
TABLE CREATION:
SQL> CREATE TABLE SAILORS(S_ID NUMBER(5)CHECK(S_ID BETWEEN 100 AND
10000)PRIMARY KEY,S_NAME VARCHAR2(30),RATING NUMBER(5),AGE
NUMBER(5));
Table created.
BACKEND CONNECTION:
VB CODE:
15 rows selected.
SQL> SSELECT * FROM BOATS;
12 rows selected.
9 rows selected.
QUERIES:
(1) Find the average age of the sailor (who are at least 25 years old) for
each rating level that has at least two such sailors.
AVG(AGE)
---------------
36.1666667
(2) Find the S_ID’s of all sailors who have reserved RED boats but not
GREEN boats.
NAME
---------------
B. Patro J. Das
(3) Finds the names of sailors who have reserved boat 203.
S_NAME
--------------
T. Sikdar A. Jana D.
Das
(4) Find the name of the sailors who have reserved a red boat.
S_NAME
--------------
A. Paul A. Datta B.
Patro J. Das A. Datta
D. Das D. Das D. Das
8 rows selected.
(5) Find the sailors whose rating is better than some sailor named ADITI.
S_NAME
---------------
T. Sikdar A. Paul M.
Deb D. Das M. Pandey
A. Jana M. Majhi A.
Datta B. Patro
9 rows selected.
(6) Find the name of sailors who have reserved both RED and GREEN
boat.
NAME
-------------
A. Paul D. Das D. Das
D. Das A. Datta A.
Datta
(7) Find the name of sailors who are older than the oldest sailor with a
rating of 8.
SQL> SELECT S_NAME FROM SAILORS WHERE AGE>(SELECT
MAX(AGE)FROM SAILORS WHERE RATING=8);
S_NAME
------------
A. Jana A. Pandit J.
Das B. Patro
S. Mahapatro
(8) Find the name of sailors who have reserved all boats.
S_NAME
--------------
D. Das
________________
Teacher Signature