![]() |
The growth of technology and automation coupled with exponential amounts of data has led to the importance and omnipresence of databases which, simply 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 operation or a nested query is better subject to conditions:
Join operation and nested queries are both used in relational database management systems (RDBMS) to combine data from multiple tables, but they differ in their approach. Join operation: For example, let’s say we have two tables, Table1 and Table2, with the following data: Table1 Table2 If we want to combine the data from these two tables based on the ID column, we can perform an inner join using the following SQL query:
SELECT Table1.ID, Table1.Name, Table2.Address Result: Nested query: For example, let’s say we have the same two tables as before: Table1 Table2 If we want to retrieve the names of the people who have an address in Table2, we can use a nested query as follows: SELECT Name Result: In this case, the nested query is executed first, and it returns the ID values of the rows in Table2. These ID values are then used to evaluate the outer query, which retrieves the names of the people who have those ID values in Table1. The choice between using a join operation or a nested query depends on the specific requirements of the task at hand. Joins are often faster and more efficient for large datasets, but nested queries can be more flexible and allow for more complex conditions to be evaluated. |
Reffered: https://www.geeksforgeeks.org
Interview Experiences |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 7 |