Syntel Frequently Asked Oracle SQL Queries In Written Test Interview Questions
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.
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 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;
Write a query to generate sequence numbers from 1 to the specified number N?
Solution:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL<=&N;
Write a query to display only Sunday dates from Jan, 2000 to till now?
Solution:
SELECT C_DATE,
TO_CHAR(C_DATE,'DY')
FROM
(
SELECT TO_DATE('01-JAN-2000','DD-MON-YYYY')+LEVEL-1 C_DATE
FROM DUAL
CONNECT BY LEVEL <=
(SYSDATE - TO_DATE('01-JAN-2000','DD-MON-YYYY')+1)
)
WHERE TO_CHAR(C_DATE,'DY') = 'SUN';
Write a query to duplicate each row based on the value in the repeat column? The input table data looks like as below
Products, Repeat
----------------
A, 3
B, 5
C, 2
Now in the output data, the product A should be repeated 3 times, B should be repeated 5 times and C should be repeated 2 times. The output will look like as below
Products, Repeat
----------------
A, 3
A, 3
A, 3
B, 5
B, 5
B, 5
B, 5
B, 5
C, 2
C, 2
Solution:
SELECT PRODUCTS,
REPEAT
FROM T,
( SELECT LEVEL L FROM DUAL
CONNECT BY LEVEL <= (SELECT MAX(REPEAT) FROM T)
) A
WHERE T.REPEAT >= A.L
ORDER BY T.PRODUCTS;
Write a query to display each letter of the word "BIG" in a separate row?
B
I
G
Solution:
SELECT SUBSTR('SMILE',LEVEL,1) A
FROM DUAL
CONNECT BY LEVEL <=LENGTH('BIG');
Convert the string "SMILE" to Ascii values? The output should look like as 83,77,73,76,69. Where 83 is the ascii value of S and so on.
The ASCII function will give ascii value for only one character. If you pass a string to the ascii function, it will give the ascii value of first letter in the string. Here i am providing two solutions to get the ascii values of string.
Solution 1:
SELECT SUBSTR(DUMP('SMILE'),15)
FROM DUAL;
Solution2:
SELECT WM_CONCAT(A)
FROM
(
SELECT ASCII(SUBSTR('SMILE',LEVEL,1)) A
FROM DUAL
CONNECT BY LEVEL <=LENGTH('SMILE')
);
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.
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.
Syntel Frequently Asked Oracle SQL Queries In Written Test Interview Questions |
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 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;
Write a query to generate sequence numbers from 1 to the specified number N?
Solution:
SELECT LEVEL FROM DUAL CONNECT BY LEVEL<=&N;
Write a query to display only Sunday dates from Jan, 2000 to till now?
Solution:
SELECT C_DATE,
TO_CHAR(C_DATE,'DY')
FROM
(
SELECT TO_DATE('01-JAN-2000','DD-MON-YYYY')+LEVEL-1 C_DATE
FROM DUAL
CONNECT BY LEVEL <=
(SYSDATE - TO_DATE('01-JAN-2000','DD-MON-YYYY')+1)
)
WHERE TO_CHAR(C_DATE,'DY') = 'SUN';
Write a query to duplicate each row based on the value in the repeat column? The input table data looks like as below
Products, Repeat
----------------
A, 3
B, 5
C, 2
Now in the output data, the product A should be repeated 3 times, B should be repeated 5 times and C should be repeated 2 times. The output will look like as below
Products, Repeat
----------------
A, 3
A, 3
A, 3
B, 5
B, 5
B, 5
B, 5
B, 5
C, 2
C, 2
Solution:
SELECT PRODUCTS,
REPEAT
FROM T,
( SELECT LEVEL L FROM DUAL
CONNECT BY LEVEL <= (SELECT MAX(REPEAT) FROM T)
) A
WHERE T.REPEAT >= A.L
ORDER BY T.PRODUCTS;
Write a query to display each letter of the word "BIG" in a separate row?
B
I
G
Solution:
SELECT SUBSTR('SMILE',LEVEL,1) A
FROM DUAL
CONNECT BY LEVEL <=LENGTH('BIG');
Convert the string "SMILE" to Ascii values? The output should look like as 83,77,73,76,69. Where 83 is the ascii value of S and so on.
The ASCII function will give ascii value for only one character. If you pass a string to the ascii function, it will give the ascii value of first letter in the string. Here i am providing two solutions to get the ascii values of string.
Solution 1:
SELECT SUBSTR(DUMP('SMILE'),15)
FROM DUAL;
Solution2:
SELECT WM_CONCAT(A)
FROM
(
SELECT ASCII(SUBSTR('SMILE',LEVEL,1)) A
FROM DUAL
CONNECT BY LEVEL <=LENGTH('SMILE')
);
Consider the following friends table as the source
Name, Friend_Name
-----------------
sam, ram
sam, vamsi
vamsi, ram
vamsi, jhon
ram, vijay
ram, anand
Here ram and vamsi are friends of sam; ram and jhon are friends of vamsi and so on. Now write a query to find friends of friends of sam. For sam; ram,jhon,vijay and anand are friends of friends. The output should look as
Name, Friend_of_Firend
----------------------
sam, ram
sam, jhon
sam, vijay
sam, anand
Solution:
SELECT f1.name,
f2.friend_name as friend_of_friend
FROM friends f1,
friends f2
WHERE f1.name = 'sam'
AND f1.friend_name = f2.name;
This is an extension to the problem 1. In the output, you can see ram is displayed as friends of friends. This is because, ram is mutual friend of sam and vamsi. Now extend the above query to exclude mutual friends. The output should look as
Name, Friend_of_Friend
----------------------
sam, jhon
sam, vijay
sam, anand
Solution:
SELECT f1.name,
f2.friend_name as friend_of_friend
FROM friends f1,
friends f2
WHERE f1.name = 'sam'
AND f1.friend_name = f2.name
AND NOT EXISTS
(SELECT 1 FROM friends f3
WHERE f3.name = f1.name
AND f3.friend_name = f2.friend_name);
Write a query to get the top 5 products based on the quantity sold without using the row_number analytical function? The source data looks as
Products, quantity_sold, year
-----------------------------
A, 200, 2009
B, 155, 2009
C, 455, 2009
D, 620, 2009
E, 135, 2009
F, 390, 2009
G, 999, 2010
H, 810, 2010
I, 910, 2010
J, 109, 2010
L, 260, 2010
M, 580, 2010
Solution:
SELECT products,
quantity_sold,
year
FROM
(
SELECT products,
quantity_sold,
year,
rownum r
from t
ORDER BY quantity_sold DESC
)A
WHERE r <= 5;
Write a query to produce the same output using row_number analytical function?
Solution:
SELECT products,
quantity_sold,
year
FROM
(
SELECT products,
quantity_sold,
year,
row_number() OVER(
ORDER BY quantity_sold DESC) r
from t
)A
WHERE r <= 5;
write a query to get the top 5 products in each year based on the quantity sold?
Solution:
SELECT products,
quantity_sold,
year
FROM
(
SELECT products,
quantity_sold,
year,
row_number() OVER(
PARTITION BY year
ORDER BY quantity_sold DESC) r
from t
)A
WHERE r <= 5;
Post a Comment