Unable to Solve SQL Query Interview Question

Pony2

Coder
Hi all,

I'm having difficulty solving a SQL query question that I recently encountered online. I'm trying to write a query that will return the sum of all odd integers from 1 to 100, but I'm not sure how to go about it.

Here is the code I have so far:

SELECT SUM(column_name)
FROM table_name
WHERE column_name % 2 != 0;

I'm not sure if this is the correct approach or if I'm missing something. Any help would be greatly appreciated.

Thanks in advance!
 
Approach is to initialize a num with 1 and sum with 0 and keep incrementing num by 2 and sum by num until num <= N.


num NUMBER(3) = 1;
sum1 NUMBER(4) = 0;
WHILE num <= 5 LOOP
dbms_output.Put_line(num);

sum1 = sum1 + num;
num = num + 2;
END LOOP;
dbms_output.Put_line('Sum of all odd numbers is '|| sum1);
END;
Hi all,

I'm having difficulty solving a SQL query question that I recently encountered online. I'm trying to write a query that will return the sum of all odd integers from 1 to 100, but I'm not sure how to go about it.

Here is the code I have so far:

SELECT SUM(column_name)
FROM table_name
WHERE column_name % 2 != 0;

I'm not sure if this is the correct approach or if I'm missing something. Any help would be greatly appreciated.

Thanks in advance!
 
Good shot @Sudo_coder12 ! This is probably the right answer, as the question just asks for a computation without the need for having a table.
If you actually have a table with numbers 1..100, the solution suggested by the OP works beautifully.
 
The code you provided is written in PL/SQL, a procedural language used in Oracle databases. It appears to be a program that calculates the sum of all odd numbers up to a given limit (5 in this case).

Here's a breakdown of the code:

1. The variables `num` and `sum1` are declared and initialized to 1 and 0, respectively.
2. The `WHILE` loop is used to iterate as long as `num` is less than or equal to 5.
3. Inside the loop, the value of `num` is printed using `dbms_output.put_line()`.
4. The sum is calculated by adding `num` to `sum1`.
5. `num` is incremented by 2.
6. The loop continues until `num` exceeds 5.
7. Finally, the total sum of the odd numbers is printed using `dbms_output.put_line()`.

Please note that this code snippet is specific to Oracle's PL/SQL language and requires the use of the `dbms_output` package to display the output.
Approach is to initialize a num with 1 and sum with 0 and keep incrementing num by 2 and sum by num until num <= N.


num NUMBER(3) = 1;
sum1 NUMBER(4) = 0;
WHILE num <= 5 LOOP
dbms_output.Put_line(num);

sum1 = sum1 + num;
num = num + 2;
END LOOP;
dbms_output.Put_line('Sum of all odd numbers is '|| sum1);
END;
 
You're right @Pony2 . I did not pick up on that fact. Not sure now that this answer would qualify, as it is not SQL, strictly speaking. I could not find the original interview question to see what exactly they were asking. Strange thing to want to do in SQL anyway.... It seems more like a beginner's exercise one would do in JS or Python.
 
Top Bottom