Saturday, June 30, 2018

Usuage of In and Exists - Taken from Oracle Document (Interview Question)

Example 1: Using IN - Selective Filters in the Subquery
This example demonstrates how rewriting a query to use IN can improve performance. This query identifies all employees who have placed orders on behalf of customer 144.

The following SQL statement uses EXISTS:
SELECT /* EXISTS example */
         e.employee_id, e.first_name, e.last_name, e.salary
  FROM employees e
 WHERE EXISTS (SELECT 1 FROM orders o                  /* Note 1 */
                  WHERE e.employee_id = o.sales_rep_id   /* Note 2 */
                    AND o.customer_id = 144);            /* Note 3 */


Note 1: This shows the line containing EXISTS.
Note 2: This shows the line that makes the subquery a correlated subquery.
Note 3: This shows the line where the correlated subqueries include the highly selective predicate customer_id = number.

Execution Plan:


Rewriting the statement using IN results in significantly fewer resources used.

SELECT /* IN example */
         e.employee_id, e.first_name, e.last_name, e.salary
    FROM employees e
   WHERE e.employee_id IN (SELECT o.sales_rep_id         /* Note 4 */
                             FROM orders o
                            WHERE o.customer_id = 144);  /* Note 3 */


Note 3: This shows the line where the correlated subqueries include the highly selective predicate customer_id = number
Note 4: This indicates that an IN is being used. The subquery is no longer correlated, because the IN clause replaces the join in the subquery.

Execution Plan:

Example 2: Using EXISTS - Selective Predicate in the Parent

This example demonstrates how rewriting a query to use EXISTS can improve performance. This query identifies all employees from department 80 who are sales reps who have placed orders.
The following SQL statement uses IN:

SELECT /* IN example */
         e.employee_id, e.first_name, e.last_name, e.department_id, e.salary
    FROM employees e
   WHERE e.department_id = 80                                    /* Note 5 */
     AND e.job_id        = 'SA_REP'                              /* Note 6 */
     AND e.employee_id IN (SELECT o.sales_rep_id FROM orders o); /* Note 4 */


Note 4: This indicates that an IN is being used. The subquery is no longer correlated, because the IN clause replaces the join in the subquery.
Note 5 and 6: These are the selective predicates in the parent SQL.

Execution Plan:

Rewriting the statement using Exists results in significantly fewer resources used.

SELECT /* EXISTS example */
         e.employee_id, e.first_name, e.last_name, e.salary
    FROM employees e
   WHERE e.department_id = 80                           /* Note 5 */
     AND e.job_id        = 'SA_REP'                     /* Note 6 */
     AND EXISTS (SELECT 1                               /* Note 1 */
                   FROM orders o
                  WHERE e.employee_id = o.sales_rep_id);  /* Note 2 */

Note 1: This shows the line containing EXISTS.
Note 2: This shows the line that makes the subquery a correlated subquery.
Note 5 & 6:These are the selective predicates in the parent SQL.

Execution Plan:

Data Engineer - Written Exam Questions


Complex SQL Written Questions for Data Engineer

1.    We need to populate male and female alternatively
For Example:




Above records need to Populate as below:



Ans:



2.    Find all the students whose marks greater than the average marks in each subject within a class
For Example:



Expected Output:

Output:

3.    Find the data given below, and display the data with cumulative values

CREATE TABLE MONTHLYVOLUME
  (YEAR NUMBER(4),
  MONTH CHAR(3),
  SALES NUMBER(3));
/

INSERT INTO MONTHLYVOLUME VALUES (2007,'JAN',11);
INSERT INTO MONTHLYVOLUME VALUES (2007,'FEB',9);
INSERT INTO MONTHLYVOLUME VALUES (2007,'MAR',7);
INSERT INTO MONTHLYVOLUME VALUES (2007,'APR',5);
INSERT INTO MONTHLYVOLUME VALUES (2007,'MAY',14);
INSERT INTO MONTHLYVOLUME VALUES (2007,'JUN',6);
INSERT INTO MONTHLYVOLUME VALUES (2007,'JUL',8);
INSERT INTO MONTHLYVOLUME VALUES (2007,'AUG',17);
INSERT INTO MONTHLYVOLUME VALUES (2007,'SEP',18);
INSERT INTO MONTHLYVOLUME VALUES (2007,'OCT',19);
INSERT INTO MONTHLYVOLUME VALUES (2007,'NOV',12);
INSERT INTO MONTHLYVOLUME VALUES (2007,'DEC',1);
INSERT INTO MONTHLYVOLUME VALUES (2008,'JAN',4);
INSERT INTO MONTHLYVOLUME VALUES (2008,'FEB',7);
INSERT INTO MONTHLYVOLUME VALUES (2008,'MAR',8);
INSERT INTO MONTHLYVOLUME VALUES (2008,'APR',21);
INSERT INTO MONTHLYVOLUME VALUES (2008,'MAY',23);
INSERT INTO MONTHLYVOLUME VALUES (2008,'JUN',24);
INSERT INTO MONTHLYVOLUME VALUES (2008,'JUL',17);
INSERT INTO MONTHLYVOLUME VALUES (2008,'AUG',18);
INSERT INTO MONTHLYVOLUME VALUES (2008,'SEP',15);
INSERT INTO MONTHLYVOLUME VALUES (2008,'OCT',13);
INSERT INTO MONTHLYVOLUME VALUES (2008,'NOV',15);
INSERT INTO MONTHLYVOLUME VALUES (2008,'DEC',16);
INSERT INTO MONTHLYVOLUME VALUES (2009,'JAN',4);
INSERT INTO MONTHLYVOLUME VALUES (2009,'FEB',5);
INSERT INTO MONTHLYVOLUME VALUES (2009,'MAR',8);
INSERT INTO MONTHLYVOLUME VALUES (2009,'APR',1);
INSERT INTO MONTHLYVOLUME VALUES (2009,'MAY',3);
INSERT INTO MONTHLYVOLUME VALUES (2009,'JUN',2);
INSERT INTO MONTHLYVOLUME VALUES (2009,'JUL',7);
INSERT INTO MONTHLYVOLUME VALUES (2009,'AUG',8);
INSERT INTO MONTHLYVOLUME VALUES (2009,'SEP',5);
INSERT INTO MONTHLYVOLUME VALUES (2009,'OCT',3);
INSERT INTO MONTHLYVOLUME VALUES (2009,'NOV',1);
INSERT INTO MONTHLYVOLUME VALUES (2009,'DEC',6);
INSERT INTO MONTHLYVOLUME VALUES (2010,'JAN',4);
INSERT INTO MONTHLYVOLUME VALUES (2010,'FEB',17);
INSERT INTO MONTHLYVOLUME VALUES (2010,'MAR',8);
INSERT INTO MONTHLYVOLUME VALUES (2010,'APR',2);
INSERT INTO MONTHLYVOLUME VALUES (2010,'MAY',2);
INSERT INTO MONTHLYVOLUME VALUES (2010,'JUN',4);
INSERT INTO MONTHLYVOLUME VALUES (2010,'JUL',7);
INSERT INTO MONTHLYVOLUME VALUES (2010,'AUG',8);
INSERT INTO MONTHLYVOLUME VALUES (2010,'SEP',25);
INSERT INTO MONTHLYVOLUME VALUES (2010,'OCT',13);
INSERT INTO MONTHLYVOLUME VALUES (2010,'NOV',19);
INSERT INTO MONTHLYVOLUME VALUES (2010,'DEC',18);

Expected Output:

Actual Output:


1.    Find all the students who enrol for all the courses in the particular year
For Example:

Expected Output:

Actual Output:

2.    Find out the top 3 highest sold products
For Example:


Expected Output:

Actual Output:

3.    Based on the call logs find out the customer whose first call and last call is same person within a day
For Example:

Expected Output:

Actual Output:

4.    Find out all the sales representative who involves in sales of subsequent day as well
For Example:


Expected Output:

Actual Output: