DBMS Hub
Labs

Lab 12 ⭐

Query Multiple Tables (Joins, Subqueries, and Correlated Queries)

Section 1: Concept Foundation

What are Joins and Subqueries?

In a relational database, data is normalized across multiple tables. To answer business questions, you often need to combine data from two or more tables.

  • SQL JOIN: A relational operation that combines rows from two or more tables based on a related column (common domain). The result is a single temporary table containing columns from all joined tables.
  • Subquery (Nested Query): A query nested inside another SQL query. It can be used in the SELECT, FROM, WHERE, or HAVING clause. The inner query executes before or depends on the outer query, providing a value or a set of values for filtering or calculation.

Why do they exist?

  • Data Normalization: Instead of storing all data in one giant flat table, databases split data into smaller, normalized tables (e.g., Employees and Departments). Joins reconstruct the complete picture.
  • Complex Business Logic: Subqueries enable dynamic filtering based on aggregated data (e.g., "Find employees earning more than the department average").
  • Data Integrity: They allow you to query across relationships without duplicating data.

Real-world Analogy

  • JOIN is like merging two separate spreadsheets (e.g., Spreadsheet A: Employee IDs and Names; Spreadsheet B: Employee IDs and Department Names) by matching the Employee ID to get a complete view.
  • Subquery is like asking a two-part question: First, calculate the average salary (inner query). Second, find employees earning more than that average (outer query).

Where are they used?

  • Reporting & Dashboards: Generating consolidated reports from transactional databases.
  • Data Analysis: Identifying trends, exceptions, and correlations across multiple dimensions.
  • Application Backend: Fetching complex object graphs (e.g., an Order with its Customer and Line Items) in a single database round-trip.

Section 2: Visual Understanding

Execution Flow of SQL Joins

Execution Flow of Correlated Subquery


Section 3: Syntax Breakdown and Oracle-Specific Rules

1. INNER JOIN

Purpose: Returns only rows where there is a match in both tables (the intersection). Syntax:

SELECT t1.column, t2.column
FROM table1 t1
INNER JOIN table2 t2 ON t1.common_column = t2.common_column;

Explanation:

  • INNER JOIN: Combines rows.
  • ON: Defines the matching condition (usually Table1.PrimaryKey = Table2.ForeignKey). Non-matching rows are excluded.

2. LEFT JOIN (LEFT OUTER JOIN)

Purpose: Returns all rows from the left table (table1), and the matched rows from the right table (table2). If no match exists, NULL is returned for right table columns. Syntax:

SELECT t1.column, t2.column
FROM table1 t1
LEFT JOIN table2 t2 ON t1.common_column = t2.common_column;

Explanation: The LEFT table is the one defined in the FROM clause. It is the "parent" table that dictates the result set size.

3. RIGHT JOIN (RIGHT OUTER JOIN)

Purpose: Returns all rows from the right table (table2), and the matched rows from the left table (table1). If no match exists, NULL is returned for left table columns. Syntax:

SELECT t1.column, t2.column
FROM table1 t1
RIGHT JOIN table2 t2 ON t1.common_column = t2.common_column;

4. FULL JOIN (FULL OUTER JOIN)

Purpose: Returns all rows from both tables. Matched rows are combined; unmatched rows from either side show NULL for the missing side's columns. Syntax:

SELECT t1.column, t2.column
FROM table1 t1
FULL JOIN table2 t2 ON t1.common_column = t2.common_column;

5. Single-Row Subqueries

Purpose: Returns exactly one row (one value). Used with comparison operators =, >, <, >=, <=, <>. Syntax:

SELECT column1
FROM table1
WHERE column1 > (SELECT AGGREGATE(column2) FROM table2 WHERE condition);

Example: Find employees earning more than the global average salary.

6. Multi-Row Subqueries

Purpose: Returns multiple rows. Used with operators IN, ANY, ALL, EXISTS. Syntax (with IN):

SELECT column1
FROM table1
WHERE column1 IN (SELECT column2 FROM table2 WHERE condition);

Explanation: IN checks if the outer value matches any value in the list returned by the subquery.

7. Correlated Subqueries

Purpose: The inner query depends on the outer query. It executes once for every row processed by the outer query. Syntax:

SELECT e1.ename, e1.sal
FROM emp e1
WHERE e1.sal > (SELECT AVG(e2.sal) FROM emp e2 WHERE e2.deptno = e1.deptno);

Explanation: For each employee (e1), Oracle runs the inner query to calculate the average salary of that employee's specific department (e1.deptno), then compares it.

8. Subquery in FROM Clause (Derived Table / Inline View)

Purpose: Treat the result of the subquery as a temporary table, allowing you to join against it or select from it. Syntax:

SELECT deptno, avg_sal
FROM (SELECT deptno, AVG(sal) AS avg_sal FROM emp GROUP BY deptno) temp;

Explanation: The inner query creates a virtual table named temp, which the outer query reads.

9. Subquery Operators: ANY, ALL, EXISTS

OperatorMeaningLogic Example
INMatches any value in the listsal IN (1000, 2000)
EXISTSChecks if at least one row is returnedWHERE EXISTS (SELECT 1 FROM emp)
ANYCompares with at least one value in the subquerysal > ANY (SELECT sal FROM emp) (Equivalent to sal > MIN(sal))
ALLCompares with every value in the subquerysal > ALL (SELECT sal FROM emp) (Equivalent to sal > MAX(sal))

Section 4: Query Thinking Process (Professional Approach)

Requirement: "Which customers have not placed any orders for writer's desks?"

Step 1: Identify the tables involved -> Customer_T, Order_T, OrderLine_T, Product_T. Step 2: Identify the target -> Customer Names. Step 3: Determine relationships -> Customer_T -> Order_T (via CustomerID), Order_T -> OrderLine_T (via OrderID), OrderLine_T -> Product_T (via ProductID). Step 4: Determine the filtering logic -> We need customers who do NOT exist in a subquery that filters customers who did order writer's desks. Step 5: Write the Query:

  1. Find all customers who ordered writer's desk.
  2. Find customers not in that list.
SELECT CustomerName
FROM Customer_T
WHERE CustomerID NOT IN (
    SELECT o.CustomerID
    FROM Order_T o
    JOIN OrderLine_T ol ON o.OrderID = ol.OrderID
    JOIN Product_T p ON ol.ProductID = p.ProductID
    WHERE p.ProductDescription = 'Writer''s Desk'
);

Step 6: Validate -> Test with sample data. Ensure Writer's Desk is the exact product name string.


Section 5: Multiple Examples

Example 1: Easy (INNER JOIN)

Requirement: List all employees and their corresponding department names. Query:

SELECT e.Name, d.DepartmentName
FROM Employees e
INNER JOIN Departments d ON e.DeptID = d.DeptID;

Observation: Employee Usman (DeptID 5) is excluded because DeptID 5 does not exist in the Departments table.

Example 2: Medium (LEFT JOIN)

Requirement: List all employees and their department names. Include employees even if they do not have a valid department. Query:

SELECT e.Name, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d ON e.DeptID = d.DeptID;

Observation: Usman is included, with DepartmentName showing NULL.

Example 3: Exam-style (FULL JOIN)

Requirement: List all employees and all departments. Show all combinations, and include unmatched rows from both sides. Query:

SELECT e.Name, d.DepartmentName
FROM Employees e
FULL JOIN Departments d ON e.DeptID = d.DeptID;

Observation: Returns Usman (no department), Marketing (no employee), and all matched rows.

Example 4: Real-world (Subquery with EXISTS)

Requirement: Find all departments that have at least one employee. Query:

SELECT DepartmentName
FROM Departments d
WHERE EXISTS (SELECT 1 FROM Employees e WHERE e.DeptID = d.DeptID);

Performance Note: EXISTS stops scanning once the first match is found, making it very efficient for checking existence.

Example 5: Tricky (Correlated Subquery vs JOIN)

Requirement: Find employees who earn more than the average salary of their own department. Query (Correlated Subquery):

SELECT e1.ename, e1.sal
FROM emp e1
WHERE e1.sal > (SELECT AVG(e2.sal) FROM emp e2 WHERE e2.deptno = e1.deptno);

Query (JOIN version - faster):

SELECT e.ename, e.sal
FROM emp e
JOIN (SELECT deptno, AVG(sal) AS avg_sal FROM emp GROUP BY deptno) d
ON e.deptno = d.deptno
WHERE e.sal > d.avg_sal;

Explanation: The JOIN version runs the aggregate subquery once to create a derived table, then joins and filters. The correlated subquery runs the aggregate query for every single employee row.


Section 6: Pattern Recognition

Requirement KeywordThink ofSQL Feature
"List... and their..." (both sides)INNER JOINJoin where ON condition matches
"Include even if no..."LEFT JOIN (if preserving left) / RIGHT JOIN (if preserving right)Outer Join to preserve all rows on one side
"All from both tables"FULL JOINFull Outer Join
"Greater than the average"Single-row SubqueryWHERE col > (SELECT AVG(...))
"In a specific list"Multi-row Subquery with INWHERE col IN (SELECT ...)
"If a record exists"EXISTSWHERE EXISTS (SELECT 1 ...)
"More than at least one"ANYWHERE col > ANY (SELECT ...) (compare with MIN)
"More than all"ALLWHERE col > ALL (SELECT ...) (compare with MAX)
"Per group/Per department"Correlated Subquery or JOIN with Derived TableInner query depends on outer alias

Section 7: Common Mistakes

MistakeWrong QueryWhy WrongCorrect Query
Forgetting the JOIN conditionSELECT * FROM Employees e, Departments d;Creates a Cartesian Product (every employee matched with every department, resulting in a huge, incorrect result set).Always use ON e.DeptID = d.DeptID.
Ambiguous column namesSELECT DeptID FROM Employees e JOIN Departments d ON ...;DeptID exists in both tables, causing error "ORA-00918: column ambiguously defined".Use table aliases: e.DeptID or d.DeptID.
Using = for multi-row subqueriesWHERE sal = (SELECT sal FROM emp WHERE deptno=10);If deptno=10 has multiple employees, the subquery returns more than one row, causing error ORA-01427: single-row subquery returns more than one row.Use IN for multiple rows: WHERE sal IN (SELECT ...).
Incorrect JOIN directionEmployees e RIGHT JOIN Departments d (expecting all employees).RIGHT JOIN preserves all Departments, not all Employees.Use LEFT JOIN if you want to keep all Employees.
Correlated Subquery alias confusionSELECT e1.ename FROM emp e1 WHERE sal > (SELECT AVG(sal) FROM emp e2 WHERE e2.deptno = e1.deptno); (missing e2. in the average).The inner query needs a reference to the outer alias (e1). Without it, it may calculate the wrong average.Ensure the correlation condition references the outer alias (e1.deptno).
NOT IN with NULL valuesWHERE deptno NOT IN (SELECT deptno FROM departments) (if subquery contains NULL).If the subquery contains a NULL, the result of NOT IN is unknown (NULL) for all rows, returning an empty result set.Use NOT EXISTS instead, or filter out NULLs: WHERE deptno NOT IN (SELECT deptno FROM departments WHERE deptno IS NOT NULL).

Section 8: Exam Perspective

Frequently Asked Questions

  1. Write a query to list all orders with their customer names.
  2. List all products that have never been ordered (use a subquery or LEFT JOIN).
  3. What is the difference between INNER JOIN and LEFT JOIN?
  4. Explain the difference between a correlated and non-correlated subquery.
  5. Write a query to find the second highest salary using a subquery.

Viva Questions

  • What happens if you omit the ON clause in a JOIN? (You get a Cartesian product).
  • Why are JOINs generally faster than correlated subqueries for large datasets? (Correlated subqueries execute once for every outer row; JOINs process sets).
  • When should you use EXISTS instead of IN? (When checking existence, EXISTS is more efficient and safer with NULLs).
  • What is the difference between ANY and ALL? (ANY matches if at least one value meets the condition (OR); ALL matches only if all values meet the condition (AND)).
  • Explain the logic of a derived table (inline view). (It is a subquery in the FROM clause that acts as a temporary table for the outer query).

MCQ

  1. Which JOIN returns all rows from the left table and only matched rows from the right? a) INNER JOIN b) LEFT JOIN c) RIGHT JOIN d) FULL JOIN Answer: b

  2. Which operator is used to check if a value matches at least one value from a subquery? a) ALL b) EXISTS c) ANY d) IN Answer: c

  3. A subquery that depends on the outer query and executes once per outer row is called: a) Single-row subquery b) Multi-row subquery c) Correlated subquery d) Derived table Answer: c


Section 9: Industry Perspective

How professionals use JOINS and Subqueries

  • Performance Tuning: Professionals analyze execution plans. They often rewrite correlated subqueries into JOINs with derived tables because join operations are executed by the optimizer as set-based operations, which are generally faster than row-by-row operations.
  • Data Warehousing: Star schemas (Fact tables and Dimension tables) rely heavily on INNER JOINs. LEFT JOINs are used when dimension data is missing (e.g., customers without orders) to ensure completeness.
  • Application Development: ORMs (Object-Relational Mappers) often generate JOIN queries to load associated entities. Professionals avoid N+1 query problems by using explicit JOINs and eager loading.

Best Practices

  • Prefilter using WHERE before joining: WHERE filters rows after joining in some SQL engines, but in Oracle, the optimizer usually handles it well. However, always ensure your ON condition is the most selective.
  • Use EXISTS over IN: When checking existence, EXISTS is typically more efficient as it stops scanning as soon as a match is found, and it handles NULL values gracefully.
  • Avoid SELECT * with JOINS: Specifying columns reduces network traffic and prevents ambiguity.
  • Index Foreign Keys: Always create indexes on foreign key columns. This drastically speeds up join operations (ON e.DeptID = d.DeptID).
  • Use Table Aliases: Always use meaningful, short aliases (e.g., e for employees, d for departments) to make queries readable and prevent column ambiguity.

Section 10: Query Construction Framework (Multi-Table Queries)

For any problem requiring data from multiple tables:

  1. Identify all tables needed to satisfy the requirement.
  2. Determine the relationship between tables (Foreign Key to Primary Key connections).
  3. Decide on the JOIN type based on the requirement:
    • If you need matching records only -> INNER JOIN.
    • If you need all records from a specific table -> LEFT JOIN or RIGHT JOIN.
    • If you need all records from all tables -> FULL JOIN.
  4. Formulate the JOIN condition using ON and the matching keys.
  5. Filter the rows using WHERE (if needed).
  6. Group and Aggregate using GROUP BY and HAVING if aggregates are requested.
  7. Write the query step-by-step, starting with FROM and JOINs.
  8. Validate the result set size and data accuracy.

Section 11: Practice Section

Beginner Exercises

  1. List all orders with their respective customer names (use Order_T and Customer_T).
  2. List all products and their total quantities ordered (join OrderLine_T and Product_T).
  3. List all departments and the count of employees in each (join Employees and Departments, group by department).
  4. Find all customers who have placed orders (use INNER JOIN or a subquery with IN).
  5. List all products with their finish and standard price, but only those that have been ordered at least once.

Intermediate Exercises

  1. List all customers and their order IDs. Include customers who have not placed any orders (use LEFT JOIN).
  2. List all products and the order IDs they appear in. Include products that have never been ordered.
  3. Find the total value (Quantity * StandardPrice) for each order in the OrderLine_T table.
  4. List the names of customers who have ordered 'End Table'.
  5. Write a query to find the customer(s) who placed the order with the highest total value (requires a subquery or MAX).

Advanced Exercises

  1. Using a correlated subquery, find all products whose standard price is greater than the average standard price of all products in their ProductLineID.
  2. Using the EMP table, find all employees who earn more than the average salary of their department (use a derived table or a correlated subquery).
  3. Write a query using NOT EXISTS to find customers who have never placed an order.
  4. Write a query that uses ANY to find all products priced greater than the price of at least one product in line 3.
  5. Write a query that uses ALL to find the product with the highest price in the entire Product_T table (or equivalent to MAX).

Section 12: Challenge Section – Real-world Scenarios

Scenario 1: E-commerce Order Fulfillment

A warehouse wants to identify all orders that contain products that are currently out of stock. Tables: Order_T, OrderLine_T, Inventory. Write a query to list the OrderIDs and ProductIDs where the Quantity ordered is greater than the Stock available for that product.

Scenario 2: HR Departmental Analysis

Using the standard EMP and DEPT tables, the HR Director wants a report showing the department name, the number of employees, the average salary, and the maximum salary for each department, but only for departments where the average salary is greater than 3000.

Scenario 3: Banking Anomaly Detection

The Bank wants to identify accounts where the transaction amount is greater than the average transaction amount of all accounts. Write a query to list the AccountID, TransactionAmount, and the Average_Transaction_Amount (calculate AVG globally) to highlight the high-value transactions. (Use a derived table to compute the global average).


Section 13: Interview Preparation

Conceptual Questions

  1. Explain the difference between an INNER JOIN and a LEFT JOIN.

    • INNER JOIN returns only rows that have a matching key in both tables. LEFT JOIN returns all rows from the left table, and matched rows from the right table; if there is no match, the right table columns are NULL.
  2. When would you use a FULL OUTER JOIN?

    • When you need to see all records from both tables, regardless of whether they have a matching record in the other table. This is common in data reconciliation or comparing two lists.
  3. What is the fundamental difference between a correlated subquery and a non-correlated subquery?

    • A non-correlated subquery executes once and provides a static result (e.g., a list of IDs) to the outer query. A correlated subquery executes repeatedly, once for every row selected by the outer query, because the inner query references a column from the outer query.
  4. Why would a developer prefer a JOIN over a correlated subquery?

    • JOINs are typically optimized by the query optimizer as set-based operations. Correlated subqueries can be significantly slower on large datasets due to their row-by-row execution.
  5. What does NOT IN do, and why is it dangerous if the subquery returns NULL?

    • NOT IN excludes values that are in the subquery's result set. If the subquery returns any NULL, the entire NOT IN condition evaluates to UNKNOWN for every row, resulting in an empty result set.

Practical SQL Interview Questions

Q1: Write a query to list all employees (Name and Department Name) using a JOIN. Answer:

SELECT e.Name, d.DepartmentName
FROM Employees e
JOIN Departments d ON e.DeptID = d.DeptID;

Q2: Write a query to find all customers who have not placed any orders. Answer:

SELECT c.CustomerName
FROM Customer_T c
LEFT JOIN Order_T o ON c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL;

Alternative:

SELECT CustomerName FROM Customer_T WHERE CustomerID NOT IN (SELECT CustomerID FROM Order_T);

Q3: How would you find employees earning more than the average salary of all employees? Answer:

SELECT ename, sal FROM emp
WHERE sal > (SELECT AVG(sal) FROM emp);

Q4: Using ANY, find all employees who earn more than the lowest salary in department 10. Answer:

SELECT ename, sal FROM emp
WHERE sal > ANY (SELECT sal FROM emp WHERE deptno = 10);

Logic: sal > MIN(sal in dept 10).

Q5: Write a query to find the product with the highest standard price. Answer:

SELECT * FROM Product_T
WHERE ProductStandardPrice = (SELECT MAX(ProductStandardPrice) FROM Product_T);

Section 14: Topic Summary – Cheat Sheet

JOINs Cheat Sheet

Join TypeKeep LeftKeep RightRows Included
INNER JOINOnly matchedOnly matchedMatching rows only (Intersection)
LEFT JOINAllOnly matchedAll Left, NULLs for missing Right
RIGHT JOINOnly matchedAllAll Right, NULLs for missing Left
FULL JOINAllAllAll Left + All Right

Subquery Cheat Sheet

Subquery TypeExecution StyleOutputCommon Operators
Single-rowExecutes once, returns one rowSingle value= , >, <, >=, <=, <>
Multi-rowExecutes once, returns multiple rowsList of valuesIN, ANY, ALL, EXISTS
CorrelatedExecutes per outer row (row-by-row)Single value per row= , >, < (compared with outer alias)
Derived TableExecutes once, stored temporarily as a tableTableUsed in FROM clause, can be Joined

Key Operator Interpretation

  • IN: Member of a list.
  • EXISTS: True if at least one row is found.
  • ANY: Greater than at least one (min).
  • ALL: Greater than all (max).

Final Professional Advice (Lab Solutions for specific tasks in PDF)

Task 1: List customer name, ID, and order number. Include order even if no customer.

SELECT c.CustomerName, c.CustomerID, o.OrderID
FROM Customer_T c
RIGHT JOIN Order_T o ON c.CustomerID = o.CustomerID;

Task 2: (Already covered in examples).

Task 3: List customer name, ID, and order number. Include customer even if no order.

SELECT c.CustomerName, c.CustomerID, o.OrderID
FROM Customer_T c
LEFT JOIN Order_T o ON c.CustomerID = o.CustomerID;

Task 4: List customer name, ID, and order number for all orders. Include order even if no customer.

SELECT c.CustomerName, c.CustomerID, o.OrderID
FROM Customer_T c
RIGHT JOIN Order_T o ON c.CustomerID = o.CustomerID;

(Note: Task 2, 3, and 4 are fundamental joins. Task 3 is LEFT, Task 4 is RIGHT, Task 1 is RIGHT but focusing on orders).

Task 5: Which customers have not placed any orders for writer's desks? (Solution provided in Section 4 - Query Thinking Process).

Task 6: What is the name and address of the customer who placed order number 1001?

SELECT c.CustomerName, c.CustomerAddress
FROM Customer_T c
JOIN Order_T o ON c.CustomerID = o.CustomerID
WHERE o.OrderID = 1001;

Task 7: What is the total value of orders placed for each furniture product?

SELECT p.ProductDescription, SUM(ol.OrderedQuantity * p.ProductStandardPrice) AS Total_Value
FROM Product_T p
JOIN OrderLine_T ol ON p.ProductID = ol.ProductID
GROUP BY p.ProductDescription;

Mastering multi-table queries is the single most important skill for a database professional. Practice writing JOINs and Subqueries by hand, and always ask yourself: "Which tables do I need, and how are they connected?"


Special Practice Section

Table 1: Suppliers

SupplierIDSupplierNameCountry
1TechCorpUSA
2GadgetWorldChina
3HomeEssentialsGermany
4SmartSupplyJapan

Table 2: Customers

CustomerIDCustomerNameCityJoinDate
1John DoeNew York2024-01-15
2Alice SmithLondon2024-03-20
3Bob JohnsonParis2024-05-10
4Clara LeeTokyo2024-07-01
5David BrownBerlin2024-08-15
6Emma WilsonRome2025-01-01

Table 3: Products

ProductIDProductNameCategoryPriceStockSupplierID
1LaptopElectronics1200.00101
2SmartphoneElectronics800.00151
3MonitorElectronics300.00201
4TabletElectronics500.00101
5DeskFurniture450.0052
6ChairFurniture250.00102
7BookshelfFurniture180.0083

Table 4: Orders

OrderIDCustomerIDOrderDateTotalAmountStatus
112024-02-101950.00Shipped
222024-04-151300.00Delivered
332024-06-20950.00Shipped
412024-07-25800.00Delivered
542024-09-011320.00Pending
652024-10-10265.00Shipped
722024-11-05300.00Delivered
832024-12-15700.00Shipped
942025-01-20950.00Delivered
1052025-02-252000.00Shipped

Table 5: OrderItems

OrderItemIDOrderIDProductIDQuantityUnitPrice
11111200.00
2132300.00
31121150.00
4221800.00
5241500.00
6351450.00
7362250.00
8431300.00
9441500.00
105111200.00

2. ERD


3. SQL Query Questions (Mixed Difficulty)

  1. Write a query to retrieve the OrderID, OrderDate, and the corresponding CustomerName for every order in the system.

  2. Write a query to find the ProductName and Price of the single most expensive product in the inventory.

  3. Write a query to display the ProductName and the total quantity sold (sum of Quantity from OrderItems) for each product, sorted from most sold to least sold.

  4. Write a query to show the OrderID, CustomerName, ProductName, and Quantity for all orders placed after '2025-01-01'.

  5. Find the names of all customers who have never placed an order.

  6. Write a query to find all products whose Price is strictly greater than the average price of products within their own Category.

  7. Find customers who have placed more than 5 orders. Display their CustomerName and the total number of orders they have placed.

  8. Identify the suppliers who supply every single product that belongs to the 'Electronics' category.

  9. Write a query to display each OrderID, its TotalAmount, and the historical average TotalAmount of all orders placed by that specific customer.

  10. List the names of customers who have spent a total (SUM(TotalAmount) across all their orders) that is greater than the overall average total spending of all customers in the system.

  11. Write a query to find the product with the second-highest price within each product category. Display Category, ProductName, and Price.

  12. Find all customers who have placed an order containing the product that has the maximum price among all products in the entire catalog.

Solution:

Solution 1

SELECT o.OrderID, o.OrderDate, c.CustomerName
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID;

Solution 2

SELECT ProductName, Price
FROM Products
WHERE Price = (SELECT MAX(Price) FROM Products);

Solution 3

SELECT p.ProductName, SUM(oi.Quantity) AS TotalSold
FROM Products p
JOIN OrderItems oi ON p.ProductID = oi.ProductID
GROUP BY p.ProductName
ORDER BY TotalSold DESC;

Solution 4

SELECT o.OrderID, c.CustomerName, p.ProductName, oi.Quantity
FROM Orders o
JOIN Customers c ON o.CustomerID = c.CustomerID
JOIN OrderItems oi ON o.OrderID = oi.OrderID
JOIN Products p ON oi.ProductID = p.ProductID
WHERE o.OrderDate > DATE '2025-01-01';

Solution 5

SELECT CustomerName
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderID IS NULL;

Alternative using NOT EXISTS:

SELECT CustomerName
FROM Customers c
WHERE NOT EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerID = c.CustomerID);

Solution 6

SELECT ProductName, Price
FROM Products p1
WHERE Price > (
    SELECT AVG(Price)
    FROM Products p2
    WHERE p2.Category = p1.Category
);

Solution 7

SELECT c.CustomerName, COUNT(o.OrderID) AS OrderCount
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerName
HAVING COUNT(o.OrderID) > 5;

Solution 8

SELECT s.SupplierName
FROM Suppliers s
WHERE NOT EXISTS (
    SELECT 1
    FROM Products p
    WHERE p.Category = 'Electronics'
      AND NOT EXISTS (
          SELECT 1
          FROM Products p2
          WHERE p2.SupplierID = s.SupplierID
            AND p2.ProductID = p.ProductID
      )
);

Solution 9

Using a scalar subquery in the SELECT clause (traditional exam style):

SELECT o.OrderID,
       o.TotalAmount,
       (SELECT AVG(o2.TotalAmount)
        FROM Orders o2
        WHERE o2.CustomerID = o.CustomerID) AS CustomerAvgOrderValue
FROM Orders o;

For completeness – Oracle analytic equivalent (even cleaner):

SELECT OrderID,
       TotalAmount,
       AVG(TotalAmount) OVER (PARTITION BY CustomerID) AS CustomerAvgOrderValue
FROM Orders;

Solution 10

WITH CustomerTotals AS (
    SELECT CustomerID, SUM(TotalAmount) AS TotalSpent
    FROM Orders
    GROUP BY CustomerID
)
SELECT c.CustomerName
FROM Customers c
JOIN CustomerTotals ct ON c.CustomerID = ct.CustomerID
WHERE ct.TotalSpent > (SELECT AVG(TotalSpent) FROM CustomerTotals);

Solution 11

WITH RankedProducts AS (
    SELECT Category,
           ProductName,
           Price,
           DENSE_RANK() OVER (PARTITION BY Category ORDER BY Price DESC) AS rnk
    FROM Products
)
SELECT Category, ProductName, Price
FROM RankedProducts
WHERE rnk = 2;

Solution 12

SELECT DISTINCT c.CustomerName
FROM Customers c
JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderID IN (
    SELECT OrderID
    FROM OrderItems
    WHERE ProductID IN (
        SELECT ProductID
        FROM Products
        WHERE Price = (SELECT MAX(Price) FROM Products)
    )
);

On this page

Section 1: Concept FoundationWhat are Joins and Subqueries?Why do they exist?Real-world AnalogyWhere are they used?Section 2: Visual UnderstandingExecution Flow of SQL JoinsExecution Flow of Correlated SubquerySection 3: Syntax Breakdown and Oracle-Specific Rules1. INNER JOIN2. LEFT JOIN (LEFT OUTER JOIN)3. RIGHT JOIN (RIGHT OUTER JOIN)4. FULL JOIN (FULL OUTER JOIN)5. Single-Row Subqueries6. Multi-Row Subqueries7. Correlated Subqueries8. Subquery in FROM Clause (Derived Table / Inline View)9. Subquery Operators: ANY, ALL, EXISTSSection 4: Query Thinking Process (Professional Approach)Section 5: Multiple ExamplesExample 1: Easy (INNER JOIN)Example 2: Medium (LEFT JOIN)Example 3: Exam-style (FULL JOIN)Example 4: Real-world (Subquery with EXISTS)Example 5: Tricky (Correlated Subquery vs JOIN)Section 6: Pattern RecognitionSection 7: Common MistakesSection 8: Exam PerspectiveFrequently Asked QuestionsViva QuestionsMCQSection 9: Industry PerspectiveHow professionals use JOINS and SubqueriesBest PracticesSection 10: Query Construction Framework (Multi-Table Queries)Section 11: Practice SectionBeginner ExercisesIntermediate ExercisesAdvanced ExercisesSection 12: Challenge Section – Real-world ScenariosScenario 1: E-commerce Order FulfillmentScenario 2: HR Departmental AnalysisScenario 3: Banking Anomaly DetectionSection 13: Interview PreparationConceptual QuestionsPractical SQL Interview QuestionsSection 14: Topic Summary – Cheat SheetJOINs Cheat SheetSubquery Cheat SheetKey Operator InterpretationFinal Professional Advice (Lab Solutions for specific tasks in PDF)Special Practice SectionTable 1: SuppliersTable 2: CustomersTable 3: ProductsTable 4: OrdersTable 5: OrderItems2. ERD3. SQL Query Questions (Mixed Difficulty)Solution:Solution 1Solution 2Solution 3Solution 4Solution 5Solution 6Solution 7Solution 8Solution 9Solution 10Solution 11Solution 12