Questions tagged [inner-join]

A database operation that combines the values of 2 tables based on a condition, or relationship, that exists between those tables.

An inner join is the most common join operation used in applications and can be regarded as the default join-type. Inner join creates a new result table by combining column values of two tables (A and B) based upon the join-predicate. The query compares each row of A with each row of B to find all pairs of rows which satisfy the join-predicate.

When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. The result of the join can be defined as the outcome of first taking the Cartesian product (or Cross join) of all records in the tables (combining every record in table A with every record in table B) then returning all records which satisfy the join predicate.

Actual SQL implementations normally use other approaches like a hash join or a sort-merge join where possible, since computing the Cartesian product is very inefficient.

enter image description here


Resources :

6652 questions
12
votes
3 answers

SQL JOIN using a mapping table

I have three tables: COLLECTION PERSON PERSON_COLLECTION where PERSON_COLLECTION is a mapping table id|person_id|collection_id I now want to select all entries in collection and order them by person.name. Do I have to join the separate tables…
user871784
  • 1,247
  • 4
  • 13
  • 32
11
votes
2 answers

How to do joins on subqueries in AREL within Rails

I have a simple model class User has_many :logs class Logs related in the usual way through the foreign key logs.user_id. I'm trying to do the following using Arel and according to the Arel doc it should work. u_t = Arel::Table::new…
bradgonesurfing
  • 30,949
  • 17
  • 114
  • 217
11
votes
3 answers

Using AND in an INNER JOIN

I am fairly new with SQL would like to understand the logic below. SELECT * FROM Table A A1 INNER JOIN TABLE B B1 ON B1.ID = A1.ID AND A1 = 'TASK'; Not sure if this is a clear detail but please let me know. Thanks!
sonnymoon
  • 113
  • 1
  • 1
  • 4
11
votes
1 answer

Laravel 5.2. DB table + Join with Update

I need update key in table catalog and I write query (mysql query is right): update attributes a inner join catalog c on a.parent_id = c.id set a.key = c.left_key and with Laravel DB: DB::table('attributes as a') ->join(catalog as c',…
Misha Babich
  • 111
  • 1
  • 1
  • 3
11
votes
7 answers

MySQL not responding for simple queries when using INNER JOIN. especially "Sending data" takes too much time

I have two tables; songs with 48 959 281 rows, and popular_songs with 5 721 117 rows. This is structure of that two tables: CREATE TABLE songs ( songId BIGINT(20) NOT NULL, songName VARCHAR(1000) NOT NULL, songDuration BIGINT(20)…
Temüjin
  • 15,371
  • 8
  • 35
  • 57
11
votes
3 answers

data.table inner/outer join with NA in join column of type double bug?

Following this wikipedia article SQL join I wanted to have a clear view on how we could have joins with data.table. In the process we might have uncovered a bug when joining with NAs. Taking the wiki example: R) X =…
statquant
  • 13,672
  • 21
  • 91
  • 162
10
votes
3 answers

What are the differences between these query JOIN types and are there any caveats?

I have multiple queries (from different section of my site) i am executing Some are like this: SELECT field, field1 FROM table1, table2 WHERE table1.id = table2.id AND .... and some are like this: SELECT field, field1 FROM table1 JOIN table2…
rcs20
  • 595
  • 9
  • 27
10
votes
2 answers

JPQL: Inner Join without duplicate records

Below is a question which supposedly was part of the official exam from Sun: A Reader entity has a one-to-many, bidirectional relationship with a Book entity. Two Reader entities are persisted, each having two Book entities associated with…
Andriy Kharchuk
  • 1,165
  • 1
  • 13
  • 25
10
votes
3 answers

MySQL Inner Join With LIMIT to left table

I have this database query SELECT * FROM (`metadata` im) INNER JOIN `content` ic ON `im`.`rev_id` = `ic`.`rev_id` WHERE `im`.`id` = '00039' AND `current_revision` = 1 ORDER BY `timestamp` DESC LIMIT 5, 5 The query limits the total rows in the…
Mr Hyde
  • 3,393
  • 8
  • 36
  • 48
10
votes
3 answers

SQL Server join where not exist on other table

+-------------------+ +-------------------+ +---------------------+ | Service | | Asset | | AssetService | +-------------------+ +-------------------+ +---------------------+ | Id | Name | | Id | …
akoxi
  • 225
  • 1
  • 3
  • 10
10
votes
3 answers

Inner Join vs. Natural Join, speed-wise?

I'm having a discussion with a colleague of mine, and we're stuck on the point of whether an inner join is faster than a natural join simply due to the fact that the ON clause is specified in an inner join, and thus the SQL engine need not compare…
Julian H. Lam
  • 25,501
  • 13
  • 46
  • 73
10
votes
2 answers

How to use subquery in the join function of Yii framework 2 ActiveRecord?

Below is my pure SQL query. SELECT a.*, b.* FROM a INNER JOIN b ON a.id = b.a_id INNER JOIN ( SELECT a_id, MAX(add_time) AS max_add_time FROM b GROUP BY a_id ) m ON b.a_id = m.a_id AND b.add_time = m.max_add_time ORDER BY…
O Connor
  • 4,236
  • 15
  • 50
  • 91
10
votes
2 answers

How to join same column twice?

I'm building a trading site and have these tables Example: The First one is fruit. +----+--------+ | ID | Name | +----+--------+ | 1 | Apple | | 2 | Orange | | 3 | Banana | +----+--------+ The Second one is the…
user5506707
10
votes
4 answers

SQLServer count() over() with distinct

I'm working on a project, where we need to count the number of distinct rows. A simplified version of the scenario includes a user table, a keyword table and a keyword_user table. The user table just includes common user meta data, like name etc.…
Teilmann
  • 2,146
  • 7
  • 28
  • 57
10
votes
2 answers

Django Model OneToOneField without creating additional _id database column

I'm working with an Account model and I want to inner join it with a Settings model without having to create an additional settings_id column in my Account model, because the PK on the Account table matches the PK on the settings table exactly. I've…
Cory Danielson
  • 14,314
  • 3
  • 44
  • 51