Skip to Main Content
  • Questions
  • How to add two numbers without + or SUM

Breadcrumb

May 4th

Question and Answer

Connor McDonald

Thanks for the question.

Asked: September 29, 2016 - 1:07 pm UTC

Last updated: February 19, 2019 - 5:47 am UTC

Version: 11.0

Viewed 10K+ times! This question is

You Asked

Sir...i was laughed when i was seen one answer from you...That is..). How to add two numbers without using + and sum functions...

Actually that is an interview question.When i was attended an interview at TCS then were asked me these type of question...

Note:--But thank you so much for your wonderful answers.

and Connor said...

If any company asked me that at an interview, I would seriously consider looking elsewhere, because it doesn't sound like a place I'd like to work at.

For example, here's an answer

select 3 + 4 from dual

can also be written as

select 3 - ( -1 * 4 ) from dual ;

No "+" sign and no sum...

All the proves to me is that the company asking such questions doesnt have a clue what they really want from an Oracle professional

Rating

  (4 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A procedural error from the Company, sure...

J. Laurindo Chiappa, September 29, 2016 - 6:14 pm UTC

Yes, for sure this kind of test/question is totally unappropriate to avaliate Oracle database professionals, be they programmes, DBAs or what else - for the record, this kind of unusual question is common place in general programmers admissions, aiming to provoke lateral and out-of-the-box thinking, and to avaliate logical capabilities , too - http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/ is an example of the answer expected...

Of course, many other kind of database-related problems could (and would) be better directed to a database professional, no questions about it... In my opinion, the Company probably is simply using an already-existing form/procedure to avaliate the database IT professionals, only - a venial sin, not a mortal one, for sure an error but not a cause for total and complete rejection from the candidate to admission, I think...

Regards,

J. Laurindo Chiappa

Question

PraveenDarla, February 16, 2019 - 1:04 pm UTC

Hi Sir,

How to get sum of salary from emp table without using aggregate function?

Regards,
Praveen Darla
Connor McDonald
February 18, 2019 - 2:06 am UTC

SQL> select tot
  2  from
  3  ( select rownum r, sal from scott.emp )
  4  model
  5  dimension by (r)
  6  measures (0 tot, sal)
  7  rules ( tot[1] = sal[1],
  8          tot[r>1]=tot[CV()-1]+sal[CV()-1]
  9          )
 10  order by 1 desc
 11  fetch first 1 row only;

       TOT
----------
     28525


string 'em up

Racer I., February 18, 2019 - 7:47 am UTC

Hi,

This seems to be a favorite approach (from obfuscation contests) :

with
rd as (
  select 1 p, 0 pp, 4000 v from dual union all
  select 2, 1, 4000 from dual union all
  select 3, 2, 4000 from dual union all
  select 4, 3, 4000 from dual union all
  select 5, 4, 4000 from dual union all
  select 6, 5, 4000 from dual union all
  select 7, 6, 4000 from dual union all
  select 8, 7, 4000 from dual union all
  select 9, 8, 4000 from dual),
rd1(p, v) AS (
  select p, TO_CLOB(RPAD('X', v, 'X')) v
  from rd
  where pp = 0
  union all 
  select rd2.p, TO_CLOB(RPAD('X', rd2.v, 'X')) || rd1.v v
  from rd rd2, rd1
  where rd2.pp = rd1.p)
select p, v, length(v) lv
from rd1

Should work for integers between 1 and 4000. There may be some slow-down eventually ;)
Connor McDonald
February 19, 2019 - 5:47 am UTC

:-)

heavy lifting

Racer I., February 18, 2019 - 8:27 am UTC

Hi,

Just found RPAD can do CLOB :
RPAD(TO_CLOB('X'), v, 'X')
so any size numbers can be added.

regards,