Tricky SQL Queries For Interview
Tricky SQL Queries For Interview
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
Answer :
1
2
3
4
SELECT
SUM(CASE WHEN num < 0 THEN num ELSE 0 END) neg,
SUM(CASE WHEN num > 0 THEN num ELSE 0 END)pos
FROM neg_pos;
gives
neg | pos
-10 | 10
****************************************************
2. How to search for strings containing % in Oracle? Search for columns containing % in Oracle.
In ORACLE , you can use the ESCAPE keyword to search for strings containing %. Otherwise it would be
considered as a META CHARACTER .
Using the escape character ( to search for strings containing like ABC %%TRF, TR%FF or %GH)
Answer :
1
2
1
2
****************************************************
3. How does one remove special characters in ORACLE?
To replace special characters in a string with null use translate :
translate(string,'to_replace,'replace_with)
for eg:
1
2
SELECT translate
('asdfsd@#@$#$%$sdfg&;','!@#$%^&;*()_+=-`~?><:/.,',' ') FROM dual;
will returnasdfsdsdfg
To remove quotes, use two quotes for every single quote as shown below:
1
2
3
4
CREATE
INSERT
SELECT
SELECT
A table has columns with numbers and numbers with alphabets. Write a query to select only those rows
which contains alphanumeric values.
1
2
3
4
5
6
7
8
insert into
insert into
insert into
insert into
commit;
alpha_numberic
alpha_numberic
alpha_numberic
alpha_numberic
values
values
values
values
('19b45');
('231');
('1000cc');
('a1000');
Answer:
1
2
col1
a1093b
19b45
1000cc
a1000
****************************************************
4. Give a string of format NN/NN, verify that the first and last two characters are numbers and that the
middle character is/. Print the expression NUMBER if valid, NOT NUM if not valid. Use the following values
to test your solution. 12/34,01/1a, 99/98.
Answer:
1
2
3
4
5
6
****************************************************
5. From the given table, find those employees who are more than 21 years of age.
1
2
3
4
5
6
7
8
9
10
11
Answer:
1
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
CREATE
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
CREATE
INSERT
INSERT
INSERT
Answer:
1
2
3
4
5
6
****************************************************
7. Some questions on the dual table.
Select two rows from dual
1
2
3
1
2
or
1
2
Another tricky question on dual involves the use of decode with NULL.
OUTPUT1
Although two NULL values are not equal, the output is 1, as decode checks for the existence of NULL and does not
compare the two values.
****************************************************
8. Find the missing sequence. Table test_number contains the sequence for each id. Table
test_number_min_max contains the minimum and maximum number for each id. We need to find the missing
number between the minimum and maximum number for each id. text column can be ignored.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
CREATE
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
create
INSERT
INSERT
INSERT
number);
Answer:
1
2
3
4
5
6
7
8
SELECT r id,rn seq FROM (SELECT ROWNUM rn FROM all_objects WHERE ROWNUM
<13),
(SELECT ROWNUM r FROM all_objects
WHERE ROWNUM <4),test_number_min_max m
WHERE r=id
AND rn >= mn
AND rn <= mx
AND (r,rn) NOT IN
(SELECT id,seq FROM test_number)
OR
1
2
3
4
5
6
7
8
9
OUTPUT :
ID SEQ
12
19
1 11
1 12
25
28
29
32
33
****************************************************
9. Get the following OUTPUT using dual
1LR
111
112
113
121
122
123
131
132
133
Answer:
1
2
3
4
SELECT * FROM
(SELECT 1 FROM dual),
(SELECT LEVEL l FROM dual CONNECT BY LEVEL <4),
(SELECT LEVEL r FROM dual CONNECT BY LEVEL <4);
****************************************************
10. Check the Input and Output and try to figure out the question.
2
3
4
5
6
7
INSERT
INSERT
INSERT
INSERT
INSERT
INTO
INTO
INTO
INTO
INTO
test_output
test_output
test_output
test_output
test_output
VALUES
VALUES
VALUES
VALUES
VALUES
('AN','TTT',6);
('AN','TTT',7);
('BB','RRR',8);
('BB','RRR',9);
('BB','RRR',10);
Input :
NAME
CITY
NUM
AN
TTT
AN
TTT
AN
TTT
BB
RRR
BB
RRR
BB
RRR
10
Answer :
1
2
3
4
5
6
7
SELECT (CASE WHEN rn=1 THEN NAME ELSE NULL END) NAME,
(CASE WHEN rn=1 THEN CITY ELSE NULL END ) CITY,
num
FROM
(SELECT NAME,city,num,
row_number() over(PARTITION BY NAME,city ORDER BY NAME) rn
FROM test_output);
Output :
NAME
AN
CITY
TTT
NUM
5
6
7
BB
RRR
8
9
10
****************************************************
11.
****************************************************
12. Beginner question based on the above logic. From the table given below, all the numbers should be on
the first column and the alphabets on the second column.
ALPA RANK
a1
b2
c4
x5
y6
z8
9g
0f
7e
3d
All the alphabets on column B and all numbers in column A
OUTPUT:
AB
0f
1a
2b
3d
4c
5x
6y
7e
8z
9g
Answer:
1
2
****************************************************
13. One of the most common question asked in interviews. To find the second (or third or fourth) nth
highest number in each group.
1
2
3
4
5
6
7
8
9
10
11
CREATE
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
INSERT
12
13
14
15
16
17
18
INSERT INTO
INSERT INTO
INSERT INTO
INSERT INTO
COMMIT;
find_rank
find_rank
find_rank
find_rank
VALUES
VALUES
VALUES
VALUES
('G4',0);
('G5',-1);
('G5',-2);
('G5',-3);
Answer:
1
2
3
4
5
6
with &rank = 2
GROUP_ID VAL RN
G1 13 2
G2 19 2
G3 2 2
G5 -2 2
If we need to have G4 also in the output even though it does not have a second/third highest value then :
1
2
3
4
5
6
7
8
9
10
with &rank =3
GROUP_ID VAL
G1 12
G2 10
G3 1
G4 N/A
G5 -3
****************************************************
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Answer :
1
2
3
4
5
6
7
8
SELECT stu_name,
max(CASE WHEN subject='ECO'
max(CASE WHEN subject='HIS'
max(CASE WHEN subject='MAT'
max(CASE WHEN subject='GEO'
max(CASE WHEN subject='SCI'
FROM col_to_rows
GROUP BY stu_name
THEN
THEN
THEN
THEN
THEN
marks
marks
marks
marks
marks
ELSE
ELSE
ELSE
ELSE
ELSE
0
0
0
0
0
END)
END)
END)
END)
END)
ECO,
HIS,
MAT,
GEO,
SCI
OR
1
2
3
4
5
6
7
SELECT stu_name,
MAX(decode(subject,'ECO',marks,0))
MAX(decode(subject,'HIS',marks,0))
MAX(decode(subject,'MAT',marks,0))
MAX(decode(subject,'GEO',marks,0))
MAX(decode(subject,'SCI',marks,0))
FROM col_to_rows GROUP BY stu_name
ECO,
HIS,
MAT,
GEO,
SCI
OR
1
2
3
4
5
6
7
8
SELECT stu_name,
max(CASE WHEN rn=1 THEN marks ELSE 0 END) ECO,
max(CASE WHEN rn=2 THEN marks ELSE 0 END) GEO,
max(CASE WHEN rn=3 THEN marks ELSE 0 END) HIS,
max(CASE WHEN rn=4 THEN marks ELSE 0 END) MAT,
max(CASE WHEN rn=5 THEN marks ELSE 0 END) SCI FROM
(SELECT stu_name,subject,marks, rank() over (PARTITION BY stu_name ORDER BY subject )rn
FROM col_to_rows)
GROUP BY stu_name
Output :
STU_NAME
ECO
HIS
MAT
GEO
SCI
GEORGE
77
99
64
85
98
ROBERT
71
90
84
95
58
TIMOTHY
56
55
67
54
45
****************************************************
15. Another question from http://asktom.oracle.com/pls/asktom/f?
p=100:11:0::::P11_QUESTION_ID:65356113852721 . This question teaches the trick to use decode with order
by to select your own ordering rule . In this case the minimum value should always be at the last row. The
other values are sorted in ascending order. You can create your own ordering rules.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
3
1
5
4
select * from test1
order by decode( c1, (select min(c1) from test1), to_number(null), c1);
C1
2
3
4
5
1
****************************************************
Post to Blogger
Post to Delicious
Post to Digg
Post to Facebook
5
5
5
5
5
5
5
5
Post to StumbleUpon
Post to Twitter
1
1
1
1
1
1
1
1
1
admin says:
July 1, 2011 at 10:02 pm
for more queries visit
http://oddabout.com/?page_id=1907
reply
2.
unknown says:
September 4, 2012 at 10:44 am
very nice collection of queries.helped me alotthanks
reply
3.
jatin says:
October 16, 2012 at 1:58 pm
very tricky, took time to understand them and very soon use them, quite helpful too
reply
4.
Ram says:
December 5, 2012 at 11:25 pm
Very Useful.Appreciate efforts for putting very useful info.
reply
5.
Vimal says:
March 4, 2013 at 8:01 pm
Hello,
Above Help is Great.
reply
6.
Nishu says:
April 7, 2013 at 6:25 pm
Buddy nice collection of queries helpde me a lot..keep finding more things like this..Thanks!!!
reply
7.
Sankar says:
May 16, 2013 at 10:33 pm
Thanks a lot for your effort.It helped me a lot.
reply
8.
dipu says:
May 17, 2013 at 8:42 pm
10. How to solve it
CREATE TABLE test_output(NAME VARCHAR2(5), city VARCHAR2(6), num NUMBER);
INSERT INTO test_output VALUES (AN,'TTT,5);
INSERT INTO test_output VALUES (AN,'TTT,6);
INSERT INTO test_output VALUES (AN,'SSS,7);
INSERT INTO test_output VALUES (BB,'SSS,8);
INSERT INTO test_output VALUES (BB,'GGG,9);
INSERT INTO test_output VALUES (BB,'GGG,10);
The required output as follows
=============================
NAME CITY NUM
=============================
AN TTT 5
SSS 7
BB SSS 8
GGG 9
10
=============================
reply
dipu says:
May 17, 2013 at 8:49 pm
Output :
NAMECITYNUM
ANTTT5
6
SSS7
BBSSS8
GGG9
10
reply
pradeep says:
May 20, 2013 at 1:07 pm
SELECT (CASE WHEN rn=1 and num=5 or num=8 THEN NAME ELSE END) NAME,
(CASE WHEN rn=1 or city=sss and name=bb THEN CITY ELSE END ) CITY,
num
FROM
(SELECT NAME,city,num,
row_number() over(PARTITION BY city,city ORDER BY NAME) rn
FROM test_outputed) as tabl
reply
pradeep says:
May 20, 2013 at 1:12 pm
SELECT (CASE WHEN rn=1 and num=5 or num=8 THEN NAME ELSE END) NAME,
(CASE WHEN rn=1 or city=sss and name=bb THEN CITY ELSE END ) CITY,
num
FROM
(SELECT NAME,city,num,
row_number() over(PARTITION BY city,city ORDER BY NAME) rn
FROM test_output) as tabl
reply
Garuda says:
January 29, 2014 at 6:25 pm
Thanks a lot for your effort.
Nice questions. keep posting
SIva says:
February 18, 2015 at 2:47 pm
Another generalised approach: hope it might help
from(select row_number() over (partitioned by name,city) as rn_1, row_number() over (partitioned by name) as rn_2
,name,city,num) select (case when rn_1 =1 and rn_2 =1 then Name when rn_1 = 1 and rn_2!=1 then city else num
end) as name,
(case when rn_1=1 and rn_2 = 1 then city when rn_1=1 and rn_2 !=1 then num else null end) as city,
(case when rn_1=1 and rn_2 =1 then num else null end) as num;