## 7 replies

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

Contributor

2564 Points

1683 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

All-Star

49391 Points

9520 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,
A2H
My Blog | Dotnet Funda

All-Star

28667 Points

7172 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```

Contributor

6338 Points

5525 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

Contributor

2564 Points

1683 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

All-Star

50891 Points

14972 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 hardway ...

All-Star

49391 Points

9520 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,
A2H
My Blog | Dotnet Funda

All-Star

49391 Points

9520 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.

If you still have trouble with using Between then you can try with the below code also

```Select *
From   ExampleTable
Where Age >= 30 and  Age <= 35 order by age asc```
Thanks,
A2H
My Blog | Dotnet Funda