SQL Queries Interview Questions - Oracle Part 1
SQL Queries Interview Questions - Oracle Part 1
Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop
Search... Search
SQL Queries Interview Questions - Oracle Part 1
As a database developer, writing SQL queries, PLSQL code is part of daily life. Having a good
Popular Posts
knowledge on SQL is really important. Here i am posting some practical examples on SQL Informatica Scenario Based Interview Questions with
queries. Answers - Part 1
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 1/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1
INSERT INTO SALES VALUES ( 1, 100, 2010, 25, 5000); vijay bhaskar
INSERT INTO SALES VALUES ( 2, 100, 2011, 16, 5000); Add to circles
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;
PRODUCT_ID PRODUCT_NAME
-----------------------
100 Nokia
200 IPhone
300 Samsung
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;
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?
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 4/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1
Solution:
“LG” is the only product which does not have sales at all. This can be achieved in three ways.
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
SELECT P.PRODUCT_NAME
FROM PRODUCTS P
WHERE P.PRODUCT_ID NOT IN
(SELECT DISTINCT PRODUCT_ID FROM SALES);
PRODUCT_NAME
------------
LG
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 5/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1
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
3. 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
Solution:
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 6/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1
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
Solution:
This is a simple query. You just need to group by the data on PRODUCT_NAME and then find
the sum of sales.
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 7/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1
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
Recommended Posts:
If you like this post, then please share it on Google by clicking on the +1 button.
9 comments:
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 8/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1
@Neel
SELECT P.PRODUCT_NAME,
S.YEAR,
S.QUANTITY
FROM
(
SELECT YEAR,
MAX(QUANTITY) QUAN
FROM SALES
GROUP BY YEAR
)A, SALES S,
PRODUCTS P
WHERE A.YEAR = S.YEAR
AND A.QUAN = S.QUANTITY
AND S.PRODUCT_ID = P.PRODUCT_ID;
please suggest if we can achieve same using some other simple sql(no anlaytic) way.
Reply
Replies
Reply
The correlated query working only when there is distinct quantities in a year. I checked it for
picking only one product in a year and it failed in that case. This needs to be changed a bit. I will
try and post the query.
Reply
300 2011 18
100 2011 16
300 2012 20*
200 2012 20*
so, this is really fine (bcoz, i hv mentioned equal sign also in insider query).
Lets try with some other way, and post some intersting question just as you posted. or write me to
[email protected]. I want to test my oracle sql skills.
Thanks,
Neel.
Reply
Publish Preview
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 12/12