Search This Blog

Thursday, August 30, 2012

SQL Queries Interview Questions - Oracle Part 1

SQL Queries Interview Questions

CREATE TABLE PRODUCTS

(

PRODUCT_ID INTEGER,

PRODUCT_NAME VARCHAR2(30)

);

CREATE TABLE SALES

(

SALE_ID INTEGER,

PRODUCT_ID INTEGER,

YEAR INTEGER,

Quantity INTEGER,

PRICE INTEGER

);

INSERT INTO PRODUCTS VALUES ( 100, 'Nokia');

INSERT INTO PRODUCTS VALUES ( 200, 'IPhone');

INSERT INTO PRODUCTS VALUES ( 300, 'Samsung');

INSERT INTO PRODUCTS VALUES ( 400, 'LG');

INSERT INTO SALES VALUES ( 1, 100, 2010, 25, 5000);

INSERT INTO SALES VALUES ( 2, 100, 2011, 16, 5000);

INSERT INTO SALES VALUES ( 3, 100, 2012, 8, 5000);

INSERT INTO SALES VALUES ( 4, 200, 2010, 10, 9000);

INSERT INTO SALES VALUES ( 5, 200, 2011, 15, 9000);

INSERT INTO SALES VALUES ( 6, 200, 2012, 20, 9000);

INSERT INTO SALES VALUES ( 7, 300, 2010, 20, 7000);

INSERT INTO SALES VALUES ( 8, 300, 2011, 18, 7000);

INSERT INTO SALES VALUES ( 9, 300, 2012, 20, 7000);

COMMIT;


The products table contains the below data.

SELECT * FROM PRODUCTS;

PRODUCT_ID PRODUCT_NAME

-----------------------

100 Nokia

200 IPhone

300 Samsung


The sales table contains the following data.

SELECT * FROM SALES;

SALE_ID PRODUCT_ID YEAR QUANTITY PRICE

--------------------------------------

1 100 2010 25 5000

2 100 2011 16 5000

3 100 2012 8 5000

4 200 2010 10 9000

5 200 2011 15 9000

6 200 2012 20 9000

7 300 2010 20 7000

8 300 2011 18 7000

9 300 2012 20 7000


Here Quantity is the number of products sold in each year. Price is the sale price of each product.

I hope you have created the tables in your oracle database. Now try to solve the below SQL queries.

1. Write a SQL query to find the products which have continuous increase in sales every year?

Solution:

Here “Iphone” is the only product whose sales are increasing every year.

STEP1: First we will get the previous year sales for each product. The SQL query to do this is

SELECT P.PRODUCT_NAME,

S.YEAR,

S.QUANTITY,

LEAD(S.QUANTITY,1,0) OVER (

PARTITION BY P.PRODUCT_ID

ORDER BY S.YEAR DESC

) QUAN_PREV_YEAR

FROM PRODUCTS P,

SALES S

WHERE P.PRODUCT_ID = S.PRODUCT_ID;

PRODUCT_NAME YEAR QUANTITY QUAN_PREV_YEAR

-----------------------------------------

Nokia 2012 8 16

Nokia 2011 16 25

Nokia 2010 25 0

IPhone 2012 20 15

IPhone 2011 15 10

IPhone 2010 10 0

Samsung 2012 20 18

Samsung 2011 18 20

Samsung 2010 20 0


Here the lead analytic function will get the quantity of a product in its previous year.

STEP2: We will find the difference between the quantities of a product with its previous year’s quantity. If this difference is greater than or equal to zero for all the rows, then the product is a constantly increasing in sales. The final query to get the required result is

SELECT PRODUCT_NAME

FROM

(

SELECT P.PRODUCT_NAME,

S.QUANTITY -

LEAD(S.QUANTITY,1,0) OVER (

PARTITION BY P.PRODUCT_ID

ORDER BY S.YEAR DESC

) QUAN_DIFF

FROM PRODUCTS P,

SALES S

WHERE P.PRODUCT_ID = S.PRODUCT_ID

)A

GROUP BY PRODUCT_NAME

HAVING MIN(QUAN_DIFF) >= 0;

PRODUCT_NAME

------------

IPhone



2. Write a SQL query to find the products which does not have sales at all?

Solution:

“LG” is the only product which does not have sales at all. This can be achieved in three ways.

Method1:

Using the NOT EXISTS operator.

SELECT P.PRODUCT_NAME

FROM PRODUCTS P

WHERE NOT EXISTS

(SELECT 1 FROM SALES S WHERE S.PRODUCT_ID = P.PRODUCT_ID);

PRODUCT_NAME

------------

LG
Method2: Using the NOT IN operator.

SELECT P.PRODUCT_NAME

FROM PRODUCTS P

WHERE P.PRODUCT_ID NOT IN

(SELECT DISTINCT PRODUCT_ID FROM SALES);

PRODUCT_NAME

------------

LG


Method3:

Using left outer join.

SELECT P.PRODUCT_NAME

FROM PRODUCTS P

LEFT OUTER JOIN

SALES S

ON (P.PRODUCT_ID = S.PRODUCT_ID);

WHERE S.QUANTITY IS NULL

PRODUCT_NAME

------------

LG



3. Write a query to select the top product sold in each year?

Solution:

Nokia is the top product sold in the year 2010. Similarly, Samsung in 2011 and IPhone, Samsung in 2012. The query for this is

SELECT PRODUCT_NAME,

YEAR

FROM

(

SELECT P.PRODUCT_NAME,

S.YEAR,

RANK() OVER (

PARTITION BY S.YEAR

ORDER BY S.QUANTITY DESC

) RNK

FROM PRODUCTS P,

SALES S

WHERE P.PRODUCT_ID = S.PRODUCT_ID

) A

WHERE RNK = 1;

PRODUCT_NAME YEAR

--------------------

Nokia 2010

Samsung 2011

IPhone 2012

Samsung 2012


4. Write a SQL query to find the products whose sales decreased in 2012 compared to 2011?

Solution:

Here Nokia is the only product whose sales decreased in year 2012 when compared with the sales in the year 2011. The SQL query to get the required output is

SELECT P.PRODUCT_NAME

FROM PRODUCTS P,

SALES S_2012,

SALES S_2011

WHERE P.PRODUCT_ID = S_2012.PRODUCT_ID

AND S_2012.YEAR = 2012

AND S_2011.YEAR = 2011

AND S_2012.PRODUCT_ID = S_2011.PRODUCT_ID

AND S_2012.QUANTITY < S_2011.QUANTITY;

PRODUCT_NAME

------------

Nokia


5. 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;

PRODUCT_NAME TOTAL_SALES

---------------------------

LG 0

IPhone 405000

Samsung 406000

Nokia 245000

6. 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

);

PRODUCT_NAME YEAR QUANTITY

--------------------------

Nokia 2010 25

IPhone 2012 20

Samsung 2012 20

Samsung 2010 20


7. 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


8. 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);

PRODUCT_NAME YEAR RATIO

-----------------------------

IPhone 2011 0.333333333

IPhone 2012 0.444444444

IPhone 2010 0.222222222

Nokia 2012 0.163265306

Nokia 2011 0.326530612

Nokia 2010 0.510204082

Samsung 2010 0.344827586

Samsung 2012 0.344827586

Samsung 2011 0.310344828

No comments:

Post a Comment