SQL Assignment 6
1. Create a report that displays the employee number, last name, and salary of all
employees who earn more than the average salary. Sort the results in ascending order
by salary.
Select employee_id “employee number”,last_name,salary from employees where
salary>(select avg(salary) from employees) order by salary;
2. What is Sub-query and the sub-query can be placed in which SQL clauses?
Sub-query is a query inside a query. Parenthesis is used to express the subqueries.
The order of execution is first the inner query placed inside the parenthesis and then
the outer query. It is nothing but the nested query. The result of inner query is placed
inside the outer query.
The sub-query can be placed in three clauses. Those are HAVING,WHERE and
FROM clause.
3. Write a query that displays the employee number and last name of all employees
who work in a department with any employee whose last name contains a “u”.
Select employee_id “employee number”,last_name from employees where
department_id IN(select department_id from employees where last_name like(‘%u
%’)).;
4. Differentiate between single-row Subqueries and multi-row Subqueries.
Single row queries return only one row where as multi-row returns more than one row.
5. The HR department needs a report that displays the last name, department number,
and job ID of all employees whose department location ID is 1700.
Select last_name, department_id “department number”, job_id from employees
where department_id=(select department_id from departments where
location_id=1700);
6. Create a report for the HR department that displays the last name and salary of every
employee who reports to King.
Select last_name,salary from employees where manager_id in(select employee_id
from employees where last_name=’King’);
7. The outer query and inner query can get data from different tables (TRUE/FALSE).
True.
8. Write down few examples of single-row comparison operators and multi-row
comparison operators.
Single-row operators are >,<,<=,>=,<>,= and multi-row operators are IN,ANY and
ALL.
Ex-Display the employees whose job ID is the same as that of employee 100;
Select job_id from employees where job_id=(select job_id from employees where
employee_id=100); //single-row query
Ex.-display last_name of employees whose salary is equal to “King”.
Select last_name from employees where salary in(select salary from employees
where last_name=’King’); //multi-row query.
9. A sub-query must be enclosed in parentheses (TRUE/FALSE).
True
10. Create a report for the HR department that displays the department number, last
name, and job ID for every employee in the Executive department.
11. Group functions can not be used in a sub-query (TRUE/FALSE).
True.
12. What is the meaning of <ANY,>ANY and =ANY?
<ANY-It means less than maximum.
> ANY-It means more than minimum.
= ANY-It is equivalent to IN operator.
13. Write a Query to display the employee number, last name, and salary of all the
employees who earn more than the average salary and who work in a department
with any employee whose last name contains a “u”.
Select employee_id “employee number”,last_name,salary from employees where
salary >(select avg(salary) from employees) and last_name like(‘%u%’) ;