patternsqlModerate
Display column title as in the query without 'AS' statement
Viewed 0 times
withoutthecolumnstatementquerytitledisplay
Problem
I query data from multiple tables using JOIN statements like this :
When I get the answer, in psql for example, I've got this kind of rows title :
Instead, I'd like to get the original and precise notation :
For now I have to use
Is there a way to get this result with a SQL keyword or with a DBMS parameter?
I searched but wasn't able to find anything on this specific problem.
SELECT u.id,u.name,d.id,d.name FROM user u RIGHT JOIN dog d ON... ;When I get the answer, in psql for example, I've got this kind of rows title :
| id | name | id | name |
+-------+-------+------+--------+Instead, I'd like to get the original and precise notation :
| u.id | u.name | d.id | d.name |
+---------+---------+--------+----------+For now I have to use
AS statements, but it's quite repetitive to re-write and it add length to the query and makes it less readable.Is there a way to get this result with a SQL keyword or with a DBMS parameter?
I searched but wasn't able to find anything on this specific problem.
Solution
There is no built-in method for modifying the column names displayed in the results of a query. This is by-design.
Use the
Alternatively, you could rename all the columns, prefixing them with a different prefix for each table, or making them unique more descriptively. For example:
Be aware that modifying the column names after you've implemented your database into production will result in a vast amount of work ensuring you've changed all affected code both inside and outside the database. On the other hand, renaming generic names like
As an aside, the practice of using
Pretty clearly, the above query should be written as:
If you do this instead, it becomes far clearer from the outset what is intended:
Added bonus; the output now shows somewhat more explicit column names:
╔═══════╦═══════╦═══════╦═══════╦═══════╦═══════╗
║ t1_id ║ t2_id ║ t1_id ║ t3_id ║ t1_id ║ t2_id ║
╚═══════╩═══════╩═══════╩═══════╩═══════╩═══════╝
And as you are using PostgreSQL, you can also benefit from making the join conditions more compact by using the
Not only do your queries become arguably more readable this way, there is also effect on the output: the join columns are not repeated. The above statement would produce output like this:
╔═══════╦═══════╦═══════╗
║ t1_id ║ t2_id ║ t3_id ║
╚═══════╩═══════╩═══════╝
Use the
AS construct to control the column-names. Format your query for readability, as in:SELECT u.id AS "u.id"
, u.name AS "u.name"
, d.id AS "d.id"
, d.name AS "d.name"
FROM user u
RIGHT JOIN dog d ON...;Alternatively, you could rename all the columns, prefixing them with a different prefix for each table, or making them unique more descriptively. For example:
dog_id, dog_name, user_id, user_name. This would work for almost all cases (except self-joins).Be aware that modifying the column names after you've implemented your database into production will result in a vast amount of work ensuring you've changed all affected code both inside and outside the database. On the other hand, renaming generic names like
id or name would certainly be beneficial in the long run. I would expect the benefits to outweigh the trouble of going through code to fix the names after the renaming.As an aside, the practice of using
id as the name of the surrogate key for every table results in a spaghetti mess of unreadable and unintelligible code that is error prone. Take for example:CREATE TABLE T1
(
id int PRIMARY KEY
);
CREATE TABLE T2
(
id int PRIMARY KEY
, t1_id int
);
CREATE TABLE T3
(
id int PRIMARY KEY
, t1_id int
, t2_id int
);
SELECT *
FROM T1
INNER JOIN T2 ON t1.id = t2.id
INNER JOIN T3 ON t1.id = t2.id AND t2.id = t3.id
WHERE t2.id = 1;Pretty clearly, the above query should be written as:
SELECT *
FROM T1
INNER JOIN T2 ON t1.id = t2.t1_id
INNER JOIN T3 ON t1.id = t2.t1_id AND t2.id = t3.t2_id
WHERE t2.id = 1;If you do this instead, it becomes far clearer from the outset what is intended:
CREATE TABLE T1
(
t1_id int PRIMARY KEY
);
CREATE TABLE T2
(
t2_id int PRIMARY KEY
, t1_id int
);
CREATE TABLE T3
(
t3_id int PRIMARY KEY
, t1_id int
, t2_id int
);
SELECT *
FROM T1
INNER JOIN T2 ON t1.t1_id = t2.t1_id
INNER JOIN T3 ON t1.t1_id = t2.t1_id AND t2.t2_id = t3.t2_id
WHERE t2.t2_id = 1;Added bonus; the output now shows somewhat more explicit column names:
╔═══════╦═══════╦═══════╦═══════╦═══════╦═══════╗
║ t1_id ║ t2_id ║ t1_id ║ t3_id ║ t1_id ║ t2_id ║
╚═══════╩═══════╩═══════╩═══════╩═══════╩═══════╝
And as you are using PostgreSQL, you can also benefit from making the join conditions more compact by using the
USING clause:SELECT *
FROM T1
INNER JOIN T2 USING (t1_id)
INNER JOIN T3 USING (t1_id, t2_id)
WHERE t2.t2_id = 1;Not only do your queries become arguably more readable this way, there is also effect on the output: the join columns are not repeated. The above statement would produce output like this:
╔═══════╦═══════╦═══════╗
║ t1_id ║ t2_id ║ t3_id ║
╚═══════╩═══════╩═══════╝
Code Snippets
SELECT u.id AS "u.id"
, u.name AS "u.name"
, d.id AS "d.id"
, d.name AS "d.name"
FROM user u
RIGHT JOIN dog d ON...;CREATE TABLE T1
(
id int PRIMARY KEY
);
CREATE TABLE T2
(
id int PRIMARY KEY
, t1_id int
);
CREATE TABLE T3
(
id int PRIMARY KEY
, t1_id int
, t2_id int
);
SELECT *
FROM T1
INNER JOIN T2 ON t1.id = t2.id
INNER JOIN T3 ON t1.id = t2.id AND t2.id = t3.id
WHERE t2.id = 1;SELECT *
FROM T1
INNER JOIN T2 ON t1.id = t2.t1_id
INNER JOIN T3 ON t1.id = t2.t1_id AND t2.id = t3.t2_id
WHERE t2.id = 1;CREATE TABLE T1
(
t1_id int PRIMARY KEY
);
CREATE TABLE T2
(
t2_id int PRIMARY KEY
, t1_id int
);
CREATE TABLE T3
(
t3_id int PRIMARY KEY
, t1_id int
, t2_id int
);
SELECT *
FROM T1
INNER JOIN T2 ON t1.t1_id = t2.t1_id
INNER JOIN T3 ON t1.t1_id = t2.t1_id AND t2.t2_id = t3.t2_id
WHERE t2.t2_id = 1;SELECT *
FROM T1
INNER JOIN T2 USING (t1_id)
INNER JOIN T3 USING (t1_id, t2_id)
WHERE t2.t2_id = 1;Context
StackExchange Database Administrators Q#199214, answer score: 11
Revisions (0)
No revisions yet.