Joining two different tables on their matching columns can be done using nested loops, but a more efficient and scalable way is to use multimaps. The idea is to map from each column value that we want to join to all the rows that contain it, to generate a multimap from a table out of both tables.
The multimap generated has to be hash-based. Hashing is essentially a technique converting a large element into a small element that represents the same element. Therefore, generate the multimap for the smaller table, thus decreasing its generation time and memory size.
- Create the two tables.
- Now, get the ID of the columns on both of the tables.
- Then create and implement a multimap for mapping to various rows of table B.
- Print the result after the above steps.
Below is the implementation of the above approach:
Table A: 0: 32 Rahul 1: 25 Anshul 2: 17 Lok 3: 25 Akil 4: 17 Anshul Table B: 0: Rahul Tim 1: Rahul Siva 2: Anshul Gary 3: Anshul Azhar 4: Lok Vamsi Joined tables: 0: 32 Rahul Rahul Tim 1: 32 Rahul Rahul Siva 2: 17 Anshul Anshul Gary 3: 25 Anshul Anshul Gary 4: 17 Anshul Anshul Azhar 5: 25 Anshul Anshul Azhar 6: 17 Lok Lok Vamsi
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Joining three or more tables in SQL
- Count number of unique Triangles using STL | Set 1 (Using set)
- Find the most frequent digit without using array/string
- Kruskal's Minimum Spanning Tree using STL in C++
- Dijkstra’s shortest path algorithm using set in STL
- Dijkstra's Shortest Path Algorithm using priority_queue of STL
- Prim's algorithm using priority_queue in STL
- Permutations of a given string using STL
- std::tuple, std::pair | Returning multiple values from a function using Tuple and Pair in C++
- Merge operations using STL in C++ | merge(), includes(), set_union(), set_intersection(), set_difference(), ., inplace_merge,
- Counts of distinct consecutive sub-string of length two using C++ STL
- Graph implementation using STL for competitive programming | Set 1 (DFS of Unweighted and Undirected)
- Graph implementation using STL for competitive programming | Set 2 (Weighted graph)
- Convert an array to reduced form | Set 2 (Using vector of pairs)
- Counting Inversions using Set in C++ STL
- How to implement Min Heap using STL?
- Print prime numbers in a given range using C++ STL
- Searching in a map using std::map functions in C++
- Conversion of whole String to uppercase or lowercase using STL in C++
- Union and Intersection of two linked lists | Set-2 (Using Merge Sort)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.