DB Assignment 2
DB Assignment 2
Assignment 2
M. Jawad Shahid
21L-5787
BSSE-4A
Q1. You need to find out the names and IDs of the departments in
which the least salary is greater than the highest salary in the
department 10.
SQL:
select dept_name, departments.dept_id
from departments
join employees on departments.dept_id = employees.dept_id
having min(salary) > (
select max(salary)
from employees
where dept_id = 10
);
RA:
Π dept_name, departments.dept_id (σ min(salary) > (ρ max_salary (Π salary (σ
dept_id=10 (employees)))) (departments ⨝ employees))
Q2. Write a query to find the employees whose salary is equal to the
salary of at least one employee in department of id 10.
SQL:
select *
from employees
where salary in (
select salary
from employees
where dept_id = 10
)
and dept_id <> 10;
RA:
Π employees (σ salary ∈ (Π salary (σ dept_id = 10 (employees))) and dept_id ≠ 10
(employees))
Q3. You need to find out all the employees who have salary greater
than at least one employee in the department 10.
SQL:
select *
from employees
where salary > any (
select salary
from employees
where dept_id = 10
);
RA:
Π employees (σ salary > any (Π salary (σ dept_id = 10 (employees))) (employees))
Q4. You need to find out all the employees who have salary lesser
than the salary of all the employees in the department 10.
SQL:
select *
from employees
where salary < all (
select salary
from employees
where dept_id = 10
);
RA:
Π employees (σ salary < all (Π salary (σ dept_id = 10 (employees))) (employees))
Q5. You need to find out all the employees who have their manager
and department matching with the employee having an Employee
ID of 121 or 200.
SQL:
select *
from employees
where mgr = (
select mgr
from employees
where emp_id in (121, 200)
)
and dept_id = (
select dept_id
from employees
where emp_id in (121, 200)
);
RA:
Π employees (σ mgr = any (Π mgr (σ emp_id = 121 ∨ emp_id = 200
(employees)))and dept_id = any (Π dept_id (σ emp_id = 121 ∨ emp_id = 200
(employees))) (employees))
SQL:
select dept_name from departments
join employees on departments.dept_id = employees.dept_id
where emp_id = 200;
RA:
Π dept_name ((departments ⨝ σ emp_id = 200 (employees)))
Q7. You need to find the highest earning employee with the job ID
as ‘SA_REP’.
SQL:
SELECT *
FROM employees
WHERE job = 'SA_REP' AND salary = (
SELECT MAX(salary)
FROM employees
WHERE job = 'SA_REP'
);
RA:
σ(job = 'SA_REP' and salary = max(Π (salary)(σ(job = 'SA_REP')
(EMPLOYEES))))(EMPLOYEES)
Q8. You need to find the job which has at least one employee in it.
SQL:
select distinct job
from employees;
RA:
Π job_id (employees)
Q9. You need to find the job which has no employees in it.
SQL:
select distinct job
from employees
where job not in (
select distinct job
from employees
);
RA:
Π job(employees) – Π job(employees)
Q10. You need to find the 3rd maximum salary from the
EMPLOYEES table.
SQL:
select distinct salary
from employees e1
where 3 = (
select count(distinct salary)
from employees e2
where e2.salary >= e1.salary
)
order by salary desc;
RA:
Π salary (σ cnt = 3 (ρ cnt, salary (γ salary (e1) ⟕ρ cnt, salary (γ count(salary) (e2))
⋈ salary=e2.salary (e2))))