Questions tagged [table-alias]
65 questions
36
votes
17 answers
When to use SQL Table Alias
I'm curious to know how people are using table aliases. The other developers where I work always use table aliases, and always use the alias of a, b, c, etc.
Here's an example:
SELECT a.TripNum, b.SegmentNum, b.StopNum, b.ArrivalTime
FROM Trip a,…

Rossini
- 5,960
- 4
- 29
- 32
28
votes
7 answers
Why is selecting specified columns, and all, wrong in Oracle SQL?
Say I have a select statement that goes..
select * from animals
That gives a a query result of all the columns in the table.
Now, if the 42nd column of the table animals is is_parent, and I want to return that in my results, just after gender, so I…

glasnt
- 2,865
- 5
- 35
- 55
21
votes
4 answers
Is there a way to give a subquery an alias in Oracle 11g SQL?
Is there a way to give a subquery in Oracle 11g an alias like:
select *
from
(select client_ref_id, request from some_table where message_type = 1) abc,
(select client_ref_id, response from some_table where message_type = 2) defg
where
…

Matt Pascoe
- 8,651
- 17
- 42
- 48
21
votes
2 answers
How to use the 'as' keyword to alias a table in Oracle?
I'm trying to execute this query in Oracle SQL Developer:
SELECT G.Guest_ID, G.First_Name, G.Last_Name
FROM Guest AS G
JOIN Stay AS S ON G.Guest_ID = S.Guest_ID
WHERE G.City = 'Miami' AND S.Room = '222';
However, I get the following…

Marcos
- 527
- 1
- 5
- 12
12
votes
2 answers
oracle: can you assign an alias to the from clause?
can you assign an alias to the from clause? like:
select a - b "Markup" from retail a, cost b;
EDIT: sorry i typed that out a bit too quick and tried to simplify the question to the point where it didnt make any sense
What im actually trying to do…

Sinaesthetic
- 11,426
- 28
- 107
- 176
12
votes
3 answers
How to use Arel::Nodes::TableAlias in an initial where statement
I got stuck on this and for sure it's easy, but I just cannot find the solution in the docs.
I have some tree structure and the child where clause that I have to filter with an "exists" sub…

Micha
- 302
- 3
- 12
9
votes
2 answers
SQL Command not properly ended?
I am using a SQL statement with a Temporary relation, and am getting the error ORA-009933: SQL command not properly ended
I don't see anything wrong with the statement, so any assistance is greatly appreciated. The statement is:
SELECT Temp.name,
…

Paul Woidke
- 928
- 4
- 18
- 40
8
votes
6 answers
When is it required to give a table name an alias in SQL?
I noticed when doing a query with multiple JOINs that my query didn't work unless I gave one of the table names an alias.
Here's a simple example to explain the point:
This doesn't work:
SELECT subject
from items
join purchases on…

tim peterson
- 23,653
- 59
- 177
- 299
6
votes
2 answers
Update SQL with Aliased tables still returns "table is ambiguous" error
I am trying to run the below update but running into the "table is ambiguous" error.
UPDATE dbo.cg
SET cg.column = gId.ID
FROM dbo.a
INNER JOIN dbo.cg as cId ON cId.[a] = dbo.a.[c]
INNER JOIN dbo.cg as gId ON gId.[a] =…

K7Buoy
- 936
- 3
- 13
- 22
6
votes
2 answers
Yii2 Not unique table/alias: 'user'
In the "Ticket" model:
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id']);
}
public function getSupervisor()
{
return $this->hasOne(User::className(), ['id' => 'supervisor_id']);
In the "TicketSearch"…

Jkr
- 67
- 1
- 7
5
votes
1 answer
MySQL: Possible to have wildcards in AS aliases?
I have a bunch of fields named the same across multiple tables (I inherited it - don't blame me ;).
Instead of setting up all of the aliases verbosely, is it possible to assign/prepend an alias automatically by way of a wildcard?
I'm envisioning…

k00k
- 17,314
- 13
- 59
- 86
4
votes
2 answers
What does this "t" do?
I'm having trouble understanding the following example from the PostgreSQL documentation:
-- set returning function WITH ORDINALITY
SELECT * FROM pg_ls_dir('.') WITH ORDINALITY AS t(ls,n);
ls | n
-----------------+----
pg_serial …

Thomas
- 174,939
- 50
- 355
- 478
4
votes
4 answers
MySQL INSERT with table alias
I happen to have two columns having the same name as two SQL reserved words, Key and Value.
When using the SELECT statement I can create a table alias and solve it that way.
Now I'm trying to INSERT data and it seems like you can't create table…

Max Kielland
- 5,627
- 9
- 60
- 95
4
votes
3 answers
Select SQL View Slow with table alias
I am baffled as to why selecting my SQL View is so slow when using a table alias (25 seconds) but runs so much faster when the alias is removed (2 seconds)
-this query takes 25 seconds.
SELECT [Extent1].[Id] AS [Id],
…

James Dev
- 2,979
- 1
- 11
- 16
4
votes
5 answers
SQL - table alias scope
I learned to use "exists" instead of "in".
BAD
select * from table where nameid in (
select nameid from othertable where otherdesc = 'SomeDesc' )
GOOD
select * from table t where exists (
select nameid from othertable o where t.nameid…

OscarRyz
- 196,001
- 113
- 385
- 569