SQL Queries Interview Questions - Oracle Part 2
SQL Queries Interview Questions - Oracle Part 2
SQL Queries Interview Questions - Oracle Part 2
Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop
Search... Search
SQL Queries Interview Questions - Oracle Part 2
This is continuation to my previous post, SQL Queries Interview Questions - Oracle Part 1 ,
Popular Posts
Where i have used PRODUCTS and SALES tables as an example. Here also i am using the Informatica Scenario Based Interview Questions with
same tables. So, just take a look at the tables by going through that link and it will be easy for Answers - Part 1
you to understand the questions mentioned here.
Unix Sed Command to Delete Lines in File - 15 Examples
Solve the below examples by writing SQL queries. String Functions in Hive
1. Write a query to find the products whose quantity sold in a year should be greater than the Top Examples of Awk Command in Unix
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 1/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
2. Write a query to compare the products sales of "IPhone" and "Samsung" in each year? The
output should look like as
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'
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 2/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
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);
4. 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
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 3/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
look like as
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
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 4/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
FROM PRODUCTS P,
SALES S
WHERE (P.PRODUCT_ID = S.PRODUCT_ID)
GROUP BY P.PRODUCT_NAME;
Solution:
To get this result we have to group by on year and the find the count. The SQL query for this
question is
SELECT YEAR,
COUNT(1) NUM_PRODUCTS
FROM SALES
GROUP BY YEAR;
YEAR NUM_PRODUCTS
------------------
2010 3
2011 3
2012 3
Recommended Posts:
If you like this post, then please share it on Google by clicking on the +1 button.
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 5/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
13 comments:
For 1st query we need to write query like given below then only we will get all values which are
greater than the avg value.
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 S
);
Correct me if am wrong.
Reply
Replies
In my requirement i want to find the products whose quantity is greater than the
average quantity of the product across all the years.
SELECT P.PRODUCT_NAME,P.PRODUCT_id,
S.YEAR,
S.QUANTITY
FROM PRODUCTS P,
SALES S
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 6/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
Reply
here based on the Name i need to calculate the sum(2+4+3) and i need to display all the rows as
it is along with sum as shown in below table. I want to make this in mysql.
OutPut:
Thanks in advance.
Reply
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 7/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
Replies
Regards,
Yuvakesh
Reply
I would like to delete 'last_data_updated' for table_id group from my etl_run table by using the
below query
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 8/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
But am facing an error like 'you can't specify target table for updat...'
Thanks in advance.
Regards,
Yuvakesh
Reply
Replies
Right now i dont have access to the db. I am providing rough sql queries here. Try
them and if they didn't work, let me know. Then i will provide the exact sql queries on
monday.
Thanks a lot.
Reply
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 9/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
Expected Result:
YEAR IPHONE_QUANT SAM_QUANT IPHONE_PRICE SAM_PRICE
---------------------------------------------------
2010 10 20 90000 140000
2011 15 18 135000 126000
2012 20 20 100000 140000
Result Got:
YEAR IPHONE_QUANT SAM_QUANT IPHONE_PRICE SAM_PRICE
---------------------------------------------------
2010 10 20 9000 7000
2011 15 18 9000 7000
2012 20 20 9000 7000
Reply
Name=Yuvakesh R. Yarrathi
Expected =Yarrathi, Yuvakesh R.
But sometimes am getting suffix in addition to the full name, in that case I need to split as given
below-
Name=Rajesh Kumar Mr
Expected=Mr Kumar, Rajesh
That means I don’t wont to consider suffixes (Mr, Mrs, JR, SR) as a last name.
Thanks in advance.
Regards,
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 10/11
11/29/2018 SQL Queries Interview Questions - Oracle Part 2
Yuvakesh
Reply
Publish Preview
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 11/11