05 1.queries Practice Set.1-Solution1
05 1.queries Practice Set.1-Solution1
XIT Database
1. List (ID, Name) of Students from Prog ID = ‘BCS’ and are having cpi > 7.5
2. List (ID, Name, cpi) of Students from Prog ID = ‘BCS’ or Prog ID = ‘BIT’ and
are having cpi > 7.5
Company Database
1. List all employee of DNO=4
𝜎𝑑𝑛𝑜=4 (𝑒𝑚𝑝𝑙𝑜𝑦𝑒𝑒)
SELECT * FROM employee WHERE dno=4;
7. List (ENO, Name, Salary) having 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);
XIT Database
1. List (prog-name, dept-name) of the program offered at XIT
⋈
𝑟1 ← 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
𝜋𝑠.∗ (𝜎𝑑𝑛𝑎𝑚𝑒=′𝐶𝑆′ (𝑟1))
select s.* from student as s join program as p on progid=pid
natural join department
where dname='CS';
⋈
𝜋𝑛𝑎𝑚𝑒,𝑝𝑛𝑎𝑚𝑒 (𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚)
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
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;
⋈
𝑟1 ← 𝑠𝑡𝑢𝑑𝑒𝑛𝑡 𝑝𝑟𝑜𝑔𝑟𝑎𝑚 ∗ 𝑑𝑒𝑝𝑎𝑟𝑡𝑚𝑒𝑛𝑡
𝑝𝑟𝑜𝑔𝑖𝑑=𝑝𝑖𝑑
𝜋𝑑𝑛𝑎𝑚𝑒 (𝜎𝑠𝑖𝑑=′123′ (𝑟1))
select dname from student as s join program as p on progid=pid
natural join department
where studid='123';
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;
Company Database
1. List Eno of non-managers, i.e., they are not manager of any department
2. List Eno, Ename, and Salary of non-managers, i.e., they are not managers of any
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
5. Compute employees (eno, ename, salary) that are supervisors (supervises some
employees) but not managers (that are manager of any department)
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;