Questions tagged [join]

A JOIN is a general operation in relational algebra for a combining operation on two relations in a relational database system. JOIN is also a keyword of the SQL language for performing analogous SQL operations.

An SQL JOIN returns rows combined from two tables and possibly satisfying a condition.

ISO/ANSI standard SQL specifies numerous JOINs.

Unconditional/Cross joins:

  • CROSS JOIN
  • comma (implicit join)

Comma returns a CROSS JOIN but has lower precedence than the keyword joins.

Conditional joins:

  • INNER JOIN
  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • FULL OUTER JOIN

Each of those has an ON version, a USING version and a NATURAL version. OUTER is an optional keyword with no effect.

CROSS JOIN returns the rows that can be made by combining a row from the left table with a row from the right table. INNER JOIN ON/USING does a CROSS JOIN then keeps only rows satisfying a condition. LEFT/RIGHT/FULL OUTER JOIN ON/USING does an INNER JOIN then via UNION ALL adds the rows got by NULL-extending the rows from the LEFT/RIGHT/both input tables that did not form an INNER JOIN row.

Specific join tags:

You can specify your question by adding extra tags:

Questions:

43295 questions
295
votes
12 answers

SQL left join vs multiple tables on FROM line?

Most SQL dialects accept both the following queries: SELECT a.foo, b.foo FROM a, b WHERE a.x = b.x SELECT a.foo, b.foo FROM a LEFT JOIN b ON a.x = b.x Now obviously when you need an outer join, the second syntax is required. But when doing an…
jmucchiello
  • 18,754
  • 7
  • 41
  • 61
287
votes
5 answers

What is the difference between a LATERAL JOIN and a subquery in PostgreSQL?

Since PostgreSQL came out with the ability to do LATERAL joins, I've been reading up on it since I currently do complex data dumps for my team with lots of inefficient subqueries that make the overall query take four minutes or more. I understand…
jdotjdot
  • 16,134
  • 13
  • 66
  • 118
269
votes
5 answers

JOIN two SELECT statement results

Is it possible to join the results of 2 sql SELECT statements in one statement? I have a database of tasks where each record is a separate task, with deadlines (and a PALT, which is just an INT of days from start to deadline. Age is also an INT…
sylverfyre
  • 3,049
  • 3
  • 17
  • 15
267
votes
4 answers

Does the join order matter in SQL?

Disregarding performance, will I get the same result from query A and B below? How about C and D? ----- Scenario 1: -- A (left join) select * from a left join b on left join c on -- B (left…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
266
votes
6 answers

SQL JOIN and different types of JOINs

What is a SQL JOIN and what are different types?
M-D
  • 10,247
  • 9
  • 32
  • 35
249
votes
14 answers

JOIN queries vs multiple queries

Are JOIN queries faster than several queries? (You run your main query, and then you run many other SELECTs based on the results from your main query) I'm asking because JOINing them would complicate A LOT the design of my application If they are…
Andreas Bonini
  • 44,018
  • 30
  • 122
  • 156
245
votes
12 answers

Difference between natural join and inner join

What is the difference between a natural join and an inner join?
smith
  • 5,341
  • 8
  • 31
  • 38
237
votes
8 answers

Are "from Table1 left join Table2" and "from Table2 right join Table1" interchangeable?

For example, there are two tables: create table Table1 (id int, Name varchar (10)) create table Table2 (id int, Name varchar (10)) Table1 data as follows: Id Name ------------- 1 A 2 B Table2 data as…
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
221
votes
5 answers

Pandas join issue: columns overlap but no suffix specified

I have the following data frames: print(df_a) mukey DI PI 0 100000 35 14 1 1000005 44 14 2 1000006 44 14 3 1000007 43 13 4 1000008 43 13 print(df_b) mukey niccdcd 0 190236 4 1 190237 6 2 190238 …
user308827
  • 21,227
  • 87
  • 254
  • 417
209
votes
6 answers

What is causing this ActiveRecord::ReadOnlyRecord error?

This follows this prior question, which was answered. I actually discovered I could remove a join from that query, so now the working query is start_cards = DeckCard.find :all, :joins => [:card], :conditions => ["deck_cards.deck_id = ? and…
user26270
  • 6,904
  • 13
  • 62
  • 94
201
votes
4 answers

Oracle "(+)" Operator

I am checking some old SQL Statements for the purpose of documenting them and probably enhancing them. The DBMS is Oracle. I did not understand a statement which read like this: select ... from a,b where a.id=b.id(+) I am confused about the (+)…
Sekhar
  • 5,614
  • 9
  • 38
  • 44
195
votes
9 answers

LEFT JOIN only first row

I read many threads about getting only the first row of a left join, but, for some reason, this does not work for me. Here is my structure (simplified of course) Feeds id | title | content ---------------------- 1 | Feed 1 | ... Artists artist_id…
KddC
  • 2,853
  • 2
  • 17
  • 19
195
votes
6 answers

Implement paging (skip / take) functionality with this query

I have been trying to understand a little bit about how to implement custom paging in SQL, for instance reading articles like this one. I have the following query, which works perfectly. But I would like to implement paging with this one. SELECT TOP…
Lars Holdgaard
  • 9,496
  • 26
  • 102
  • 182
193
votes
6 answers

Unable to create a constant value of type Only primitive types or enumeration types are supported in this context

I am getting this error for the query below Unable to create a constant value of type API.Models.PersonProtocol. Only primitive types or enumeration types are supported in this context ppCombined below is an IEnumerable object of…
user2515186
  • 2,117
  • 3
  • 15
  • 7
191
votes
11 answers

Can I use CASE statement in a JOIN condition?

The following image is a part of Microsoft SQL Server 2008 R2 System Views. From the image we can see that the relationship between sys.partitions and sys.allocation_units depends on the value of sys.allocation_units.type. So to join them together I…
Just a learner
  • 26,690
  • 50
  • 155
  • 234