Deloitte Oracle SQL Queries In Written Test Interview

Deloitte Frequently Asked Oracle SQL Queries In Written Test Interview Questions

Oracle Drop Query

Oracle drop query is used to drop a table or view. It doesn't have structure and data. For example:

drop table customers;

Get Joining Date,Time including milliseconds from employee table

select to_char(JOINING_DATE,'dd/mm/yyyy HH:mi:ss.ff') from
EMPLOYEE . Column Data Type should be “TimeStamp”

Oracle Create Query

Oracle create query is used to create a table, view, sequence, procedure and function. For example:

CREATE TABLE customers
( id number(10) NOT NULL,
  name varchar2(50) NOT NULL,
  city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (id) 
);
Deloitte Frequently Asked Oracle SQL Queries In Written Test Interview Questions
Deloitte Frequently Asked Oracle SQL Queries In Written Test Interview Questions

Oracle Alter Query

Oracle alter query is used to add, modify, delete or drop colums of a table. Let's see a query to add column in customers table:

ALTER TABLE customers
ADD age varchar2(50);

How to add column in a table

Syntax:

ALTER TABLE table_name
  ADD column_name column-definition;
Example:

Consider that already existing table customers. Now, add a new column customer_age into the table customers.

ALTER TABLE customers
  ADD customer_age varchar2(50);
Now, a new column "customer_age" will be added in customers table.

How to add multiple columns in the existing table

Syntax:

ALTER TABLE table_name
  ADD (column_1 column-definition,
       column_2 column-definition,
       ...
       column_n column_definition);
Example

ALTER TABLE customers
  ADD (customer_type varchar2(50),
       customer_address varchar2(50));
Now, two columns customer_type and customer_address will be added in the table customers.

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;

Here the lead analytic function will get the quantity of a product in its previous year.

STEP 2: 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

Write a SQL query to find the products which does not have sales at all?

Solution:

“LG” is the only product which does not have sales at all. This can be achieved in three ways.

Method 1: 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

Method 2: 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

Method 3: Using the NOT EXISTS operator.

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

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

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

Solution:

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;

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.

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;

Write a query to find the products whose quantity sold in a year should be greater than the average quantity of the product sold across all the years?

Solution:

This can be solved with the help of correlated query. The SQL query for this is

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 S1
       WHERE S1.PRODUCT_ID = S.PRODUCT_ID
       );

Post a Comment

Previous Post Next Post