2

I am doing a query in sql to find rows with distinct value of name as below:

select distinct name, age, sex from person

it works but I don't want to show the name column in the result set. Is there a way to hide this column?

EDIT1 the reason I put distinct name there is to avoid multiple rows with the same name returned. My table has person with the same name but different age and sex. So I want to make the result distinct in name but don't show the name.

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523

3 Answers3

3

You could try something like this.

select age, sex from (
   select distinct name, age, sex from person);

I'm presuming you might have people with the same age and sex but a different name.

Otherwise just remove the name

kev670
  • 810
  • 2
  • 18
  • 37
1

Here is my solution (sql server 2016):

create table person (age varchar(20), [name] varchar(20), gender varchar(20))
go
insert into person values ('20', 'joe', 'm')
insert into person values ('19', 'tom', 'm')
insert into person values ('25', 'sally', 'f')
insert into person values ('28', 'Tammy', 'f')
go

select age, gender from (select distinct name, age, gender from person) t
James Bailey
  • 199
  • 13
1

You have to use your query as a sub query here. From your query again select age and sex alone.

select age, sex from (select distinct name, age, sex from person) As x
Santhosh
  • 729
  • 7
  • 19