Innkeeper
Innkeeper

Reputation: 673

Are there any differences in the following sql statements?

SELECT * FROM table t
SELECT t.* FROM table t

I tried it and it yielded the same results, but I want to make sure because I'm refactoring a piece of code that uses the second version, and I was surprised as it is both longer to write, and less simple.

Are there any hidden stuff here?

MySQL version: 5.5.29-0ubuntu0.12.04.2 (Ubuntu)

Upvotes: 0

Views: 64

Answers (5)

juergen d
juergen d

Reputation: 204756

Both statements are the same in your case.

They would be not if you join multiple tables in one query.

select * 

selects all columns.

select t.*

select all columns of table t (or the table assigned the alias t)

Upvotes: 6

Deepshikha
Deepshikha

Reputation: 10264

Both the statements will give same results until it's combined with another table with some table operator as Join, Apply where you will need to uniquely identify columns( more specifically ambiguous columns ) from this table.

As a best practice you should use column names instead of using select * as it makes code more readable and front end code doesn't break in case table structure gets changed at any point of time.

Upvotes: 2

user2891084
user2891084

Reputation: 111

SELECT * FROM table t and SELECT t.* FROM table t

Return the whole table

SELECT t.* FROM table as t inner join table2 as t2 will only return the fields in the "table" table while SELECT * FROM table as t inner join table2 as t2 will return the fields of table and table2

Upvotes: 2

David M
David M

Reputation: 72860

SELECT * will return all columns from all tables in the query. SELECT t.* will return all columns from the table named, or aliased as, t. The same in your example because there's only one table involved.

Upvotes: 1

Tarik
Tarik

Reputation: 11209

The statements are identical. All you have is an alias for table "table" called "t".

Upvotes: 1

Related Questions