Is it possible in SQL to return a first row that has attribute names and then values

profile for Nikola at Stack Overflow, Q&A for professional and enthusiast programmers
I’m a big fan of Stack Overflow and I tend to contribute regularly (am currently in the top 0.X%). In this category (stackoverflow) of posts I will will be posting my top rated questions and answers. This, btw, is allowed as explained in the meta thread here.

My question was:

I am wondering if there is a way to write the SQL so that it would return me a result as usual but now, on the first row that would return also the attribute names.

To explain what I mean:

say you have a table “test” which has 2 attributes “id” and “name”:

id name
1  nik
2  tst

query:

SELECT*FROM test;

produces:

1 nik
2 tst

but what I want it to return is this:

id name
1 nik
2 tst

Is this possible?

edit: I am using PostreSQL

 The answer, by a_horse_with_no_name, was:

You cannot return the names and the actual column values in a single result unless you give up on the real datatypes (which is probably not what you want).

Your example mixes character data and numeric data in the id column and Postgres will (rightfully) refuse to return such a result set.

Edit:
I tested the “union” solution given e.g. by JNK and it fails (as expected) on Postgres, Oracle and SQL Server precisely because of the non-matching datatypes. MySQL follows it’s usual habits of not throwing errors and simply converts everything to characters.

Written by Nikola Brežnjak