11/29/2018 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
average quantity of the product sold across all the years?
Sed Command in Unix and Linux Examples
Solution: Design/Implement/Create SCD Type 2 Effective Date
Mapping in Informatica
This can be solved with the help of correlated query. The SQL query for this is
Date Functions in Hive
SQL Queries Interview Questions - Oracle Part 1
SELECT P.PRODUCT_NAME,
Top Unix Interview Questions - Part 1
S.YEAR,
S.QUANTITY Update Strategy Transformation in Informatica
FROM PRODUCTS P,
SALES S
WHERE P.PRODUCT_ID = S.PRODUCT_ID Have Questions? Follow Me
AND S.QUANTITY >
(SELECT AVG(QUANTITY)
FROM SALES S1
WHERE S1.PRODUCT_ID = S.PRODUCT_ID
);
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
PRODUCT_NAME YEAR QUANTITY vijay bhaskar
-------------------------- Add to circles
Nokia 2010 25
IPhone 2012 20
Samsung 2012 20
Samsung 2010 20
2. 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 994 have me in circles View all
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'
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
AND P_S.PRODUCT_NAME = 'Samsung'
AND S_I.YEAR = S_S.YEAR
3. 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
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
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
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;
5. Write a query to find the number of products sold in each year?
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:
SQL Queries Interview Questions - Oracle Part 1
SQL Queries Interview Questions - Oracle Part 2
SQL Queries Interview Questions - Oracle Part 3
SQL Queries Interview Questions - Oracle Part 4
SQL Queries Interview Questions - Oracle Part 5
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:
Yuvakesh Y 04 July, 2012 21:43
Hi Vijay,
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
vijay bhaskar 04 July, 2012 22:00
This works when you want to find the avg across all the years (ignoring products).
In my requirement i want to find the products whose quantity is greater than the
average quantity of the product across all the years.
shaukat pathan 03 October, 2012 08:10
i think below query will not work as year 2011 is not included in the output.
this should have below query correct me if i am wrong.
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
WHERE P.PRODUCT_ID = S.PRODUCT_ID
AND S.QUANTITY >
(SELECT AVG(QUANTITY) FROM SALES S1 WHERE S1.YEAR = S.YEAR)
order by year
vijay bhaskar 03 October, 2012 08:30
Year 2011 is not qualified as the product sales is less than the average sales
Reply
Yuvakesh Y 01 August, 2012 04:43
Hi Vijay,
Below is the Input table.
City Name Count
A Yuvakesh 2
C Yuvakesh 4
D Yuvakesh 3
A Raj 1
C Raj 4
E Raj 3
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:
City Name Count sum
A Yuvakesh 2 9
C Yuvakesh 4 9
D Yuvakesh 3 9
A Raj 1 8
C Raj 4 8
E Raj 3 8
Please let me know how to get the desired result in mysql.
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
vijay bhaskar 01 August, 2012 23:11
I am not sure about analytic functions in mysql. However you can get the required
output by using two sub queries and joining them. This is shown below:
select t.city, t.Name, t.count_c, a.sum_s
from
(
select Name, sum(count_c) sum_s
from tablename
group by Name
) a,
tablename t
where a.Name = t.Name
Yuvakesh Y 02 August, 2012 22:23
Thanks a lot Vijay, am able to get desired result by using the above query....
Regards,
Yuvakesh
mann 19 October, 2012 23:29
Is there any solution for solving the first query without using inbuilt function?pls help..
Reply
Yuvakesh Y 14 September, 2012 00:25
Hi Vijay,
I would like to delete 'last_data_updated' for table_id group from my etl_run table by using the
below query
Delete from etl_run where last_data_updated not in
(select max(last_data_updated) as last_data_updated
from `etl_run` where status='Completed'
group by table_ID);
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...'
Please suggest me how to achieve this in another way...
Thanks in advance.
Regards,
Yuvakesh
Reply
Replies
vijay bhaskar 14 September, 2012 06:30
Hi,
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.
delete from etl_run t
where not exists
(select 1 from etl_run
where status ='completed'
and table_id = t.table_id
group by table_id
having max(last_data_updated) = t.last_data_updated
);
delete from etl_run t
where last_data_updated !=
(select max(last_data_updated) from etl_run
where status ='completed'
and table_id = t.table_id
);
Yuvakesh Y 18 September, 2012 02:54
Thanks Vijay,with the help of the above logic I have developed required query.
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
Puppala Kalyani 27 October, 2012 06:36
Hi, I Think the expected result for the question No. 2 is as follows.
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
Yuvakesh Y 30 October, 2012 07:13
Hi Vijay,
I am able to split full name as Last name, First name Middle name as given below.
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
Name=’Ranjith Kumar SR’
Expected=SR Kumar, Ranjith
That means I don’t wont to consider suffixes (Mr, Mrs, JR, SR) as a last name.
Kindly let me know how to get this in Mysql.
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
Enter your comment...
Comment as: Google Accoun
Publish Preview
Newer Post Home Older Post
Subscribe to: Post Comments (Atom)
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle_30.html 11/11