Easy: select specific series of numbers

I have collected some interview questions that may ask in interviews or it may help you understand SQL well.

Let me start with the questions:

1) I have a table Table1 with only one column Nums. It contains serial numbers. For instance I will consider 1 to 10 as below.
CREATE TABLE Table1 (Nums int)

INSERT INTO Table1 (Nums) Values (1)
INSERT INTO Table1 (Nums) Values (2)
INSERT INTO Table1 (Nums) Values (3)
INSERT INTO Table1 (Nums) Values (4)
INSERT INTO Table1 (Nums) Values (5)
INSERT INTO Table1 (Nums) Values (6)
INSERT INTO Table1 (Nums) Values (7)
INSERT INTO Table1 (Nums) Values (8)
INSERT INTO Table1 (Nums) Values (9)
INSERT INTO Table1 (Nums) Values (10)

SELECT * FROM Table1

Nums
1
2
3
4
5
6
7
8
9
10
1) Write a query to obtain only even numbers.
Ans: SELECT * FROM Table1 where Nums%2=0

2) Write query to obtain multiples of 3
Ans: SELECT * FROM Table1 where Nums%3=0

3) Get only Odd numbers
Ans: SELECT * FROM Table1 where Nums%2<>0

No comments:

Post a Comment