October 28, 2018

Srikaanth

Oracle SQL Queries Most Frequently Asked In Facebook Written Test Interview

How to retrieve 2nd highest sal in each departement from emp and dept tables using GROUP BY?

SELECT e.DeptNo, MAX(e.Sal),d.DeptName Salary

FROM Emp e left outer join dept d ON e.DeptNo=d.DeptNo

WHERE e.Sal <

(SELECT MAX(Sal)

 FROM Emp

 WHERE DeptNo = e.DeptNo)

GROUP BY e.DeptNo,d.DeptName

Find the 3rd MAX and MIN salary in the emp table?

select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2 where e1.sal <= e2.sal);

select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2where e1.sal >= e2.sal);

If there are two tables emp1 and emp2, and both have common record. How can I fetch all the records but common records only once?

(Select * from emp) Union (Select * from emp1)

How to fetch only common records from two tables emp and emp1?

(Select * from emp) Intersect (Select * from emp1)

How can I retrive all records of emp1 those should not present in emp2?

(Select * from emp) Minus (Select * from emp1)

Get the first day of the month?

Quickly returns the first day of current month. Instead of current month you want to find first day of month where a date falls, replace SYSDATE with any date column/value.

SELECT TRUNC (SYSDATE, 'MONTH') "First day of current month"
    FROM DUAL;
Oracle SQL Queries Most Frequently Asked In Facebook Written Test Interview
Oracle SQL Queries Most Frequently Asked In Facebook Written Test Interview

Get the last day of the month?

This query is similar to above but returns last day of current month. One thing worth noting is that it automatically takes care of leap year. So if you have 29 days in Feb, it will return 29/2. Also similar to above query replace SYSDATE with any other date column/value to find last day of that particular month.

SELECT TRUNC (LAST_DAY (SYSDATE)) "Last day of current month"
    FROM DUAL;

Get the first day of the Year

First day of year is always 1-Jan. This query can be use in stored procedure where you quickly want first day of year for some calculation.

SELECT TRUNC (SYSDATE, 'YEAR') "Year First Day" FROM DUAL;

Get the last day of the year

Similar to above query. Instead of first day this query returns last day of current year.

SELECT ADD_MONTHS (TRUNC (SYSDATE, 'YEAR'), 12) - 1 "Year Last Day" FROM DUAL

Get number of days in current month

Now this is useful. This query returns number of days in current month. You can change SYSDATE with any date/value to know number of days in that month.

SELECT CAST (TO_CHAR (LAST_DAY (SYSDATE), 'dd') AS INT) number_of_days
  FROM DUAL;

Write a query to find the total sales of each product.?

Solution:

This is a simple query. You just need to group by the data on PRODUCT_NAME and then find the sum of sales.

SELECT P.PRODUCT_NAME,
       NVL( SUM( S.QUANTITY*S.PRICE ), 0) TOTAL_SALES
FROM   PRODUCTS P
       LEFT OUTER JOIN
       SALES S
ON     (P.PRODUCT_ID = S.PRODUCT_ID)
GROUP BY P.PRODUCT_NAME;

Write a query to find the products whose quantity sold in a year should be greater than the average quantity of the product sold across all the years?

Solution:

This can be solved with the help of correlated query. The SQL query for this is

SELECT P.PRODUCT_NAME,
       S.YEAR,
       S.QUANTITY
FROM   PRODUCTS P,
       SALES S
WHERE  P.PRODUCT_ID = S.PRODUCT_ID
AND    S.QUANTITY >
       (SELECT AVG(QUANTITY)
       FROM SALES S1
       WHERE S1.PRODUCT_ID = S.PRODUCT_ID
       );

Write a query to compare the products sales of "IPhone" and "Samsung" in each year? The output should look like as

YEAR IPHONE_QUANT SAM_QUANT IPHONE_PRICE SAM_PRICE
---------------------------------------------------
2010   10           20       9000         7000
2011   15           18       9000         7000
2012   20           20       9000         7000

Solution:

By using self-join SQL query we can get the required result. The required SQL query is

SELECT S_I.YEAR,
       S_I.QUANTITY IPHONE_QUANT,
       S_S.QUANTITY SAM_QUANT,
       S_I.PRICE    IPHONE_PRICE,
       S_S.PRICE    SAM_PRICE
FROM   PRODUCTS P_I,
       SALES S_I,
       PRODUCTS P_S,
       SALES S_S
WHERE  P_I.PRODUCT_ID = S_I.PRODUCT_ID
AND    P_S.PRODUCT_ID = S_S.PRODUCT_ID
AND    P_I.PRODUCT_NAME = 'IPhone'
AND    P_S.PRODUCT_NAME = 'Samsung'
AND    S_I.YEAR = S_S.YEAR

Write a query to find the ratios of the sales of a product?

Solution:

The ratio of a product is calculated as the total sales price in a particular year divide by the total sales price across all years. Oracle provides RATIO_TO_REPORT analytical function for finding the ratios. The SQL query is

SELECT P.PRODUCT_NAME,
       S.YEAR,
       RATIO_TO_REPORT(S.QUANTITY*S.PRICE)
         OVER(PARTITION BY P.PRODUCT_NAME ) SALES_RATIO
FROM   PRODUCTS P,
       SALES S
WHERE (P.PRODUCT_ID = S.PRODUCT_ID);

In the SALES table quantity of each product is stored in rows for every year. Now write a query to transpose the quantity for each product and display it in columns? The output should look like as

PRODUCT_NAME QUAN_2010 QUAN_2011 QUAN_2012
------------------------------------------
IPhone       10        15        20
Samsung      20        18        20
Nokia        25        16        8

Solution:

Oracle 11g provides a pivot function to transpose the row data into column data. The SQL query for this is

SELECT * FROM
(
SELECT P.PRODUCT_NAME,
       S.QUANTITY,
       S.YEAR
FROM   PRODUCTS P,
       SALES S
WHERE (P.PRODUCT_ID = S.PRODUCT_ID)
)A
PIVOT ( MAX(QUANTITY) AS QUAN FOR (YEAR) IN (2010,2011,2012));

If you are not running oracle 11g database, then use the below query for transposing the row data into column data.

SELECT P.PRODUCT_NAME,
       MAX(DECODE(S.YEAR,2010, S.QUANTITY)) QUAN_2010,
       MAX(DECODE(S.YEAR,2011, S.QUANTITY)) QUAN_2011,
       MAX(DECODE(S.YEAR,2012, S.QUANTITY)) QUAN_2012
FROM   PRODUCTS P,
       SALES S
WHERE (P.PRODUCT_ID = S.PRODUCT_ID)
GROUP BY P.PRODUCT_NAME;


Subscribe to get more Posts :