0% found this document useful (0 votes)
17 views6 pages

Mod 4 Queries

The document outlines the schema and SQL queries for a Company Database, including tables for employees, departments, projects, and work assignments. It provides various SQL commands to retrieve, update, and manipulate data, such as listing project numbers for employees named 'Scott', calculating salary raises, and summarizing employee salaries in specific departments. Additionally, it includes instructions for creating tables, inserting values, and handling referential integrity constraints.

Uploaded by

Bindushree Bade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Mod 4 Queries

The document outlines the schema and SQL queries for a Company Database, including tables for employees, departments, projects, and work assignments. It provides various SQL commands to retrieve, update, and manipulate data, such as listing project numbers for employees named 'Scott', calculating salary raises, and summarizing employee salaries in specific departments. Additionally, it includes instructions for creating tables, inserting values, and handling referential integrity constraints.

Uploaded by

Bindushree Bade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

2.

Consider the schema for Company Database:


EMPLOYEE (SSN, Name, Address, Sex, Salary, SuperSSN, DNo) DEPARTMENT (DNo,
DName, MgrSSN, MgrStartDate) DLOCATION (DNo,DLoc)
PROJECT (PNo, PName, PLocation, DNo) WORKS_ON (SSN, PNo, Hours)
Write SQL queries to
1. Make a list of all project numbers for projects that involve an employee whose
last name is ‘Scott’, either as a worker or as a manager of the department that
controls the project.
2. Show the resulting salaries if every employee working on the ‘IoT’ project is
given a 10 percent raise.
3. Find the sum of the salaries of all employees of the ‘Accounts’ department, as
well as the maximum salary, the minimum salary, and the average salary in this
department
4. Retrieve the name of each employee who works on all the projects controlled by
department number 5 (use NOT EXISTS operator). For each department that has
more than five employees, retrieve the department number and the number of its
employees who are making more than Rs. 6,00,000.

Table Creation;

create table department (dno varchar(10) primary key, dname varchar(20), mgrstartdate
date);

note: we must alter department table to add foreign key mgrssn;


alter table department add mgrssn references employee(ssn)

create table employee (ssn varchar(10) primary key, fname varchar(10),lname varchar(10),
addr varchar(10), sex char (1),salary decimal(10,2)superssn varchar(10), dno varchar(10)
references department (dno));

alter table department add mgrssn varchar(10) references employee(ssn);

create table dlocation (dloc varchar(20),dno varchar(10) references department (dno));

create table project (pno integer primary key, pname varchar(20),ploc varchar(20), dno
varchar(10) references department (dno));

create table workson (noh integer (2),


ssn varchar(10) references employee (ssn), pno integer references project(pno));

Insertion of values to tables ;


insert into employee values (4000,’kumar’,’bangalore’,’male’,80000,’null’,101);
insert into employee values (4010,’scott’,’chennai’,’male’,75000,’null’,102);

insert into department values (101,’marketing’,’10-jan-01’,4000);


insert into department values (102,’accounts’,’21-mar-2007’,4010);

insert into delocation values(101,’banagalore’);


insert into delocation values (102,’bangalore’);
insert into project values (900,’iot’,’bangalore’101);
insert into project values (901,’datascience’,’chennai’101);
insert into project values (902,’machinelearning’,’bangalore’102);

insert into workson values (4000,900,10); insert into workson values (4010,902,5); insert
into workson values (4001,900,0); insert into workson values (4002,900,9); insert into
workson values (4011,902,8); insert into workson values (4012,902,5);

Note: update entries of employee table to fill missing fields SUPERSSN and DNO
Update employee set dno=101 where ssn=4000;
Update employee set dno=102 where ssn=4010;

Queries:
1) Display all the details of all employees working in the company.
select * from employee;

2) Display ssn, lname, fname, address of employees who work indepartment no 7.


select ssn,lname,fname,address from employee where dno=7;

3) Retrieve the birthdate and address of the employee whose name is'Franklin T.Wong'
select bdate,address from employee where fname="Franklin" and mname="T" and
lname="Wong";

4) Retrieve the name and salary of every employee


select fname,mname,lname,salary from employee;

5) Retrieve all distinct salary values


select distinct salary from employee;

6) Retrieve all employee names whose address is in ‘Bellaire’


select fname,mname,lname from employee where address="Bellaire";

7) Retrieve all employees who were born during the 1950s


select fname from employee where bdate between #01-01-50# and #31-12-
59#;

8) Retrieve all employees in department 5 whose salary is between


50,000 and 60,000(inclusive)
select * from employee where dno=5 and salary >=50000 and salary
<=60000;

9) Retrieve the names of all employees who do not have supervisors


select fname,mname,lname from employee where superssn is null;

10) Retrieve SSN and department name for all employees


select e.ssn, d.dname from employee e, department d;

11)Retrieve the name and address of all employees who work for Research department
12) For every project located in 'Stafford', list the project
number,the controlling department number, and the department
manager's lastname, address, and birthdate
select p.pnumber,p.dnum,e.lname,e.address,e.bdate
from project p, department d, employee e
where p.plocation="Stafford" and p.dnum= d.dnumber and d.mgrssn=e.ssn;

13) : For each employee, retrieve the employee's name, and the name of
his or her immediate supervisor
select e.fname,e.lname,s.fname,s.lname
from employee as e, employee as s
where s.superssn=e.ssn;

14) Retrieve all combinations of Employee Name and Department Name


select e.fname,e.lname,d.dname
from employee e, department d;

15) Make a list of all project numbers for projects that involve an
employee whose last name is 'Narayan’ either as a worker or as a
manager of the department that controls the project

(select distinct pnumber


from project,department,employee
where dnum=dnumber and mgrssn=ssn and lname="Narayan")

union

(select distinct pnumber


from project,works_on,employee
where pnumber=pno and essn=ssn and lname="Narayan");

16) : Increase the salary of all employees working on the 'ProductX'


project by 15% .
select fname,lname.1.1*salary as increased_sal
from employee,works_on,project
where ssn=essn and pno=pnumber and pname="productX";
**updating in DB

17) Retrieve a list of employees and the project name each works in,
ordered by the employee's department, and within each department
ordered alphabetically by employee first name
select dname,lname,fname,pname
from department,employee,works_on,project
where dnumber=dno and ssn=essn and pno=pnumber
order by dname,lname,fname;

18) Select the names of employees whose salary does not match with
salary of any employee in department 10
select fname

from employee
where salary > all(select salary from employee where dno=5);

19) Retrieve the name of each employee who has a dependent with the
same first name and same sex as the employee
select e.fname,e.lname
from employee as e
where e.ssn in (select essn from dependent where
e.fname=dependent_name and e.sex=sex);

20) Retrieve the employee numbers of all employees who work on project
located in Bellaire, Houston, or Stafford
select ssn
from employee
where ((select pno
from works_on
where ssn=essn) contains
(select pnumber
from project
where dnum=5));

21) Find the sum of the salaries of all employees, the maximum
salary, the minimum salary, and the average salary. Display with
proper headings
select sum(salary),max(salary),min(salary),avg(salary)
from employee;

22) Find the sum of the salaries and number of employees of all
employees of the ‘Marketing’ department, as well as the maximum
salary, the minimum salary, and the average salary in this department
select sum(salary),count(*)
from employee, department
where dname like "market%";

23) Select the names of employees whose salary is greater than the
average salary of all employees in department 10

select fname

from employee

where dno=10

group by salary
having salary>avg(salary);

24) For each department, retrieve the department number, the number of
employees in the department, and their average salary
select dno,count(*),avg(salary)
from employeegroup by dno;

25) For each project, retrieve the project number, the project name,
and the number of employees who work on that project
select pnumber,pname,count(*)
from project
group by pnumber;

26) Change the location and controlling department number for all
projects having more than 5 employees to ‘Bellaire’ and 6 respectively
update project
set plocation="Bellaire", dnum=6
where (select count(essn)
from works_on
where pno=pnumber)>5;

27) : For each department having more than 10 employees, retrieve the
department no, no of employees drawing more than 40,000 as salary
select dno
from employee
where salary>40000
group by dno
having count(*)>10;

28) Insert a record in Project table which violates refrential


integrity constraint with respect to Department number. Now remove the
violation by making necessary insertion in the Department table.
insert into project
values("Research and development",25,"Bhopal",9);
/* The above query will give an error since there exists no
department with department number 9 exixts in the department table */
/* To remove this error, we create a record in table department
with dnumber as 9 */
insert into department
values("Research",9,"123","20-08-2012");

29) Delete all dependents of employee whose ssn is ‘123456789’


delete from dependent
where essn=123456789;

30) Delete an employee from Employee table with ssn = ‘12345’( make
sure that this employee has some dependents, is working on some
project, is a manager of some department and is supervising some
employees). Check and display the cascading effect on Dependent and
Works on table. In Department table MGRSSN should be set to default
value and in Employee table SUPERSSN should be set to NULL
delete from employee
where ssn=1234567891 cascade****;

31) . Perform a query using alter command to drop/add field and a


constraint in Employee table.
alter table
drop foreign key(superssn);

You might also like