Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 September SQL Puzzle: SQL Advance Query – Print UP or DOWN or UpDown

SQL Puzzle: SQL Advance Query – Print UP or DOWN or UpDown

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and expected output to print “Up” if number is multiples of three and print “Down” if number is multiples of five. If number is multiples of three and five both, print “UpDown”

Expected Output: For top 100 numbers

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
number Result
----------- ------
1 1
2 2
3 Up
4 4
5 Down
6 Up
7 7
8 8
9 Up
10 Down
11 11
12 Up
13 13
14 14
15 UpDown
16 16
17 17
18 Up
19 19
20 Down
21 Up
22 22
23 23
24 Up
25 Down
26 26
27 Up
28 28
29 29
30 UpDown
31 31
32 32
33 Up
34 34
35 Down
36 Up
37 37
38 38
39 Up
40 Down
41 41
42 Up
43 43
44 44
45 UpDown
46 46
47 47
48 Up
49 49
50 Down
51 Up
52 52
53 53
54 Up
55 Down
56 56
57 Up
58 58
59 59
60 UpDown
61 61
62 62
63 Up
64 64
65 Down
66 Up
67 67
68 68
69 Up
70 Down
71 71
72 Up
73 73
74 74
75 UpDown
76 76
77 77
78 Up
79 79
80 Down
81 Up
82 82
83 83
84 Up
85 Down
86 86
87 Up
88 88
89 89
90 UpDown
91 91
92 92
93 Up
94 94
95 Down
96 Up
97 97
98 98
99 Up
100 Down

Solution:

1
2
3
4
5
6
7
8
9
SELECT DISTINCT number,
CASE WHEN number % 15 = 0 THEN 'UpDown'
WHEN number % 3 = 0 THEN 'Up'
WHEN number % 5 = 0 THEN 'Down'
ELSE
CAST(number AS VARCHAR(3))
END Result
FROM master..spt_values
WHERE number BETWEEN 1 AND 100 ORDER BY number

Please try the different solution for this puzzle and share it via comment...

Sep 9, 2017Anvesh Patel
Greenplum: Important Parameters of Resource Queue (Workload Management)Greenplum: Important Queries to Configure the Resource Queue (Workload Management)
Comments: 2
  1. Dinesh.IS
    October 12, 2017 at 7:11 am

    ;
    WITH CTE
    As
    (Select 1 As Number
    Union All
    Select Number+1
    From CTE
    Where Number+1<=100)
    Select *,(Case When Number%15=0 Then 'UpDown' Else
    Case When Number%3=0 Then 'Up' Else
    Case When Number%5=0 Then 'Down' Else Convert(VarChar,Number) End End End) As Result From CTE

    ReplyCancel
  2. Ankush
    September 14, 2018 at 12:33 pm

    Select rwn, CASE WHEN mod(rwn,3) =0 AND mod(rwn,5) =0 THEN ‘UpDown’
    WHEN mod(rwn,3) = 0 then ‘Up’
    WHEN mod(rwn,5) = 0 then ‘Down’
    ELSE to_char(rwn) END from
    (Select rownum as rwn from dual
    connect by rownum <=100)

    ReplyCancel

Leave a Reply to Dinesh.IS Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

September 9, 2017 2 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....