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

05 1.queries Practice Set.1-Solution1

The document outlines various SQL queries for two databases: XIT and Company. It includes selection, projection, and join operations to retrieve specific information about students and employees based on criteria like program ID, CPI, department, salary, and supervisor relationships. Each query is presented in both relational algebra notation and SQL syntax.

Uploaded by

dev.m.dodiya2004
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)
11 views6 pages

05 1.queries Practice Set.1-Solution1

The document outlines various SQL queries for two databases: XIT and Company. It includes selection, projection, and join operations to retrieve specific information about students and employees based on criteria like program ID, CPI, department, salary, and supervisor relationships. Each query is presented in both relational algebra notation and SQL syntax.

Uploaded by

dev.m.dodiya2004
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

Queries based on Selection and Project operations:

XIT Database

1. List (ID, Name) of Students from Prog ID = ‘BCS’ and are having cpi > 7.5

𝜋𝑠𝑡𝑢𝑑𝑖𝑑, 𝑛𝑎𝑚𝑒 (𝜎𝑝𝑟𝑜𝑔𝑖𝑑=′𝐵𝐶𝑆 ′𝐴𝑁𝐷 𝑐𝑝𝑖>7.5 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡))


SELECT studid, name from student
WHERE progid='BCS' AND cpi > 7.5;

2. List (ID, Name, cpi) of Students from Prog ID = ‘BCS’ or Prog ID = ‘BIT’ and
are having cpi > 7.5

𝜋𝑠𝑡𝑢𝑑𝑖𝑑, 𝑛𝑎𝑚𝑒, 𝑐𝑝𝑖 (𝜎(𝑝𝑟𝑜𝑔𝑖𝑑=′𝐵𝐶𝑆 ′𝑂𝑅 𝑝𝑟𝑜𝑔𝑖𝑑=′𝐵𝐼𝑇 ′) 𝐴𝑁𝐷 𝑐𝑝𝑖>7.5 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡))


SELECT studid, name, cpi from student
WHERE (progid='BCS' or progid='BIT') AND cpi > 7.5;

Company Database
1. List all employee of DNO=4

𝜎𝑑𝑛𝑜=4 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)
SELECT * FROM employee WHERE dno=4;

2. List employees having salary > 30000

𝜎𝑠𝑎𝑙𝑎𝑟𝑦 > 30000 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)


SELECT * FROM employee WHERE salary > 30000;

3. List (ENO, Name, Salary) for all employees

𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒, 𝑠𝑎𝑙𝑎𝑟𝑦 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)


SELECT eno, ename, salary FROM employee;
4. List (ENO, Name, Salary) for all employees of DNO=4

𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒, 𝑠𝑎𝑙𝑎𝑟𝑦 (𝜎𝑑𝑛𝑜=4 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒))


SELECT eno, ename, salary FROM employee WHERE dno=4;

5. List (ENO, Name, Salary) of all Female Employees

𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒, 𝑠𝑎𝑙𝑎𝑟𝑦 (𝜎𝑔𝑒𝑛𝑑𝑒𝑟=′𝐹′ (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒))


SELECT eno, ename, salary FROM employee WHERE gender='F';

6. List eno, name, salary of employees who are supervised by eno=105

𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒, 𝑠𝑎𝑙𝑎𝑟𝑦 (𝜎𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜=105 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒))


SELECT eno, ename, salary FROM employee WHERE super_eno=105;

7. List (ENO, Name, Salary) having salary >= 10000 and <= 30000

𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒, 𝑠𝑎𝑙𝑎𝑟𝑦 (𝜎𝑠𝑎𝑙𝑎𝑟𝑦 >= 10000 𝑎𝑛𝑑 <= 30000 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒))


SELECT eno, ename, salary FROM employee
WHERE salary >= 10000 and <= 30000;

8. List employees either working dno=4 and salary > 25000 or working dno=5 and
salary > 30000.

𝜎(𝑑𝑛𝑜=4 𝑎𝑛𝑑 𝑠𝑎𝑙𝑎𝑟𝑦 > 25000) 𝑜𝑟 (𝑑𝑛𝑜=5 𝑎𝑛𝑑 𝑠𝑎𝑙𝑎𝑟𝑦 > 30000) (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)
SELECT eno, ename, salary FROM employee
WHERE (dno=4 and salary > 25000) or (dno=5 and salary > 30000);

9. List employees who are older than 50 years

𝜎(𝑦𝑒𝑎𝑟(𝑐𝑢𝑟𝑟𝑒𝑛𝑡𝑑𝑎𝑡𝑒−𝑑𝑜𝑏)≥ 50) (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)


select * FROM employee where extract(year from age(dob)) >= 50;
Queries based on JOIN operations:

XIT Database
1. List (prog-name, dept-name) of the program offered at XIT

𝜋𝑝𝑛𝑎𝑚𝑒,𝑑𝑛𝑎𝑚𝑒 (𝑝𝑟𝑜𝑔𝑟𝑎𝑚 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡)


select pname, dname from program
natural join department;

2. List students who are studying in CS department


𝑟1 ← 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
𝜋𝑠.∗ (𝜎𝑑𝑛𝑎𝑚𝑒=′𝐶𝑆′ (𝑟1))
select s.* from student as s join program as p on progid=pid
natural join department
where dname='CS';

3. List (stud-name, prog-name) of all students


𝜋𝑛𝑎𝑚𝑒,𝑝𝑛𝑎𝑚𝑒 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚)
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
select name, pname from student join program on progid=pid;

4. List student name, program name of students having cpi > 7.5


𝜋𝑛𝑎𝑚𝑒,𝑝𝑛𝑎𝑚𝑒 (𝜎𝑐𝑝𝑖>7.5 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚))
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
select name, pname from student join program on progid=pid
where cpi > 7.5;

5. What name of the department in which student id = 123 is studying?


𝑟1 ← 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
𝜋𝑑𝑛𝑎𝑚𝑒 (𝜎𝑠𝑖𝑑=′123′ (𝑟1))
select dname from student as s join program as p on progid=pid
natural join department
where studid='123';

6. List ID and Name of students of BTech(CS) and BTech(ECE)

𝜋𝑠𝑖𝑑,𝑛𝑎𝑚𝑒 (𝜎𝑝𝑟𝑜𝑔𝑖𝑑=′𝐵𝐶𝑆 ′𝑂𝑅 𝑝𝑟𝑜𝑔𝑖𝑑=′𝐵𝐶𝐸′ (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 ))


select studid, name from student
where progid='BCS' or progid='BCE';
Company Database
1. List ENO, Name, DName of all employees

𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒,𝑑𝑛𝑎𝑚𝑒 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡)


select eno,ename,dname from employee
natural join department where dno = 4;

2. List ename, salary of all Female Supervisors



𝜋𝑠.𝑒𝑛𝑎𝑚𝑒,𝑠.𝑠𝑎𝑙𝑎𝑟𝑦 (𝜎𝑠.𝑔𝑒𝑛𝑑𝑒𝑟=′𝐹′ (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑠))
𝑒.𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜=𝑠.𝑒𝑛𝑜
select distinct s.ename, s.salary from employee as e
join employee as s on e.super_eno=s.eno
where s.gender='F';

3. List ename, salary of all Female Managers



𝜋𝑒𝑛𝑎𝑚𝑒,𝑠𝑎𝑙𝑎𝑟𝑦 (𝜎𝑔𝑒𝑛𝑑𝑒𝑟=′𝐹′ (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡))
𝑚𝑔𝑟_𝑒𝑛𝑜=𝑒𝑛𝑜
select ename, salary from employee
join department on mgr_eno=eno
where gender='F';

4. List employees having salary > their manager (manager of their department)

𝑟1 ← 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑚
𝑚𝑔𝑟_𝑒𝑛𝑜 =𝑚.𝑒𝑛𝑜
𝜋𝑒.𝑒𝑛𝑜,𝑒.𝑒𝑛𝑎𝑚𝑒 (𝜎𝑒.𝑠𝑎𝑙𝑎𝑟𝑦>𝑚.𝑠𝑎𝑙𝑎𝑟𝑦 (𝑟1))
select e.eno, e.ename from employee e natural join department
join employee as m on mgr_eno=m.eno
where e.salary > m.salary;
5. List Employee that are working on projects monitored by DNO=4

𝑟1 ← 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 ∗ 𝑤𝑜𝑟𝑘𝑠_𝑜𝑛 𝑤 𝑝𝑟𝑜𝑗𝑒𝑐𝑡 𝑝
𝑤.𝑝𝑛𝑜 =𝑝.𝑝𝑛𝑜
𝜋𝑒.𝑒𝑛𝑜,𝑒.𝑒𝑛𝑎𝑚𝑒 (𝜎𝑝.𝑑𝑛𝑜=4 (𝑟1))
select e.eno, e.ename from employee e natural join works_on w
join project p on w.pno=p.pno
where p.dno=4;

6. List Employee Name, Supervisor Name (if any)


𝐿𝐸𝐹𝑇 ⋈
𝜋𝑒.𝑒𝑛𝑎𝑚𝑒,𝑠.𝑒𝑛𝑎𝑚𝑒 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑠)
𝑒.𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜=𝑠.𝑒𝑛𝑜
select e.ename, s.ename from employee as e
left join employee as s on e.super_eno=s.eno;
7. List employees (eno, name) that work on projects managed by department
'Research'.

𝑟1 ← 𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 ∗ 𝑤𝑜𝑟𝑘𝑠𝑜𝑛 𝑤 𝑝𝑟𝑜𝑗𝑒𝑐𝑡 𝑝
𝑤.𝑝𝑛𝑜 =𝑝.𝑝𝑛𝑜

𝜋𝑒.𝑒𝑛𝑜,𝑒.𝑒𝑛𝑎𝑚𝑒 (𝜎𝑑𝑛𝑎𝑚𝑒=′𝑅𝑒𝑠𝑒𝑎𝑟𝑐ℎ′ (𝑟1 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 𝑑))
𝑝.𝑑𝑛𝑜 =𝑑.𝑑𝑛𝑜
select e.eno, e.ename from employee e natural join works_on w
join project p on w.pno=p.pno join department d on p.dno=d.dno
where dname='Research';

Queries based on SET operations:

Company Database
1. List Eno of non-managers, i.e., they are not manager of any department

𝜋𝑒𝑛𝑜 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒) − 𝜋𝑚𝑔𝑟_𝑒𝑛𝑜 (𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡)


select eno from employee
except
select mgr_eno from department;

2. List Eno, Ename, and Salary of non-managers, i.e., they are not managers of any
department

𝑟1 ← 𝜋𝑒𝑛𝑜 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒) − 𝜋𝑚𝑔𝑟_𝑒𝑛𝑜 (𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡)


𝑟𝑒𝑠𝑢𝑙𝑡 ← 𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒,𝑠𝑎𝑙𝑎𝑟𝑦 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 ∗ 𝑟1)
select eno, ename, salary from employee natural join
(select eno from employee
except
select mgr_eno from department) as r;
OR
𝑠𝑒𝑚𝑖−𝑑𝑖𝑓𝑓
𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒,𝑠𝑎𝑙𝑎𝑟𝑦 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡 𝑑))
𝑒𝑛𝑜=𝑚𝑔𝑟_𝑒𝑛𝑜
select eno, ename, salary from employee where eno
not in (select mgr_eno from department);

3. Find out Employees (eno, ename, salary) that are directly (works for) for dno=4 or
indirectly associated with (work on projects managed by) dno=4

𝑟1 ← 𝜋𝑒𝑛𝑜 (𝜎𝑑𝑛𝑜=4 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)) ∪ 𝜋𝑒𝑛𝑜 (𝜎𝑑𝑛𝑜=4 𝑤𝑜𝑟𝑘𝑠_𝑜𝑛 ∗ 𝑝𝑟𝑜𝑗𝑒𝑐𝑡)


𝑟𝑒𝑠𝑢𝑙𝑡 ← 𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒,𝑠𝑎𝑙𝑎𝑟𝑦 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 ∗ 𝑟1)
select eno, ename, salary from employee natural join
(select eno from employee where dno=4
union
select eno from works_on natural join project where dno=4) as r;
4. List ENO, ENAME of employees who work on at least one project.
𝑠𝑒𝑚𝑖−𝑖𝑛𝑡𝑒𝑟𝑠𝑒𝑐𝑡𝑖𝑜𝑛
𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 𝑤𝑜𝑟𝑘𝑠_𝑜𝑛 𝑤))
𝑒.𝑒𝑛𝑜=𝑤.𝑒𝑛𝑜
select eno, ename from employee where eno in
(select eno from works_on);

5. Compute employees (eno, ename, salary) that are supervisors (supervises some
employees) but not managers (that are manager of any department)

𝑟1 ← 𝜋𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒) − 𝜋𝑚𝑔𝑟_𝑒𝑛𝑜 (𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡)



𝑟𝑒𝑠𝑢𝑙𝑡 ← 𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒,𝑠𝑎𝑙𝑎𝑟𝑦 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 𝑟1)
𝑒.𝑒𝑛𝑜=𝑟1.𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜
select eno, ename from employee e join
(select distinct super_eno from employee
except
select mgr_eno from department
) as r
on r.super_eno=e.eno;

6. Compute employees (eno, ename, salary) that are supervisors or Managers but not
both

𝑟1 ← 𝜋𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)
𝑟2 ← 𝜋𝑚𝑔𝑟_𝑒𝑛𝑜 (𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡)
𝑟3 ← (𝑟 ∪ 𝑟2) − (𝑟 ∩ 𝑟2)

𝑟𝑒𝑠𝑢𝑙𝑡 ← 𝜋𝑒𝑛𝑜,𝑒𝑛𝑎𝑚𝑒,𝑠𝑎𝑙𝑎𝑟𝑦 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒 𝑒 𝑟3)
𝑒.𝑒𝑛𝑜=𝑟3.𝑠𝑢𝑝𝑒𝑟_𝑒𝑛𝑜
select eno, ename, salary from employee e join
((select distinct super_eno from employee
union
select mgr_eno from department)
except
(select distinct super_eno from employee
intersect
select mgr_eno from department
)) as r
on r.super_eno=e.eno;

You might also like