Write a Query to print numbers in sequence from 1 to 100 with Conditions

 Question : Write a Query to print numbers in sequence from 1 to 100

    Conditions : 

1. if number is divided by 15, Print 'FOO YOYO'

2. if number is divided by 3, Print 'FOO'

3. if number is divided by 5, Print 'YOYO'

4. ELSE Print number.

           

SQL Query : 

SELECT DECODE(MOD (COL,15),0,'FOFOYOYO',
DECODE(MOD(COL,3),0,'FOO',
DECODE(MOD(COL,5),0,'YOYO',COL))) VAL
FROM
(SELECT LEVEL COL FROM DUAL CONNECT BY LEVEL<=100);


Result:
1
2
FOO
4
YOYO
FOO
7
8
FOO
YOYO
11
FOO
13
14
FOFOYOYO
16
17
FOO
19
YOYO
FOO
22
23
FOO
YOYO
26
FOO
28
29
FOFOYOYO
31
32
FOO
34
YOYO
FOO
37
38
FOO
YOYO
41
FOO
43
44
FOFOYOYO
46
47
FOO
49
YOYO
FOO
52
53
FOO
YOYO
56
FOO
58
59
FOFOYOYO
61
62
FOO
64
YOYO
FOO
67
68
FOO
YOYO
71
FOO
73
74
FOFOYOYO
76
77
FOO
79
YOYO
FOO
82
83
FOO
YOYO
86
FOO
88
89
FOFOYOYO
91
92
FOO
94
YOYO
FOO
97
98
FOO
YOYO

Post a Comment

0 Comments