0% found this document useful (0 votes)
19 views

SQL Queries Interview Questions - Oracle Part 1

Uploaded by

RaJu Bhai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

SQL Queries Interview Questions - Oracle Part 1

Uploaded by

RaJu Bhai
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

11/29/2018 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

Unix Sed Command to Delete Lines in File - 15 Examples


To solve these interview questions on SQL queries you have to create the products, sales tables
in your oracle database. The "Create Table", "Insert" statements are provided below. String Functions in Hive

Top Examples of Awk Command in Unix

CREATE TABLE PRODUCTS Sed Command in Unix and Linux Examples


(
PRODUCT_ID INTEGER, Design/Implement/Create SCD Type 2 Effective Date
Mapping in Informatica
PRODUCT_NAME VARCHAR2(30)
); Date Functions in Hive
CREATE TABLE SALES
( SQL Queries Interview Questions - Oracle Part 1
SALE_ID INTEGER,
Top Unix Interview Questions - Part 1
PRODUCT_ID INTEGER,
YEAR INTEGER, Update Strategy Transformation in Informatica
Quantity INTEGER,
PRICE INTEGER
); Have Questions? Follow Me

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


INSERT INTO PRODUCTS VALUES ( 200, 'IPhone');
INSERT INTO PRODUCTS VALUES ( 300, 'Samsung');
INSERT INTO PRODUCTS VALUES ( 400, 'LG');

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;

The products table contains the below data.

SELECT * FROM PRODUCTS; 994 have me in circles View all

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
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 2/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1

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
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 3/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1

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.

Method1: 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

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 the NOT EXISTS operator.

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

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

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

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.

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:

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.

9 comments:

Neel 02 January, 2012 11:21


Please provide simple sql (dont use analytical function) to question no 4 (i.e Write a query to
select the top product sold in each year)
Reply

vijay bhaskar 02 January, 2012 20:37

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

Without using analytical functions.

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;

What if we want to get top 3 products sold in every year?


Reply

Neel 03 January, 2012 05:46


Thanks Vijay for quick and super easy answer, i was actually looking for such kind of solution.
Thanks a lot.
Now to your question (about top 3 product sold each year), we can do two ways: -- (i am
assuming only top 2 -- as number of records r 3 total each year..so top 3 would include all rows :)
hence query for top 2 result.......

----Using analytical function----

select s.product_id, s.year, s.quantity


from
(select product_id, year, quantity, rank() over(partition by year order by quantity desc) sno
from sales )s
where s.sno <=2;
-- product name can be found by joining products table to above result set

--using simple sql -- (corelated query)

select ad.product_id, ad.year, ad.quantity


from sales ad
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 9/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1

where 2 >= ( select count(quantity) from sales b


where ad.quantity <= b.quantity and ad.year=b.year
)
order by ad.year;

please suggest if we can achieve same using some other simple sql(no anlaytic) way.
Reply

Replies

Pradeep Anumala 27 March, 2017 20:01


select s.product_id from
(select max(quantity) q,year
from sales
group by year) sp,
sales s
where s.year= sp.year
and s.quantity =sp.q

Reply

vijay bhaskar 03 January, 2012 19:56


@Neel

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

Neel 03 January, 2012 23:12


Vijay,
I executed corelated query again and again, and found the result which include same quantity in
same year (star marked, manually):

see the result set:


id year quantity
--- ----- ----
100 2010 25
300 2010 20
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 10/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1

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

Mahesh Jagdale 21 March, 2017 04:13


Q1) Write a SQL query to find the products which have continuous increase in sales every year?
Mysql not support for LEAD, OVER

How To Solve this in Mysql?


Reply

RAVICHANDRA JL 01 August, 2017 07:49


Write a SQL query to find the products which does not have sales at all?For this question we can
use below query as well
select product_name from products where product_id in (select product_id from products minus
select product_id from sales)
Reply

Shivam Maltare 09 May, 2018 22:17


4. Write a query to select the top product sold in each year?
this will fail if you add some more column, but i have a solution for it :
select p.product_id,p.product_name,s.year,s.quantity,s.price
from products p,
sales s
where p.product_id=s.product_id(+)
and quantity||year in (select max(quantity)||year from sales group by year)
order by s.year
Reply
https://www.folkstalk.com/2011/12/sql-queries-interview-questions-oracle.html 11/12
11/29/2018 SQL Queries Interview Questions - Oracle Part 1

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.html 12/12

You might also like