Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does keyword USING work in PostgreSQL?

Tags:

postgresql

I am confused with the USING keyword which is used to join two tables in postgres. I first saw it in another SO post Compare two tables in postgres. I checked the manual for postgres 2.6. Joins Between Tables.
I can't understand how postgres identified the user_id to be joined (in the SO post)

like image 744
Nandakumar V Avatar asked May 27 '15 07:05

Nandakumar V


People also ask

Do keywords Postgres?

Description. DO executes an anonymous code block, or in other words a transient anonymous function in a procedural language. The code block is treated as though it were the body of a function with no parameters, returning void . It is parsed and executed a single time.

Is type a keyword in PostgreSQL?

The most natural column name is typically type , but I try to avoid using SQL keywords or reserved words in my naming. I'm aware that type is a non-reserved keyword in both MySQL and Postgres, so I can use it, but should I? What is current best practice around using type as a column name?

Why we use $$ in PostgreSQL?

In PostgreSQL, the dollar-quoted string constants ($$) is used in user-defined functions and stored procedures. In PostgreSQL, you use single quotes for a string constant like this: select 'String constant'; When a string constant contains a single quote ('), you need to escape it by doubling up the single quote.

How does Postgres CTE work?

In PostgreSQL, the CTE(Common Table Expression) is used as a temporary result set that the user can reference within another SQL statement like SELECT, INSERT, UPDATE or DELETE. CTEs are temporary in the sense that they only exist during the execution of the query.


1 Answers

As the PostgreSQL documentation states:

The USING clause is a shorthand that allows you to take advantage of the specific situation where both sides of the join use the same name for the joining column(s). It takes a comma-separated list of the shared column names and forms a join condition that includes an equality comparison for each one. For example, joining T1 and T2 with USING (a, b) produces the join condition ON T1.a = T2.a AND T1.b = T2.b.

So simply said, in this case both tables have the column user_id and the join is done based on them.

like image 148
Sami Kuhmonen Avatar answered Oct 27 '22 01:10

Sami Kuhmonen