![]() |
PL/SQL Stands for procedural language extension to SQL. In a procedure, the role of the subprogram is to perform a particular task and it is a unit module of a program. It combined to form larger programs. A subprogram can be involved by another program which is called the calling program. PL/SQL provides a block structure of executable unit code. It Provides procedural constructs, for example, in control structure includes loops, conditional statements, and variable, constant, and data type. In PL/SQL, CROSS APPLY and INNER JOIN serve different purposes and are used in different contexts. It’s important to understand their functionalities and use cases to decide when to use one over the other. INNER JOIN
CROSS APPLY
Example of CROSS APPLY over INNER JOINExample 1: Retrieving Latest Salary Using CROSS APPLYThe provided SQL script creates two tables, Table: CREATE TABLE employees (employee_id INT PRIMARY KEY, employee_name VARCHAR(50)); Insert Data Into Table: INSERT INTO employees VALUES (1, 'John'); Query Using CROSS APPLY: SELECT employees.employee_id, employees.employee_name, latest_salary.salary Output: Explanation: In this example, CROSS APPLY clause is used to apply the subquery for each row in the employees table.
Choosing Between INNER JOIN and CROSS APPLY:
Example 2: Color-Animal Relationship Exploration with CROSS APPLYTable: CREATE TABLE Color (Id INT, Name VARCHAR(10)); Insert Data into table: INSERT INTO Color(Id,Name) VALUES (1, 'Red'); Query Using CROSS APPLY: SELECT C.Name, Output: Explanation: The FROM Color C part retrieves data from the Color table, aliasing it as C. The LEFT JOIN Animal A ON C.Id = A.ReferenceId specifies a left join between the Color and Animal tables based on the condition that the Id from the Color table matches the ReferenceId from the Animal table. The SELECT C.Name AS ColorName, A.Name AS AnimalName selects the Name column from the Color table (aliased as C) and the Name column from the Animal table (aliased as A). The use of AS is optional; it is used to provide a clear alias for each column in the result set. The query retrieves color names and, where available, the corresponding animal names based on the ReferenceId relationship. If there is no matching ReferenceId for a color in the Animal table, the AnimalName column will contain NULL for that particular row. INNER JOINAn INNER JOIN is a type of SQL join Query that combines rows from two or more tables based on a related column between them will have some common relation between the tables. The result includes only the rows where there is a match between the values in the specified columns. We will run this query on same table of Animal and colors. SELECT Output: Explanation: In this query:
This query will return only the rows where there is a match between the Color and Animal tables based on the specified condition. Key Differences:
In summary, INNER JOIN is a standard SQL operation for combining rows based on matching criteria, while CROSS APPLY is specific to SQL Server and is often used to apply table-valued functions for each row of the outer table. The choice between them depends on the specific requirements of your query and the data manipulation you need to perform. ConclusionIt’s essential to consider the nature of your data and the logic you want to achieve when choosing between these operators. In Oracle databases (used with PL/SQL), the equivalent to CROSS APPLY is often achieved using OUTER APPLY or other constructs depending on the specific use case. Always refer to the documentation of the specific database you are using for details on available features and syntax. |
Reffered: https://www.geeksforgeeks.org
Databases |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |