Johnny Cash
Johnny Cash

Reputation: 5147

difference between select * and select table name

This is the basic question about sql statements.

What is the difference between

SELECT * FROM "Users"

and

SELECT "Users".* FROM "Users"

Upvotes: 1

Views: 10708

Answers (5)

Jon Crowell
Jon Crowell

Reputation: 22338

For the examples you provided, the only difference is in syntax. What both of the queries share is that they are really bad. Select * is evil no matter how you write it and can get you into all kinds of trouble. Get into the habit of listing the columns you want to have included in your result set.

Upvotes: 0

pinkpanther
pinkpanther

Reputation: 4808

In the case of example given by you, there is no difference between them when it comes to semantics.When it comes to performance it might be too little... just parsing two different length strings....

But, it is only true for the example given by you. Where as in queries where multiple tables are involved tableName.* disambiguate the table from which table we want to select all columns.

Example: If you have two tables TableA and TableB. Let's suppose that they have column with same names that is Name. If you want to specify from which table you want to select Name column. Table-name qualifier helps.

`select TableA.Name, TableB.Name where TableA.age=TableB.age`

That's all I can say.

Upvotes: 2

Andriy M
Andriy M

Reputation: 77687

The particular examples specified would return the same result and have the same performance. There would be no difference in that respect, therefore.

However, in some SQL products, difference in interpreting * and alias.* has effect, in particular, on what else you can add to the query. More specifically, in Oracle, you can mix an alias.* with other expressions being returned as columns, i.e. this

SELECT "Users".*, SomeColumn * 2 AS DoubleValue FROM "Users"

would work. At the same time, * must stand on its own, meaning that the following

SELECT *, SomeColumn * 2 AS DoubleValue FROM "Users"

would be illegal.

Upvotes: 0

user4035
user4035

Reputation: 23729

In your case there is no difference. It emerges, when you are selecting from multiple tables. * takes data from all the tables, TABLE_NAME.* - all the data from this table. Suppose, we have a database with 2 tables:

mysql> SELECT * FROM report;
+----+------------+
| id | date       |
+----+------------+
|  1 | 2013-05-01 |
|  2 | 2013-06-02 |
+----+------------+

mysql> SELECT * FROM sites_to_report;
+---------+-----------+---------------------+------+
| site_id | report_id | last_run            | rows |
+---------+-----------+---------------------+------+
|       1 |         1 | 2013-05-01 16:20:21 |    1 |
|       1 |         2 | 2013-05-03 16:20:21 |    1 |
|       2 |         2 | 2013-05-03 14:21:47 |    1 |
+---------+-----------+---------------------+------+

mysql> SELECT
    ->  *
    -> FROM
    ->  report
    -> INNER JOIN
    ->  sites_to_report
    -> ON
    ->  sites_to_report.report_id=report.id;
+----+------------+---------+-----------+---------------------+------+
| id | date       | site_id | report_id | last_run            | rows |
+----+------------+---------+-----------+---------------------+------+
|  1 | 2013-05-01 |       1 |         1 | 2013-05-01 16:20:21 |    1 |
|  2 | 2013-06-02 |       1 |         2 | 2013-05-03 16:20:21 |    1 |
|  2 | 2013-06-02 |       2 |         2 | 2013-05-03 14:21:47 |    1 |
+----+------------+---------+-----------+---------------------+------+

mysql> SELECT
    ->  report.*
    -> FROM
    ->  report
    -> INNER JOIN
    ->  sites_to_report
    -> ON
    ->  sites_to_report.report_id=report.id;
+----+------------+
| id | date       |
+----+------------+
|  1 | 2013-05-01 |
|  2 | 2013-06-02 |
|  2 | 2013-06-02 |
+----+------------+

Upvotes: 3

Web Develop Wolf
Web Develop Wolf

Reputation: 6326

[TableName].[column] is usually used to pinpoint the table you wish to use when two tables a present in a join or a complex statement and you want to define which column to use out of the two with the same name.

It's most common use is in a join though, for a basic statement such as the one above there is no difference and the output will be the same.

Upvotes: 9

Related Questions