Search This Blog/Web

Wednesday, November 27, 2013

ORACLE SUBQUERY/CORRELATED QUERY EXAMPLES

A subquery is a SELECT statement which is used in another SELECT statement. Subqueries are very useful when you need to select rows from a table with a condition that depends on the data of the table itself. You can use the subquery in the SQL clauses including WHERE clause, HAVING clause, FROM clause etc.

The subquery can also be referred as nested SELECT, sub SELECT or inner SELECT. In general, the subquery executes first and its output is used in the main query or outer query.

Types of Sub queries:

There are two types of subqueries in oracle:

  • Single Row Subqueries: The subquery returns only one row. Use single row comparison operators like =, > etc while doing comparisions.
  • Multiple Row Subqueries: The subquery returns more than one row. Use multiple row comparison operators like IN, ANY, ALL in the comparisons.

SINGLE ROW SUBQUERY EXAMPLES


1. Write a query to find the salary of employees whose salary is greater than the salary of employee whose id is 100?

SELECT EMPLOYEE_ID,
 SALARY
FROM EMPLOYEES
WHERE SALARY >
    (
  SELECT SALARY
  FROM EMPLOYEES
  WHERE EMPLOYEED_ID = 100
  )

2. Write a query to find the employees who all are earning the highest salary?

SELECT EMPLOYEE_ID,
 SALARY
FROM EMPLOYEES
WHERE SALARY =
  (
  SELECT  MAX(SALARY)
  FROM EMPLOYEES
  )

3. Write a query to find the departments in which the least salary is greater than the highest salary in the department of id 200?

SELECT DEPARTMENT_ID,
 MIN(SALARY)
FROM EMPLOYEES
GROUP BY DEPARTMENT_ID
HAVING MIN(SALARY) > 
  (
  SELECT MAX(SALARY)
  FROM EMPLOYEES
  WHERE DEPARTMENT_ID = 200
  )

MULTIPLE ROW SUBQUERY EXAMPLES


1. Write a query to find the employees whose salary is equal to the salary of at least one employee in department of id 300?

SELECT EMPLOYEE_ID,
 SALARY
FROM EMPLOYEES
WHERE SALARY IN
  (
  SELECT  SALARY
  FROM EMPLOYEES
  WHERE DEPARTMENT_ID = 300
  )

2. Write a query to find the employees whose salary is greater than at least on employee in department of id 500?

SELECT EMPLOYEE_ID,
 SALARY
FROM EMPLOYEES
WHERE SALARY > ANY
  (
  SELECT  SALARY
  FROM EMPLOYEES
  WHERE DEPARTMENT_ID = 500
  )

3. Write a query to find the employees whose salary is less than the salary of all employees in department of id 100?

SELECT EMPLOYEE_ID,
 SALARY
FROM EMPLOYEES
WHERE SALARY < ALL
  (
  SELECT  SALARY
  FROM EMPLOYEES
  WHERE DEPARTMENT_ID = 100
  )

4. Write a query to find the employees whose manager and department should match with the employee of id 20 or 30? 

SELECT EMPLOYEE_ID,
 MANAGER_ID,
 DEPARTMENT_ID
FROM EMPLOYEES
WHERE (MANAGER_ID,DEPARTMENT_ID) IN
  (
  SELECT MANAGER_ID,
   DEPARTMENT_ID
  FROM EMPLOYEES
  WHERE EMPLOYEE_ID IN (20,30)
  )

5. Write a query to get the department name of an employee?

SELECT EMPLOYEE_ID,
 DEPARTMENT_ID,
 (SELECT DEPARTMENT_NAME
 FROM DEPARTMENTS D
 WHERE D.DEPARTMENT_ID = E.DEPARTMENT_ID
 )
FROM EMPLOYEES E

CORRELATED SUBQUERIES EXAMPLES


Correlated sub query is used for row by row processing. The sub query is executed for each row of the main query.

1. Write a query to find the highest earning employee in each department? 

SELECT DEPARTMENT_ID,
 EMPLOYEE_ID,
 SALARY
FROM EMPLOYEES E_0
WHERE 1 = 
  (
  SELECT  COUNT(DISTINCT SALARY)
  FROM EMPLOYEES E_I
  WHERE E_O.DEPARTMENT_ID = E_I.DEPARTMENT_ID
  AND E_O.SALARY <=  E_I.SALARY
  )

2. Write a query to list the department names which have at lease one employee? 

SELECT DEPARTMENT_ID,
 DEPARTMENT_NAME
FROM DEPARTMENTS D
WHERE EXISTS
 (
 SELECT 1
 FROM EMPLOYEES E
 WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID)

3. Write a query to find the departments which do not have employees at all? 

SELECT DEPARTMENT_ID,
 DEPARTMENT_NAME
FROM DEPARTMENTS D
WHERE NOT EXISTS
 (
 SELECT  1
 FROM EMPLOYEES E
 WHERE E.DEPARTMENT_ID = D.DEPARTMENT_ID)

Difference between Subquery, Nested Subquery and Correlated Subquery

Query: - Query can be defined as a way to inquire the data from the database. It is used to extract the data from one table or multiple tables depending upon the user needs.

Subquery:-If a sql statement contains another sql statement then the sql statement which is inside another sql statement is called Subquery. It is also known as nested query. The Sql Statement which contains the other sql statement is called Parent Statement.

Nested Subquery:-If a Subquery contains another subquery, then the subquery inside another subquery is called nested subquery.

Correlated Subquery:-If the outcome of a subquery is depends on the value of a column of its parent query table then the Sub query is called Correlated Subquery.

or

A correlated subquery is a subquery that uses values from the outer query, requiring the inner query to execute once for each outer query 

The Oracle database wants to execute the subquery once and use the results for all the evaluations in the outer query. With a correlated subquery, the database must run the subquery for each evaluation because it is based on the outer query’s data.
select
   book_key,
   store_key,
   quantity
from
   sales s
where
  quantity <
   (select max(quantity)
       from sales
       where book_key = s.book_key); 
BOOK_K STOR QUANTITY
------ ---- ----------
B101 S101 1000
B102 S102 10
B102 S103 200
…
B116 S105 100
B101 S105 8000
B109 S109 100
81 rows selected.
In the example above, the subquery references the book_key in the outer query. The value of the book_key changes by row of the outer query, so the database must rerun the subquery for each row comparison. This has a significant performance impact on the execution time of the query, and for that reason, correlated subqueries should be avoided if possible.
The outer query knows nothing about the inner query except its results. For that reason, the outer query cannot reference any columns in the subquery. However, the subquery has access to the outer query and can reference outer query columns, thus the correlated subquery.