zarathustra
zarathustra

Reputation: 2080

Dot notation in psql query - what is the difference?

In some Rails code I found these lines:

User.where('created_at >= ?', '2018-10-10').count
# which results in:
SELECT COUNT(*) FROM "users" WHERE (created_at >= '2018-10-10')
=> 1234
User.where('users.created_at >= ?', '2018-10-10').count
# which results in:
SELECT COUNT(*) FROM "users" WHERE (users.created_at >= '2018-10-10')
=> 1234

Is there any difference between using WHERE users.created_at and WHERE created_at ?

Upvotes: 1

Views: 773

Answers (1)

Bill Karwin
Bill Karwin

Reputation: 562310

There's no difference in this case.

The dotted notation you're seeing is called qualified column names.

There's no advantage to using qualified column names in your example query, since you have only one table. It's clear which table the created_at column belongs to.

But if you had a more complex query with JOIN and so on, it would be a good idea to qualify each column with the table they belong to, in case more than one table has a column of the same name.

Upvotes: 2

Related Questions