SQLite Forum

Colons in column names?
Login
The column names in the first query are both "id" because by default the alias-name for a value that derives from \<table\>.\<column\> is \<column\>.  If the value is an expression, then the default name is the expression.

You can specify an explicit aliasname with the AS keyword.  Aliasnames do not have to be unique (but if they are not unique then you cannot uniquely identify a column by its aliasname).

When you use a "subquery" (as in the second example), the result columns must have unique aliasnames otherwise you could not refer to the column unambiguously in the outer query.  So the aliasnames are "uniquificated" from left to right by appending :n to duplicates.  That is, if a subquery contains 3 fields with the aliasname "id", then they will be called, from left to right, "id", "id:1", "id:2".  If this were not the case then the outer query would not be able to access *any* of the "id" columns by name because the reference would be ambiguous.

This is the case even if you use specified duplicated aliasnames in the query:

```
sqlite> select t1.id as x, t2.id as x from t1 join t2 on t1.id = t2.t1Id;
x           x
----------  ----------
0           1
sqlite> select * from (select t1.id as x, t2.id as x from t1 join t2 on t1.id = t2.t1Id);
x           x:1
----------  ----------
0           1
```

The solution is to may sure you use unambiguous aliasnames or make sure that all column names are unique.  This is why some people love to prefix the column names of a table with the table name -- it is not because they like the extra typing, it is because of badly designed tools (such as your framework) that make assumptions that are not manifest.