SQL Queries Interview Questions - Oracle Analytical Functions Part 1
SQL Queries Interview Questions - Oracle Analytical Functions Part 1
SQL Queries Interview Questions - Oracle Analytical Functions Part 1
Home Data Warehouse Informatica Informatica Scenarios Informatica Cloud Oracle Unix Hadoop
Search... Search
SQL Queries Interview Questions - Oracle Analytical
Functions Part 1 Popular Posts
Analytic functions compute aggregate values based on a group of rows. They differ from Informatica Scenario Based Interview Questions with
aggregate functions in that they return multiple rows for each group. Most of the SQL developers Answers - Part 1
won't use analytical functions because of its cryptic syntax or uncertainty about its logic of
Unix Sed Command to Delete Lines in File - 15 Examples
operation. Analytical functions saves lot of time in writing queries and gives better performance
when compared to native SQL. String Functions in Hive
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
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 1/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
vijay bhaskar
INSERT INTO SALES VALUES ( 6, 200, 2010, 10, 9000); Add to circles
INSERT INTO SALES VALUES ( 7, 200, 2011, 15, 9000);
INSERT INTO SALES VALUES ( 8, 200, 2012, 20, 9000);
INSERT INTO SALES VALUES ( 9, 200, 2008, 13, 9000);
INSERT INTO SALES VALUES ( 10,200, 2009, 14, 9000);
SELECT Year,
COUNT(1) CNT
FROM SALES
GROUP BY YEAR;
YEAR CNT
---------
2009 3
2010 3
2011 3
2008 3
2012 3
SELECT SALE_ID,
PRODUCT_ID,
Year,
QUANTITY,
PRICE,
COUNT(1) OVER (PARTITION BY YEAR) CNT
FROM SALES;
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 3/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
From the ouputs, you can observe that the aggregate functions return only one row per group
whereas analytic functions keeps all the rows in the gorup. Using the aggregate functions, the
select clause contains only the columns specified in group by clause and aggregate functions
whereas in analytic functions you can specify all the columns in the table.
The PARTITION BY clause is similar to GROUP By clause, it specifies the window of rows that
the analytic funciton should operate on.
I hope you got some basic idea about aggregate and analytic functions. Now lets start with
solving the Interview Questions on Oracle Analytic Functions.
1. Write a SQL query using the analytic function to find the total sales(QUANTITY) of each
product?
Solution:
SUM analytic function can be used to find the total sales. The SQL query is
SELECT PRODUCT_ID,
QUANTITY,
SUM(QUANTITY) OVER( PARTITION BY PRODUCT_ID ) TOT_SALES
FROM SALES;
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 4/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
2. Write a SQL query to find the cumulative sum of sales(QUANTITY) of each product? Here first
sort the QUANTITY in ascendaing order for each product and then accumulate the QUANTITY.
Cumulative sum of QUANTITY for a product = QUANTITY of current row + sum of QUANTITIES
all previous rows in that product.
Solution:
We have to use the option "ROWS UNBOUNDED PRECEDING" in the SUM analytic function to
get the cumulative sum. The SQL query to get the ouput is
SELECT PRODUCT_ID,
QUANTITY,
SUM(QUANTITY) OVER( PARTITION BY PRODUCT_ID
ORDER BY QUANTITY ASC
ROWS UNBOUNDED PRECEDING) CUM_SALES
FROM SALES;
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 5/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
The ORDER BY clause is used to sort the data. Here the ROWS UNBOUNDED PRECEDING
option specifies that the SUM analytic function should operate on the current row and the
pervious rows processed.
3. Write a SQL query to find the sum of sales of current row and previous 2 rows in a product
group? Sort the data on sales and then find the sum.
Solution:
SELECT PRODUCT_ID,
QUANTITY,
SUM(QUANTITY) OVER(
PARTITION BY PRODUCT_ID
ORDER BY QUANTITY DESC
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 6/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
The ROWS BETWEEN clause specifies the range of rows to consider for calculating the SUM.
Solution:
SELECT PRODUCT_ID,
QUANTITY,
PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY QUANTITY ASC)
OVER (PARTITION BY PRODUCT_ID) MEDIAN
FROM SALES;
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 7/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
5. Write a SQL query to find the minimum sales of a product without using the group by clause.
Solution:
SELECT PRODUCT_ID,
YEAR,
QUANTITY
FROM
(
SELECT PRODUCT_ID,
YEAR,
QUANTITY,
ROW_NUMBER() OVER(PARTITION BY PRODUCT_ID
ORDER BY QUANTITY ASC) MIN_SALE_RANK
FROM SALES
) WHERE MIN_SALE_RANK = 1;
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 8/10
11/29/2018 SQL Queries Interview Questions - Oracle Analytical Functions Part 1
Recommended Reading:
If you like this article, then please share it or click on the google +1 button.
4 comments:
Publish Preview
https://www.folkstalk.com/2012/01/analytical-functions-interview.html 10/10