## 7 replies

Last post Apr 18, 2015 11:12 AM by a2h

• Rameezwaheed

Contributor

2600 Points

1756 Posts

### Simple Query idea is required

Hi all,

I have age stored in table as an int  Now i have to select all those records based upon the age limits like 30 - 35 and 40-45 etc

Now my question is either i have to use the greater (>) then or < less then operators or i may use the between clause.

Any Idea?

Regards,

life is name of learning!
Mark as an answer if it helps
• A2H

All-Star

50731 Points

9873 Posts

### Re: Simple Query idea is required

IMHO your best option is to use the Between when you select records based on ranges.

You can try with the below code

``` Select *
From   yourtablename
Where Age Between 30 And 35```

Thanks,
Aje
My Blog | Dotnet Funda
• Rajneesh Ver...

All-Star

28988 Points

7251 Posts

### Re: Simple Query idea is required

Apr 18, 2015 02:08 AM|Rajneesh Verma|LINK

#### Rameezwaheed

Any Idea?

Use between:

```SELECT SUM(CASE WHEN age_c < 18 THEN 1 ELSE 0 END) AS [Under 18],
SUM(CASE WHEN age_c BETWEEN 18 AND 24 THEN 1 ELSE 0 END) AS [18-24],
SUM(CASE WHEN age_c BETWEEN 25 AND 34 THEN 1 ELSE 0 END) AS [25-34]
FROM contacts```
• wmec

Contributor

6392 Points

5689 Posts

### Re: Simple Query idea is required

Try

`... [age] <=35 and [age] >=30 or [age] <=45 and [age] >=40`

Many Thanks & Best Regards,
HuaMin Chen
• Rameezwaheed

Contributor

2600 Points

1756 Posts

### Re: Simple Query idea is required

#### a2h

You can try with the below code

``` Select *
From   yourtablename
Where Age Between 30 And 35```

Thanks for reply when i tried with between then it is not displaying the records whose age is 30  or 35 but between shows the records between 30 to 35. i also need those records whose age is 30 and 35.

Regards

life is name of learning!
Mark as an answer if it helps
• oned_gk

All-Star

52232 Points

15534 Posts

### Re: Simple Query idea is required

BETWEEN 30 AND 35 results including 30 and 35 value

Results

30

31

32

33

34

35

Programming to simplify, don't look for difficult way
• A2H

All-Star

50731 Points

9873 Posts

### Re: Simple Query idea is required

#### Rameezwaheed

Thanks for reply when i tried with between then it is not displaying the records whose age is 30  or 35 but between shows the records between 30 to 35. i also need those records whose age is 30 and 35.

Between includes the start and Endvalue also in result set. You can see the below code in action which I tried and its return proper result set.

```CREATE TABLE ExampleTable
(
Id int,
Age int,
Name Varchar(10)
);

INSERT INTO ExampleTable
(Id, Age, Name)
VALUES
(1,25,'Name1'),
(2,32,'Name2'),
(3,34,'Name3'),
(4,28,'Name4'),
(5,30,'Name5'),
(6,35,'Name6')```

Sample Query:

```Select *
From   ExampleTable
Where Age Between 30 And 35 order by age asc```
Thanks,
Aje
My Blog | Dotnet Funda
• A2H

All-Star

50731 Points

9873 Posts

### Re: Simple Query idea is required

```Select *