DBMS New Assignment

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Assignment on subqueries

1. Problem Statement: Display the employees whose job-id is same as that of employee
102.

Query:
SELECT *
FROM employee
WHERE job_id =
( SELECT job_id
FROM employee
WHERE employee_id=102
)

Q2. Problem Statement: Display the employees whose job_id is same as that of employee 102
& whose salary is greater than that of employee 144.
Query:
SELECT * FROM EMPLOYEE
WHERE job_id = (select job_id from employee where employee_id =102)
and salary > (SELECT salary
FROM employee WHERE employee_id=144)

Q3. Problem Statement: Display the information of all employees whose salary is equal to
minimum salary
Query:
Select * from Employee where salary = (select min(salary) from employee)

Q4. Problem Statement: Display all the department minimum salary that have minimum salary
greater than that of department 20.
Query:

Select e.department_id, e.salary as min_salary from employee e where e.salary =


(Select min(salary) from employee e2 where e2.department_id = e.department_id) and e.salary
> (select min(salary) from employee where department_ = 20

Q5. Problem Statement: Find the job with the lowest average salary

Query:
select t.job_id, t.avg_salary from(
select job_id, avg(salary) from employee group by job_id) t
Where t.avg_salary = (select min(avg_salary) from (select avg(sal) as avg_salary from
employee group by job_id) t2)

Q6.Problem Statement: Display the employee number & ename of all employees who work in a
department with any employee whose last name contain letter u.
Query:
select EMPLOYEE_ID, first_name, last_name from employee where DEPARTMENT_ID in
(select DEPARTMENT_ID from employee where last_name like '%u%')
Q7.Problem Statement: Display the last name & salary of every employee who reports to king.
Query:
select last_name, salary from employee where last_name = ‘king’

Q8.Problem Statement: Display the name of employees who is getting the maximum salary
Query:
select first_name, last_name from employee
where salary = (select max(salary) from employee)

Q9.Problem Statement: Display the employee number, last _name & salaries of all employees
who work in a department with any employee whose last name contain letter u & who earn more
than the average salary
Query:
select employee_id, last_name, salary from employee
where department_id =
(select department_id from employee where last_name like 'U%')
and department_id in
(select department_id from employee where salary > (select avg(salary) from employee))

Q10.Problem Statement: Display the employees who work in the same department as that of
steven
Query:
select * from employee where department_id in
(select department_id from employee where first_name = 'Steven')

Q11.Problem Statement: Find the employee who earn least.


Query:
select * from employee where salary = (select min(salary) from employee)
Q12.Problem Statement: Show the department no ,department name & the number of
employees working in each department : a) Includes employees less than 3 b) Has the highest
number of employees c) Has the lowest number of employees
Query:
select dept_no, dept_name count(employee_id) from employee having count(employee) < 3

b)Query:
Select department_id, job_id, count(*) as num_employee from employee group by
department_id, job_id order by num_employee desc
c)Query:
Select department_id, job_id, count(*) as num_employee from employee group by
department_id, job_id order by num_employee asc
Q13.Problem Statement: Write a query to list the employees who earn more than their
managers.
Query:
select * from employee where salary >
(select max(salary) from employee where manager_id is not null)

Q14.Problem Statement: List the department number, department name & the number of
employees.
Query:
Select department_id, job_id, count(*) as num_employee from employee group by
department_id, job_id

Assignment on alter

Q1. Problem Statement: Create the emp_new table


Query:
create table emp_new(ID Number(7),
First_name Varchar2(25),
Last_Name Varchar2(25),
Dept_id Number(7))

Q2. Problem Statement: Create the dept table

Query:
create table dept(Dept_id Number(10),
Dname Varchar2(20));

Q3. Problem Statement: Modify the emp_new table to allow the longer employee last_name by
increasing the width of the column by 50.
Query:
alter table emp_new modify (last_name Varchar2(75));

Q4. Problem Statement: Drop the first name column from emp_new table. Confirm your
modification by checking the description of the table.
Query:
alter table emp_new drop column first_name;
Query:
describe emp_new;

Q5. Problem Statement: Rename the dept-id column as deptno.


Query:
alter table dept rename column dept_id to deptno;

Q6. Problem Statement: Rename the table emp_new as employee.


Query:
alter table emp_new rename to employee;

Q7. Problem Statement: Create a primary key constraint to the dept table on dept_id column.
The constraint should be named at creation. Name the constraint my_dept_id_pk.
Query:
ALTER TABLE dept
ADD CONSTRAINT my_dept_id_pk PRIMARY KEY (deptno);

Q8. Problem Statement: Add a foreign key reference on the emp table that ensures the
employee is not assigned to a nonexistent department. Name the constraint
my_emp_dept_id_fk
Query:
ALTER TABLE emp
ADD CONSTRAINT my_emp_dept_id_fk
FOREIGN KEY (dept_id) REFERENCES dept(id);

Q9. Problem Statement: Confirm that the constraints were added by querying the
USER_CONSTRAINTS VIEW.
Query:
SELECT * FROM USER_CONSTRAINTS
ASSIGNMENT ON JOINS
Q1. Problem Statement: Display the employees-id,employee name,dept_id & department name
for all the employees.
Query:
select employee.id, employee.first_name , dept.department_id, dept.name from
employee,department join dept on emp.id = dept.id;

Q2. Problem Statement: Display the employee smith department number & department name.
Query:
select dept_id, dept_name, from dept join department on employee_id = dept_id where
employee.name = 'smith';

Q3*****. Problem Statement: Display the employee last name, department name & city for each
employee whose salary is greater than 2000
Query:
selct employee.id, dept.name, from employee, dept join dept on employee.id = dept.id where
employee.sal>2000;

Q4***. Problem Statement: Display the employee last name & department name for all
employees who have ‘a’ in their last name.
Query: select employee.last_name, dept.name from employee, dept
where employee.last_name like 'a%';

Q5. Problem Statement: Display the employee last name & employee number along with their
manager last name & manager number. The output should be in the following sequence
(employee_lastname,emp_id,manager_lastname,manager_id).
Query:
select employee.lastname,employee.id,manager.lastna
me,manager.id from employee, manager join manager on employee.id = manager.id;

ASSIGNMENT ON GROUP FUNCTIONS


Q1. Problem Statement: List minimum , maximum , average salaries of employee.
Query:
select min(salary), max(salary), avg(salary) from employee

Q2. Problem Statement: Find how many job titles are available in employee table.
Query:
select count(distinct(job_id)) from employee

Q3.Problem Statement: What is the difference between maximum and minimum salaries of
employees in the organization.
Query:
SELECT MAX(salary) - MIN(salary) DIFFERENCE
FROM employees

Q4. Problem Statement: Find how much amount the company is spending towards salaries.
Query:
select sum(salary) from emp

Q5. Problem Statement: Calculate the average salary of all employees.


Query:
select avg(salary) from employee

Q6. Problem Statement: Count the number of records in emp table.


Query:
select count(*) from employee
Q7. Problem Statement: To calculate the number of employees in each dept.
Query:
select department_id, count(*) from employee group by department_id

Q8. Problem Statement: Calculate the maximum, minimum salary for each department
Query:
select department_id, max(salary), min(salary) from employee group by department_id

Q9. Problem Statement: Display the job where the number of employees is less than 3
Query:
select job_id from employee group by job_id having count(Job_id) < 3

Q10. Problem Statement: Display the department number & average salaries for those
department whose maximum salary greater than 10,000.
Query:
select department_id, avg(salary) from employee
where salary > 10000 group by department_id

Q11. Problem Statement: Display the job_id & total monthly salary for each job with a total
payroll exceeding 13000 & sort the result by total monthly salary & jobid does not contain MAN.
Query:
select job_id, salary from employee
where salary > 13000 and not job_id like '%MAN%'

You might also like