0% found this document useful (0 votes)
10 views

SQL Oracle

Uploaded by

Madiha Farhad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

SQL Oracle

Uploaded by

Madiha Farhad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

nner outer left right join in oracle with + sign syntax

ChatGPT

Sure, here are the equivalent join queries in Oracle SQL using the (+) sign syntax:

Inner Join:
sql
Copy code
SELECT s.student_id, s.name AS student_name, c.course_id, c.name AS
course_name
FROM student s, course c
WHERE s.course_id = c.course_id;

Left Outer Join (Equivalent to Left Join):


sql
Copy code
SELECT s.student_id, s.name AS student_name, c.course_id, c.name AS
course_name
FROM student s, course c
WHERE s.course_id(+) = c.course_id;

Right Outer Join (Equivalent to Right Join):


sql
Copy code
SELECT s.student_id, s.name AS student_name, c.course_id, c.name AS
course_name
FROM student s, course c
WHERE s.course_id = c.course_id(+);

Full Outer Join:


sql
Copy code
SELECT s.student_id, s.name AS student_name, c.course_id, c.name AS
course_name
FROM student s, course c
WHERE s.course_id(+) = c.course_id(+);

Explanation:

 For a left outer join, place the (+) operator on the right side of the equality condition.
 For a right outer join, place the (+) operator on the left side of the equality condition.
 For a full outer join, place the (+) operator on both sides of the equality condition.

Using the (+) operator syntax for outer joins can sometimes be less readable and is not as widely
used as the ANSI SQL join syntax, but it's still supported in Oracle SQL.
One invoice can be associated with multiple payments how practical example
ChatGPT

In Oracle E-Business Suite (EBS), handling invoices and payments involves tracking the
complex financial transactions between invoices and payments. A practical example of a many-
to-many relationship between invoices and payments can be demonstrated with the use of the
AP_INVOICES_ALL, AP_PAYMENTS_ALL, and AP_INVOICE_PAYMENTS tables.

Here’s a practical scenario:

1. Invoice 1: Total amount $1000


2. Invoice 2: Total amount $2000
3. Payment 1: Amount $1500
4. Payment 2: Amount $2500

How This Works:

 Payment 1 can partially pay Invoice 1 and Invoice 2.


 Payment 2 can complete the payment of Invoice 1 and partially pay Invoice 2.

Step-by-Step Practical Example:

1. Create Simplified Tables for Demonstration:

sql
Copy code
-- Table for storing invoices
CREATE TABLE ap_invoices_all (
invoice_id NUMBER PRIMARY KEY,
invoice_number VARCHAR2(50) NOT NULL,
invoice_date DATE NOT NULL,
amount NUMBER NOT NULL
);

-- Table for storing payments


CREATE TABLE ap_payments_all (
payment_id NUMBER PRIMARY KEY,
payment_number VARCHAR2(50) NOT NULL,
payment_date DATE NOT NULL,
payment_amount NUMBER NOT NULL
);

-- Junction table linking invoices to payments


CREATE TABLE ap_invoice_payments (
invoice_id NUMBER,
payment_id NUMBER,
payment_amount_applied NUMBER NOT NULL,
PRIMARY KEY (invoice_id, payment_id),
FOREIGN KEY (invoice_id) REFERENCES ap_invoices_all(invoice_id),
FOREIGN KEY (payment_id) REFERENCES ap_payments_all(payment_id)
);
2. Insert Sample Data:

sql
Copy code
-- Insert invoices
INSERT INTO ap_invoices_all (invoice_id, invoice_number, invoice_date, amount)
VALUES (1, 'INV-001', TO_DATE('2024-01-01', 'YYYY-MM-DD'), 1000);
INSERT INTO ap_invoices_all (invoice_id, invoice_number, invoice_date, amount)
VALUES (2, 'INV-002', TO_DATE('2024-02-01', 'YYYY-MM-DD'), 2000);

-- Insert payments
INSERT INTO ap_payments_all (payment_id, payment_number, payment_date,
payment_amount)
VALUES (1, 'PAY-001', TO_DATE('2024-03-01', 'YYYY-MM-DD'), 1500);
INSERT INTO ap_payments_all (payment_id, payment_number, payment_date,
payment_amount)
VALUES (2, 'PAY-002', TO_DATE('2024-04-01', 'YYYY-MM-DD'), 2500);

-- Insert invoice-payments relationships


INSERT INTO ap_invoice_payments (invoice_id, payment_id,
payment_amount_applied)
VALUES (1, 1, 1000); -- Payment 1 pays $1000 towards Invoice 1
INSERT INTO ap_invoice_payments (invoice_id, payment_id,
payment_amount_applied)
VALUES (2, 1, 500); -- Payment 1 pays $500 towards Invoice 2
INSERT INTO ap_invoice_payments (invoice_id, payment_id,
payment_amount_applied)
VALUES (2, 2, 1500); -- Payment 2 pays $1500 towards Invoice 2

Explanation:

 Invoices: Represented by ap_invoices_all.


 Payments: Represented by ap_payments_all.
 Invoice-Payments Relationships: Represented by ap_invoice_payments, this table
records how much of each payment is applied to each invoice.

Practical Queries:

1. Query to Show How Payments Are Applied to Invoices:

sql
Copy code
SELECT
i.invoice_number,
i.invoice_date,
i.amount AS invoice_amount,
p.payment_number,
p.payment_date,
p.payment_amount AS total_payment_amount,
ip.payment_amount_applied
FROM
ap_invoices_all i
JOIN
ap_invoice_payments ip ON i.invoice_id = ip.invoice_id
JOIN
ap_payments_all p ON ip.payment_id = p.payment_id;

In this example:

 Payment 1: Paid $1000 towards Invoice 1 and $500 towards Invoice 2.


 Payment 2: Paid $1500 towards Invoice 2.

This setup demonstrates the practical handling of a many-to-many relationship between invoices
and payments in Oracle EBS.

one to many example in oracle ebs database table


ChatGPT

In Oracle E-Business Suite (EBS), a common example of a one-to-many relationship is between


customers and orders. One customer can place multiple orders, but each order is associated
with only one customer.

EBS Tables Involved:

 HZ_PARTIES: Stores customer information.


 OE_ORDER_HEADERS_ALL: Stores order information, where each order is linked to a single
customer.

Example Scenario:

1. Customers: Represented by the HZ_PARTIES table.


2. Orders: Represented by the OE_ORDER_HEADERS_ALL table.

Step 1: Create Simplified Versions of the Tables

For demonstration purposes, simplified versions of these tables will be used. You can adjust the
table names and columns based on the actual EBS structure.
Creating Simplified Tables for Demonstration

To understand what the subquery is returning with values, let's break down the provided query
using an example dataset and see the detailed execution.

Here's the SQL query again:

sql
Copy code
SELECT DISTINCT Salary
FROM EmployeePosition E1
WHERE 2 >= (
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE E1.Salary >= E2.Salary
)
ORDER BY E1.Salary DESC;

Example Dataset

Let's consider the following EmployeePosition table:

EmpID EmpPosition DateOfJoining Salary

1 Manager 2024-05-01 500000

2 Executive 2024-05-02 75000

3 Manager 2024-05-01 90000

4 Lead 2024-05-02 85000

5 Executive 2024-05-01 300000

Step-by-Step Execution

Outer Query Iteration

1. For E1.Salary = 500000:


o The subquery is executed:

sql
Copy code
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE 500000 >= E2.Salary

o This checks how many distinct salaries in the EmployeePosition table are less than or
equal to 500000:
 50000, 75000, 90000, 85000, 300000, 500000 (6 distinct salaries)
o Result: 6
o Condition: 2 >= 6 (false) → This salary is not included.

2. For E1.Salary = 300000:


o The subquery is executed:

sql
Copy code
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE 300000 >= E2.Salary

o This checks how many distinct salaries in the EmployeePosition table are less than or
equal to 300000:
 50000, 75000, 90000, 85000, 300000 (5 distinct salaries)
o Result: 5
o Condition: 2 >= 5 (false) → This salary is not included.

3. For E1.Salary = 90000:


o The subquery is executed:

sql
Copy code
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE 90000 >= E2.Salary

o This checks how many distinct salaries in the EmployeePosition table are less than or
equal to 90000:
 50000, 75000, 90000 (3 distinct salaries)
o Result: 3
o Condition: 2 >= 3 (false) → This salary is not included.

4. For E1.Salary = 85000:


o The subquery is executed:

sql
Copy code
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE 85000 >= E2.Salary

o This checks how many distinct salaries in the EmployeePosition table are less than or
equal to 85000:
 50000, 75000, 85000 (3 distinct salaries)
o Result: 3
o Condition: 2 >= 3 (false) → This salary is not included.
5. For E1.Salary = 75000:
o The subquery is executed:

sql
Copy code
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE 75000 >= E2.Salary

o This checks how many distinct salaries in the EmployeePosition table are less than or
equal to 75000:
 50000, 75000 (2 distinct salaries)
o Result: 2
o Condition: 2 >= 2 (true) → This salary is included.

6. For E1.Salary = 50000:


o The subquery is executed:

sql
Copy code
SELECT COUNT(DISTINCT Salary)
FROM EmployeePosition E2
WHERE 50000 >= E2.Salary

o This checks how many distinct salaries in the EmployeePosition table are less than or
equal to 50000:
 50000 (1 distinct salary)
o Result: 1
o Condition: 2 >= 1 (true) → This salary is included.

Final Result

After processing all rows, the outer query will include the salaries 75000 and 50000, as these
satisfy the condition 2 >= COUNT(DISTINCT Salary).

The final query result, after ordering in descending order, is:

Salary

75000

50000

This query effectively finds the top 2 highest distinct salaries from the EmployeePosition table.

You might also like