home
0%
Progress
CBSE Code 802 | Unit 1: Database Concepts - RDBMS

07. Multi-Table Queries & SQL Joins

Cartesian Product math, Equi-Join, INNER JOIN ON, Natural Join, board question walkthroughs.

Unit 1: Database Concepts (RDBMS)

Chapter 7: Multi-Table Queries and SQL Joins

1. Why Multi-Table Queries?

Relational databases normalize data across multiple related tables to eliminate data redundancy. A Join combines tuples from two or more tables based on a common related column (typically Primary Key in one table and Foreign Key in the other).


2. Cartesian Product (Cross Join)

A Cartesian Product pairs every row of Table $A$ with every row of Table $B$ without any matching condition. - If Table $A$ has Degree $D_1$ and Cardinality $C_1$, and Table $B$ has Degree $D_2$ and Cardinality $C_2$: - $\text{Degree}(A \times B) = D_1 + D_2$ - $\text{Cardinality}(A \times B) = C_1 \times C_2$

-- Cartesian Product Syntax
SELECT * FROM Student, Department;

3. Equi-Join

An Equi-Join is a join condition using an equality operator (=) on common key attributes to filter out meaningless combinations from a Cartesian product.

HOW SQL EQUI-JOIN BRINGS TWO TABLES TOGETHER Table 1: STUDENT RollNo Name Dept_ID (FK) 101 Aman D01 102 Priya D02 Table 2: DEPARTMENT Dept_ID (PK) Dept_Name D01 Comp. Science D02 Information Tech COMMON KEY Dept_ID = Dept_ID RESULT OF EQUI-JOIN (Combined Row Data) RollNo Name Dept_ID Dept_Name 101 Aman D01 Comp. Science 102 Priya D02 Information Tech
Figure 7.1: Visual Sketch — How an Equi-Join uses the common key (Dept_ID) to merge related tuples seamlessly.

Syntax

SELECT Table1.col1, Table2.col2
FROM Table1, Table2
WHERE Table1.common_key = Table2.common_key;

Table Aliases for Clean Syntax

SELECT S.RollNo, S.Name, D.Dept_Name, D.HOD
FROM Student S, Department D
WHERE S.Dept_ID = D.Dept_ID;

4. ANSI SQL JOIN Syntax (INNER JOIN)

The modern ANSI standard uses the explicit JOIN ... ON syntax:

SELECT S.RollNo, S.Name, S.Marks, D.Dept_Name
FROM Student S
INNER JOIN Department D
ON S.Dept_ID = D.Dept_ID
WHERE S.Marks >= 80;

5. Natural Join

A Natural Join compares all columns with matching names in both tables and automatically eliminates redundant duplicate join columns from the result.

SELECT * FROM Student NATURAL JOIN Department;

6. Comprehensive Board Question Walkthrough

Consider the two tables below:

Table: DOCTOR | Doc_ID | Doc_Name | Department | OPD_Fee | | :--- | :--- | :--- | :--- | | D101 | Dr. Verma | Cardiology | 800 | | D102 | Dr. Mehta | Neurology | 1000 | | D103 | Dr. Ray | Pediatrics | 600 |

Table: PATIENT | Pat_No | Pat_Name | Age | Doc_ID | | :--- | :--- | :--- | :--- | | P01 | Aman | 45 | D101 | | P02 | Rekha | 32 | D103 | | P03 | Vijay | 58 | D101 | | P04 | Meera | 24 | D102 |

Queries:

  1. Display Patient Name, Age, and their Doctor's Name: sql SELECT P.Pat_Name, P.Age, D.Doc_Name FROM PATIENT P, DOCTOR D WHERE P.Doc_ID = D.Doc_ID;
  2. Display Doctor Name and Patient Name where OPD Fee is greater than 700: sql SELECT D.Doc_Name, P.Pat_Name, D.OPD_Fee FROM DOCTOR D, PATIENT P WHERE D.Doc_ID = P.Doc_ID AND D.OPD_Fee > 700;
  3. What is the degree and cardinality of the Cartesian product of DOCTOR and PATIENT?
  4. Degree of DOCTOR = 4, Cardinality = 3
  5. Degree of PATIENT = 4, Cardinality = 4
  6. $\text{Result Degree} = 4 + 4 = 8$
  7. $\text{Result Cardinality} = 3 \times 4 = 12$

Entity Details

Create New Item

help

Submit Technical Query

Have a question or run into an issue? Describe it below, upload an optional screenshot, and our engineering team will answer it!

image Attach image (optional)

Submit Feedback

build Free Developer Utility Free Tool
gavel

Privacy & Legal Disclaimer

1. Client-Side Browser Processing

All utility tools on DeepEngineerHub (including Image to PDF, Text Formatters, JSON Converters, and Encryptors) execute 100% locally within your client browser using WebAssembly and JavaScript. No uploaded images, text, or documents are transmitted, collected, or stored on remote servers.

2. Limitation of Liability ("As-Is" Provision)

Tools and services are provided free of charge for convenience and educational purposes "as-is" without warranties of any kind. DeepEngineerHub shall not be held liable for any data loss, formatting inconsistencies, or indirect damages resulting from tool usage.

3. Open Source & Third-Party Software

Certain utilities utilize open-source client libraries (such as jsPDF, Mermaid.js, Pyodide) licensed under MIT, Apache, or BSD open licenses. All intellectual property remains with their respective copyright holders.