The document describes a table called Employee with columns for employee ID, name, date of birth, department, designation, date of joining, and salary. It provides sample data to insert into the table. It then lists 10 questions to answer by writing SQL queries on the Employee table. The questions include displaying all records, finding employees by department or date range, filtering by salary, and aggregating data.
1. Question:
[A] Table Creation
Create a table according to the schema given below. Choose the appropriate data types while
creating the table. Insert the records given in Table 1 into the Employee table. And, write SQL
queries to satisfy the questions given below.
Employee (Emp_ID, Emp_Name, DoB, Department, Designation, DoJ, Salary)
Here, DoB means Date of Birth, DoJ means Date of Joining.
Emp_ID Emp_Name DoB Department Designation DoJ Salary
F110 Sam 15-JUN-
1970
Bio-
Technology
Professor 12-APR-
2001
45000
F111 Kumar 25-MAY-
1980
Mechanical Asst. Prof. 02-MAY-
2006
30000
F115 Raguvaran 10-AUG-
1982
CSE Asst. Prof. 05-MAY-
2007
27000
F114 Jennifer 10-SEP-
1975
CSE Asst. Prof. 03-JUN-
2004
35000
F117 Ismail 15-MAY-
1979
IT Asst. Prof. 10-MAY-
2005
33000
Table 1 – Employee
[B] Queries
1. Display all the records from table Employee.
2. Find all the employees who are working for CSE department.
3. Get the details about the employees who have joined after ’10-JUN-2005’.
4. Find all the employees who earn more than 30000.
5. Get the details of employees who are not ‘Professor’.
6. Find the name, date of birth, and designation of all the employees who work for ‘IT’ department.
7. Find all the departments which are offering salary above 25000.
8. Get the DoB of employee named ‘Kumar’.
9. Find the names and departments of employees who earn the salary in the range 20000 to 40000.
10. Find the employee details of any employee who work for ‘CSE’ and earn more than 30000.
2. Let us assume a table User_Personal as given below;
UserID U_email Fname Lname City State Zip
MA12 Mani@ymail.com MANISH JAIN BILASPUR CHATISGARH 458991
PO45 Pooja.g@gmail.co POOJA MAGG KACCH GUJRAT 832212
LA33 Lavle98@jj.com LAVLEEN DHALLA RAIPUR CHATISGARH 853578
CH99 Cheki9j@ih.com CHIMAL BEDI TRICHY TAMIL NADU 632011
DA74 Danu58@g.com DANY JAMES TRICHY TAMIL NADU 645018
A. Is this table in First Normal Form?
Yes. All the attributes contain only atomic values.
Is this table in Second Normal Form?
To verify this property, we need to find all the functional dependencies which are holding
in User_Personal table, and have to identify a Primary key.
Let us do that by using the sample data. This leads to the following set of FDs;
F = { UserID → U_email Fname Lname City State Zip,
Zip → City State }
As UserID attribute can uniquely determine all the other attributes, we can have UserID
as the Primary key for User_Personal table.
The next step is to check for the 2NF properties;
Property 1 – The table should be in 1NF.
Property 2 – There should not be any partial key dependencies.
Our table is in 1NF, hence property 1 is holding.
Primary key of our table is UserID and UserID is single simple attribute. As the key is
not composite, there is no chance for partial key dependency to hold. Hence property
2 is also holding.
User_Personal table is in 2NF.
3. Is User_Personal in 3NF?
To verify this we need to check the 3NF properties;
Property 1 – Table should be in 2NF.
Property 2 – There should not be any Transitive Dependencies in the table.
Table User_Personal is in 2NF, hence property 1 is satisfied.
User_Personal table holds the following Transitive dependency;
UserID → Zip, Zip → City State
Hence, property 2 is not satisfied and the table is not in 3NF.
Solution:
Decompose User_Personal. For this, we can use the functional dependencies Zip → City
State and UserID → U_email Fname Lname City State Zip.
As a result, we can have the following tables (primary keys are underlined);
User_Personal (UserID, U_email, Fname, Lname, Zip)
City (Zip, City, State)
UserID U_email Fname Lname Zip
MA12 Mani@ymail.com MANISH JAIN 458991
PO45 Pooja.g@gmail.co POOJA MAGG 832212
LA33 Lavle98@jj.com LAVLEEN DHALLA 853578
CH99 Cheki9j@ih.com CHIMAL BEDI 632011
DA74 Danu58@g.com DANY JAMES 645018
Table - User_Personal
Zip City State
458991 BILASPUR CHATISGARH
832212 KACCH GUJRAT
853578 RAIPUR CHATISGARH
632011 TRICHY TAMIL NADU
645018 TRICHY TAMIL NADU
Table – City
Both tables are in 3NF.
Hence, tables are normalized to Third Normal Form.
4. S Q L P r a c t i c e Q u e s t i o n s - S o l u t i o n
1.For the following relation schema:
employee(employee-name, street, city)
works(employee-name, company-name, salary)
company(company-name, city)
manages(employee-name, manager-name)
Give an expression in SQL for each of the following queries:
B. Find the names, street address, and cities of residence for all employees who work
for 'First Bank Corporation' and earn more than $10,000.
select employee.employee-name, employee.street, employee.city fromemployee,
workswhere employee.employee-name=works.employee-name and company-
name = 'First Bank Corporation' and salary > 10000)
b)
Find the names of all employees in the database who live in the same cities as thecompaniesfor
whichtheywork.
select e.employee-namefrom employee e, works w, company cwhere e.employee-name =
w.employee-name and e.city = c.city and w.company-name = c.company-name
c)
Find the names of all employees in the database who live in the same cities and on thesame
streets as do their managers.
select p.employee-namefrom employee p, employee r, manages mwhere p.employee-name =
m.employee-name and m.manager-name =r.employee-
name and p.street = r.street and p.city = r.city
d)
Find the names of all employees in the database who do not work for 'First
Bank Corporation'. Assume that all people work for exactly one company.
select employee-namefrom workswhere company-name <> 'First Bank Corporation'
e)
Find the names of all employees in the database who earn more than every employeeof 'Small
Bank Corporation'. Assume that all people work for at most one company.
select employee-namefrom workswhere salary > all (select salary from works where company-
name = 'Small Bank Corporation')
f)
Assume that the companies may be located in several cities. Find all companieslocated in every
city in which 'Small Bank Corporation' is located.
5. select s.company-namefrom company swhere not exists((select city from company where
company-name = 'Small BankCorporation')except(select city from company t where s.company-
name = t.company-name))
g)
Find the names of all employees who earn more than the average salary of allemployees of their
company. Assume that all people work for at most one company.
select employee-namefrom works twhere salary >(select avg(salary) from works
s where t.company-name = s.company-name)
h)
Find the name of the company that has the smallest payroll.
select company-namefrom worksgroup by company-namehaving sum(salary) <= all (select
sum(salary) from works group by company-name)