The growth of technology and automation coupled with exponential amounts of data have led to the importance and omnipresence of databases which, simple put, are organized collections of data. Considering a naive approach, one can theoretically keep all the data in one large table, however that increases the access time in searching for a record, security issues if the master table is destroyed, redundant storage of information and other issues. So tables are decomposed into multiple smaller tables.
For retrieving information from multiple tables, we need to extract selected data from different records, using operations called join(inner join, outer join and most importantly natural join). Consider 2 table schemas employee(employee_name, street, city)with n rows and works(employee_name, branch_name, salary) with m rows. A cartesian product of these 2 tables creates a table with n*m rows. A natural join selects from this n*m rows all rows with same values for employee_name. To avoid loss of information(some tuples in employee have no corresponding tuples in works) we use left outer join or right outer join.
A join or a nested query is better subject to conditions:
- Suppose our 2 tables are stored on a local system. Performing a join or a nested query will make little difference. Now let tables be stored across a distributed databases. For a nested query, we only extract the relevant information from each table, located on different computers, then merge the tuples obtained to obtain the result. For a join, we would be required to fetch the whole table from each site and create a large table from which the filtering will occur, hence more time will be required. So for distributed databases, nested queries are better.
- RDBMS optimizer is concerned with performance related to the subquery or join written by the programmer. Joins are universally understood hence no optimization issues can arise. If portability across multiple platforms is called for, avoid subqueries as it may run into bugs(SQL server more adept with joins as its usually used with Microsoft’s graphical query editors that use joins).
- Implementation specific: Suppose we have queries where a few of the nested queries are constant. In MySQL, every constant subquery would be evaluated as many times as encountered, there being no cache facility. This is an obvious problem if the constant subquery involves large tuples. Subqueries return a set of data. Joins return a dataset which is necessarily indexed. Working on indexed data is faster so if the dataset returned by subqueries is large, joins are a better idea.
- Subqueries may take longer to execute than joins depending on how the database optimizer treats them(maybe converted to joins). Subqueries are easier to read, understand and evaluate than cryptic joins. They allow a bottom-up approach, isolating and completing each task sequentially.
Refer – Join operation Vs nested query
- SQL | Join (Cartesian Join & Self Join)
- Difference between Nested Loop Join and Hash Join
- Difference between Nested Loop join and Sort Merge Join
- Inner Join vs Outer Join
- Difference between Inner Join and Outer Join in SQL
- Difference between Natural join and Inner Join in SQL
- Difference between Natural join and Cross join in SQL
- Full join and Inner join in MS SQL Server
- Left join and Right join in MS SQL Server
- Difference between Hash Join and Sort Merge Join
- Self Join and Cross Join in MS SQL Server
- SQL | EQUI Join and NON EQUI JOIN
- SQL | Join (Inner, Left, Right and Full Joins)
- Lossless Join and Dependency Preserving Decomposition
- Should I join a mass recruiter or a startup?
- Differences between wait() and join() methods in Java
- Join two text columns into a single column in Pandas
- Join algorithms in Database
- Difference between JOIN and UNION in SQL
- Difference between Left, Right and Full Outer Join
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.