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
9
votes
4 answers

Querying the inner join of two tables with the same column name, Column 'exName' in field list is ambiguous

I am querying the inner join of three tables using the following query. Two of the tables have columns named "name1". I am getting the following error. Column 'exName' in field list is ambiguous The "name1" columns are foreign key so the…
Ben Pearce
  • 6,884
  • 18
  • 70
  • 127
9
votes
6 answers

SQL statement I can't wrap my head around (brain too small)

I'm writing sort of a travel-'dating' app. Users register themselves Users tell the app if they are male or female Users tell the app which countries they would like to visit Users tell the app if they want to travel with males (pref_m=1) or…
Wayfarer
  • 620
  • 6
  • 16
9
votes
3 answers

MYSQL group by and inner join

I have an article table which holds the number of articles views for each day. A new record is created to hold the count for each seperate day for each article. The query below gets the article id and total views for the top 5 viewed article id for…
user1052096
  • 853
  • 4
  • 13
  • 23
9
votes
2 answers

Postgres update with an inner join across 2 tables?

I have 3 tables in my local Postgres database: [myschema].[animals] -------------------- animal_id animal_attrib_type_id (foreign key to [myschema].[animal_attrib_types]) animal_attrib_value_id (foreign key to…
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
8
votes
3 answers

Join to an oracle table valued function

Is is possible to join to an Oracle table valued function? SELECT * FROM SOME_TABLE a INNER JOIN TABLE(GET_TABLE_LIST()) b ON = a.COL_A = b.COL_A
Ryan Fisch
  • 2,614
  • 5
  • 36
  • 57
8
votes
7 answers

Ambiguous column name SQL error with INNER JOIN: Why?

The following code will work to select data from two tables: SELECT t1.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.foo=t2.foo I could just as easily written SELECT t2.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON…
Vivian River
  • 31,198
  • 62
  • 198
  • 313
8
votes
2 answers

getting first row in postgres query

I am querying some data from 2 tables using inner join. here is the query, test_db=> select api_booking.install_ts, api_user.id from api_booking inner join api_user on api_booking.user_id=api_user.id and api_booking.status='completed' limit 20 ; …
Luv33preet
  • 1,686
  • 7
  • 33
  • 66
8
votes
1 answer

Hibernate mapping for complex joins

I have legacy code that I am trying to map to the new code. OLD_PERSON pid sid name age NEW_PERSON pid sid fid age RESOLVE_PERSON pid fid status Java class domain.Person { ID _id; String _name; Integer _age; } In the legacy world, there was…
imgr8
  • 501
  • 4
  • 11
  • 25
8
votes
3 answers

Is it possible to delete_all with inner join conditions?

I need to delete a lot of records at once and I need to do so based on a condition in another model that is related by a "belongs_to" relationship. I know I can loop through each checking for the condition, but this takes forever with my large…
spilliton
  • 3,811
  • 5
  • 35
  • 35
8
votes
3 answers

postgreSQL update from one Table to another based on a ID match

I have a database with sites and counties. Every site is located in one county. In the sites table, the county is already mentioned but I want to replace it with the ID, which in is the other table. My update code is as follows: UPDATE sites SET…
Toik95
  • 157
  • 1
  • 2
  • 13
8
votes
4 answers

Getting all fields from one table using INNER JOIN?

I want to get all fields from one table and use DISTINCT with the second table. I have this: SELECT stats.*, DISTINCT(visit_log.blog_id) AS bid FROM stats INNER JOIN visit_log ON stats.blog_id = visit_log.blog_id But I get this error: You have…
Klian
  • 1,520
  • 5
  • 21
  • 32
8
votes
5 answers

SQL: many-to-many relationship, IN condition

I have a table called transactions with a many-to-many relationship to items through the items_transactions table. I want to do something like this: SELECT "transactions".* FROM "transactions" INNER JOIN "items_transactions" ON…
Maarten
  • 133
  • 1
  • 5
8
votes
5 answers

What is so bad about using SQL INNER JOIN

Every time a database diagram gets looked out, one area people are critical of is inner joins. They look at them hard and has questions to see if an inner join really needs to be there. Simple Library Example: A many-to-many relationship is…
SBurris
  • 7,378
  • 5
  • 28
  • 36
8
votes
7 answers

Compare inner join and outer join SQL statements

What is the difference between an inner join and outer join? What's the precise meaning of these two kinds of joins?
freenight
  • 1,069
  • 3
  • 13
  • 19
8
votes
1 answer

Inner Join BETWEEN two values

I'm currently trying to populate my fact table and I'm having trouble populating the required time ID's. The time dimension is fully populated and complete. Basically in the time dimension each row is a 15 minute period. The fact table is populating…
Richard C
  • 389
  • 2
  • 5
  • 16