SQL Queries Interview Questions - Oracle Part 4
SQL Queries Interview Questions - Oracle Part 4
Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop
Search... Search
SQL Queries Interview Questions - Oracle Part 4
1. Consider the following friends table as the source
Popular Posts
sam, vamsi
Top Examples of Awk Command in Unix
vamsi, ram
vamsi, jhon Sed Command in Unix and Linux Examples
ram, vijay
ram, anand Design/Implement/Create SCD Type 2 Effective Date
Mapping in Informatica
Name, Friend_of_Firend
----------------------
Have Questions? Follow Me
sam, ram
sam, jhon
sam, vijay
sam, anand
https://www.folkstalk.com/2011/11/oracle-scenario-based-questions-part-2.html 1/6
11/29/2018 SQL Queries Interview Questions - Oracle Part 4
vijay bhaskar
Solution: Add to circles
SELECT f1.name,
f2.friend_name as friend_of_friend
FROM friends f1,
friends f2
WHERE f1.name = 'sam'
AND f1.friend_name = f2.name;
2. This is an extension to the problem 1. In the output, you can see ram is displayed as friends of
friends. This is because, ram is mutual friend of sam and vamsi. Now extend the above query to
exclude mutual friends. The outuput should look as
994 have me in circles View all
Name, Friend_of_Friend
----------------------
sam, jhon
sam, vijay
sam, anand
Solution:
SELECT f1.name,
f2.friend_name as friend_of_friend
FROM friends f1,
friends f2
WHERE f1.name = 'sam'
AND f1.friend_name = f2.name
AND NOT EXISTS
(SELECT 1 FROM friends f3
https://www.folkstalk.com/2011/11/oracle-scenario-based-questions-part-2.html 2/6
11/29/2018 SQL Queries Interview Questions - Oracle Part 4
3. Write a query to get the top 5 products based on the quantity sold without using the
row_number analytical function? The source data looks as
Solution:
SELECT products,
quantity_sold,
year
FROM
(
SELECT products,
quantity_sold,
year,
rownum r
from t
https://www.folkstalk.com/2011/11/oracle-scenario-based-questions-part-2.html 3/6
11/29/2018 SQL Queries Interview Questions - Oracle Part 4
4. This is an extension to the problem 3. Write a query to produce the same output using
row_number analytical function?
Solution:
SELECT products,
quantity_sold,
year
FROM
(
SELECT products,
quantity_sold,
year,
row_number() OVER(
ORDER BY quantity_sold DESC) r
from t
)A
WHERE r <= 5;
5. This is an extension to the problem 3. write a query to get the top 5 products in each year
based on the quantity sold?
Solution:
SELECT products,
quantity_sold,
year
FROM
(
https://www.folkstalk.com/2011/11/oracle-scenario-based-questions-part-2.html 4/6
11/29/2018 SQL Queries Interview Questions - Oracle Part 4
SELECT products,
quantity_sold,
year,
row_number() OVER(
PARTITION BY year
ORDER BY quantity_sold DESC) r
from t
)A
WHERE r <= 5;
Recommended Posts:
No comments:
Post a Comment
Publish Preview
https://www.folkstalk.com/2011/11/oracle-scenario-based-questions-part-2.html 5/6
11/29/2018 SQL Queries Interview Questions - Oracle Part 4
https://www.folkstalk.com/2011/11/oracle-scenario-based-questions-part-2.html 6/6