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
5190
votes
28 answers

What is the difference between "INNER JOIN" and "OUTER JOIN"?

Also, how do LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN fit in?
Chris de Vries
  • 56,777
  • 5
  • 32
  • 27
2143
votes
2 answers

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN?

What's the difference between INNER JOIN, LEFT JOIN, RIGHT JOIN and FULL JOIN in MySQL?
Lion King
  • 32,851
  • 25
  • 81
  • 143
1391
votes
6 answers

Difference between JOIN and INNER JOIN

Both these joins will give me the same results: SELECT * FROM table JOIN otherTable ON table.ID = otherTable.FK vs SELECT * FROM table INNER JOIN otherTable ON table.ID = otherTable.FK Is there any difference between the statements in performance…
driis
  • 161,458
  • 45
  • 265
  • 341
1196
votes
29 answers

SQL Update from One Table to Another Based on a ID Match

I have a database with account numbers and card numbers. I match these to a file to update any card numbers to the account number so that I am only working with account numbers. I created a view linking the table to the account/card database to…
Boerseun
1105
votes
12 answers

INNER JOIN ON vs WHERE clause

For simplicity, assume all relevant fields are NOT NULL. You can do: SELECT table1.this, table2.that, table2.somethingelse FROM table1, table2 WHERE table1.foreignkey = table2.primarykey AND (some other conditions) Or else: SELECT …
JCCyC
  • 16,140
  • 11
  • 48
  • 75
1085
votes
20 answers

Join vs. sub-query

I am an old-school MySQL user and have always preferred JOIN over sub-query. But nowadays everyone uses sub-query, and I hate it; I don't know why. I lack the theoretical knowledge to judge for myself if there is any difference. Is a sub-query as…
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
961
votes
13 answers

Update a table using JOIN in SQL Server?

I want to update a column in a table making a join on other table e.g.: UPDATE table1 a INNER JOIN table2 b ON a.commonfield = b.[common field] SET a.CalculatedColumn= b.[Calculated Column] WHERE b.[common field]= a.commonfield AND a.BatchNO…
Manjot
  • 11,166
  • 9
  • 38
  • 49
930
votes
22 answers

SQL JOIN - WHERE clause vs. ON clause

After reading it, this is not a duplicate of Explicit vs Implicit SQL Joins. The answer may be related (or even the same) but the question is different. What is the difference and what should go in each? If I understand the theory correctly, the…
BCS
  • 75,627
  • 68
  • 187
  • 294
900
votes
8 answers

Pandas Merging 101

How can I perform a (INNER| (LEFT|RIGHT|FULL) OUTER) JOIN with pandas? How do I add NaNs for missing rows after a merge? How do I get rid of NaNs after merging? Can I merge on the index? How do I merge multiple DataFrames? Cross join with…
cs95
  • 379,657
  • 97
  • 704
  • 746
859
votes
15 answers

How can I do a FULL OUTER JOIN in MySQL?

I want to do a full outer join in MySQL. Is this possible? Is a full outer join supported by MySQL?
Spencer
  • 21,348
  • 34
  • 85
  • 121
652
votes
24 answers

LEFT OUTER JOIN in LINQ

How to perform left outer join in C# LINQ to objects without using join-on-equals-into clauses? Is there any way to do that with where clause? Correct problem: For inner join is easy and I have a solution like this List innerFinal = (from…
Toy
  • 6,573
  • 3
  • 16
  • 4
628
votes
19 answers

How do I perform the SQL Join equivalent in MongoDB?

How do I perform the SQL Join equivalent in MongoDB? For example say you have two collections (users and comments) and I want to pull all the comments with pid=444 along with the user info for each. comments { uid:12345, pid:444, comment="blah"…
The Unknown
  • 19,224
  • 29
  • 77
  • 93
556
votes
10 answers

Join/Where with LINQ and Lambda

I'm having trouble with a query written in LINQ and Lambda. So far, I'm getting a lot of errors here's my code: int id = 1; var query = database.Posts.Join(database.Post_Metas, post => database.Posts.Where(x => x.ID…
David
  • 5,579
  • 3
  • 16
  • 4
536
votes
12 answers

T-SQL: Selecting rows to delete via joins

Scenario: Let's say I have two tables, TableA and TableB. TableB's primary key is a single column (BId), and is a foreign key column in TableA. In my situation, I want to remove all rows in TableA that are linked with specific rows in TableB: Can I…
John
  • 17,163
  • 16
  • 65
  • 83
494
votes
7 answers

How can I do three table JOINs in an UPDATE query?

I asked a question and got this reply which helped. UPDATE TABLE_A a JOIN TABLE_B b ON a.join_col = b.join_col AND a.column_a = b.column_b SET a.column_c = a.column_c + 1 Now I am looking to do this if there are three tables involved…
Ricky
  • 5,201
  • 4
  • 19
  • 22
1
2 3
99 100