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
487
votes
12 answers

Explicit vs implicit SQL joins

Is there any efficiency difference in an explicit vs implicit inner join? For example: SELECT * FROM table a INNER JOIN table b ON a.id = b.id; vs. SELECT a.*, b.* FROM table a, table b WHERE a.id = b.id;
dmanxiii
  • 51,473
  • 10
  • 33
  • 23
480
votes
18 answers

What is the syntax for an inner join in LINQ to SQL?

I'm writing a LINQ to SQL statement, and I'm after the standard syntax for a normal inner join with an ON clause in C#. How do you represent the following in LINQ to SQL: select DealerContact.* from Dealer inner join DealerContact on…
Glenn Slaven
  • 33,720
  • 26
  • 113
  • 165
465
votes
13 answers

SQL join: selecting the last records in a one-to-many relationship

Suppose I have a table of customers and a table of purchases. Each purchase belongs to one customer. I want to get a list of all customers along with their last purchase in one SELECT statement. What is the best practice? Any advice on building…
netvope
  • 7,647
  • 7
  • 32
  • 42
405
votes
7 answers

When and why are database joins expensive?

I'm doing some research into databases and I'm looking at some limitations of relational DBs. I'm getting that joins of large tables is very expensive, but I'm not completely sure why. What does the DBMS need to do to execute a join operation,…
Rik
  • 28,507
  • 14
  • 48
  • 67
390
votes
4 answers

MySQL Select all columns from one table and some from another table

How do you select all the columns from one table and just some columns from another table using JOIN? In MySQL.
Alex
  • 5,565
  • 6
  • 36
  • 57
380
votes
12 answers

SQL Inner-join with 3 tables?

I'm trying to join 3 tables in a view; here is the situation: I have a table that contains information of students who are applying to live on this College Campus. I have another table that lists the Hall Preferences (3 of them) for each Student.…
Bob Sanders
  • 4,317
  • 4
  • 18
  • 11
370
votes
8 answers

Rails :include vs. :joins

This is more of a "why do things work this way" question rather than a "I don't know how to do this" question... So the gospel on pulling associated records that you know you're going to use is to use :include because you'll get a join and avoid a…
Rob Cameron
  • 9,674
  • 7
  • 39
  • 42
351
votes
6 answers

MySQL JOIN ON vs USING?

In a MySQL JOIN, what is the difference between ON and USING()? As far as I can tell, USING() is just more convenient syntax, whereas ON allows a little more flexibility when the column names are not identical. However, that difference is so minor,…
Nathanael
  • 6,893
  • 5
  • 33
  • 54
349
votes
6 answers

pandas: merge (join) two data frames on multiple columns

I am trying to join two pandas dataframes using two columns: new_df = pd.merge(A_df, B_df, how='left', left_on='[A_c1,c2]', right_on = '[B_c1,c2]') but got the following error: pandas/index.pyx in pandas.index.IndexEngine.get_loc…
Edamame
  • 23,718
  • 73
  • 186
  • 320
326
votes
14 answers

What is the difference between JOIN and UNION?

What is the difference between JOIN and UNION? Can I have an example?
Gold
  • 60,526
  • 100
  • 215
  • 315
319
votes
27 answers

SQL select join: is it possible to prefix all columns as 'prefix.*'?

I'm wondering if this is possible in SQL. Say you have two tables A and B, and you do a select on table A and join on table B: SELECT a.*, b.* FROM TABLE_A a JOIN TABLE_B b USING (some_id); If table A has columns 'a_id', 'name', and 'some_id', and…
foxdonut
  • 7,779
  • 7
  • 34
  • 33
310
votes
12 answers

pandas three-way joining multiple dataframes on columns

I have 3 CSV files. Each has the first column as the (string) names of people, while all the other columns in each dataframe are attributes of that person. How can I "join" together all three CSV documents to create a single CSV with each row…
lollercoaster
  • 15,969
  • 35
  • 115
  • 173
301
votes
13 answers

How to do joins in LINQ on multiple fields in single join

I need to do a LINQ2DataSet query that does a join on more than one field (as var result = from x in entity join y in entity2 on x.field1 = y.field1 and x.field2 = y.field2 I have yet found a suitable solution (I can add the…
johnc
  • 39,385
  • 37
  • 101
  • 139
300
votes
7 answers

What is the difference between join and merge in Pandas?

Suppose I have two DataFrames like so: left = pd.DataFrame({'key1': ['foo', 'bar'], 'lval': [1, 2]}) right = pd.DataFrame({'key2': ['foo', 'bar'], 'rval': [4, 5]}) I want to merge them, so I try something like this: pd.merge(left, right,…
munk
  • 12,340
  • 8
  • 51
  • 71
296
votes
6 answers

What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate

Please help me understand where to use a regular JOIN and where a JOIN FETCH. For example, if we have these two queries FROM Employee emp JOIN emp.department dep and FROM Employee emp JOIN FETCH emp.department dep Is there any difference between…
abbas
  • 6,453
  • 2
  • 40
  • 36