CISCO Systems Oracle SQL Queries In Written Test Interview

CISCO Systems Frequently Asked Oracle SQL Queries In Written Test Interview Questions

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.

How to modify column of a table

Syntax:

ALTER TABLE table_name
  MODIFY column_name column_type;
Example:

ALTER TABLE customers
  MODIFY customer_name varchar2(100) not null;
Now the column column_name in the customers table is modified
to varchar2 (100) and forced the column to not allow null values.
CISCO Systems Frequently Asked Oracle SQL Queries In Written Test Interview Questions
CISCO Systems Frequently Asked Oracle SQL Queries In Written Test Interview Questions

How to modify multiple columns of a table
Syntax:

ALTER TABLE table_name
  MODIFY (column_1 column_type,
          column_2 column_type,
          ...
          column_n column_type);
Example:

ALTER TABLE customers
  MODIFY (customer_name varchar2(100) not null,
          city varchar2(100));
This will modify both the customer_name and city columns in the table.

How to drop column of a table

Syntax:

ALTER TABLE table_name
  DROP COLUMN column_name;
Example:

ALTER TABLE customers

  DROP COLUMN customer_name;
This will drop the customer_name column from the table.

How to rename column of a table

Syntax:

ALTER TABLE table_name
  RENAME COLUMN old_name to new_name;
Example:

ALTER TABLE customers
 RENAME COLUMN customer_name to cname;
This will rename the column customer_name into cname.

How to rename table

Syntax:

ALTER TABLE table_name
  RENAME TO new_table_name;
Example:

ALTER TABLE customers
RENAME TO retailers;
This will rename the customer table into "retailers" table.

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