Skip to content

SQL Table Joins & DDL / DML Review

In relational databases, data is split across multiple normalized tables to prevent data redundancy and anomalies. A Join is an operation that combines records from two or more tables based on a related common column.


1. Cartesian Product (Cross Join)

The Cartesian Product of two relations A and B (denoted A×B) is the concatenation of every tuple in relation A with every tuple in relation B.

sql
SELECT * FROM Student, Course;
-- Or explicitly:
SELECT * FROM Student CROSS JOIN Course;

Mathematical Properties:

If relation A has Degree D1 and Cardinality C1, and relation B has Degree D2 and Cardinality C2:

Degree(A×B)=D1+D2Cardinality(A×B)=C1×C2

NOTE

If Student has 4 columns and 10 rows, and Course has 3 columns and 5 rows, the Cartesian Product will have:

  • Degree=4+3=7 columns
  • Cardinality=10×5=50 rows

2. Equi-Join and Natural Join

2.1 Equi-Join

An Equi-Join is a join condition that specifies an equality comparison (=) on the common key attribute(s) between the two tables.

Syntax

sql
SELECT table1.col1, table2.col2, ...
FROM table1, table2
WHERE table1.common_col = table2.common_col;

Using Table Aliases

Aliases provide concise, readable shorthand for table names:

sql
SELECT S.RollNo, S.Name, C.CourseName, C.Fee
FROM Student S, Course C
WHERE S.CourseId = C.CourseId;

2.2 Natural Join

A Natural Join is a special type of Equi-Join where the join occurs automatically on all attributes that share the same name and domain across both tables, and the duplicate common column is eliminated from the result set.

sql
SELECT * FROM Student NATURAL JOIN Course;
Degree(Natural Join)=D1+D2K(where K is number of common attributes)

3. Two-Table Query Walkthrough

Consider two relational tables:

Table DOCTOR (D1=4,C1=4)

DocIDDocNameDepartmentOPD_Fee
D101Dr. VermaCardiology800
D102Dr. SharmaNeurology1000
D103Dr. PatelPediatrics600
D104Dr. SenCardiology900

Table PATIENT (D2=4,C2=5)

PCodePatientNameDocIDAdmitDate
P01RohanD1012026-02-10
P02TanyaD1022026-02-12
P03KabirD1012026-02-14
P04AanyaD1032026-02-15
P05MeeraD1012026-02-18

Sample Board Queries on DOCTOR and PATIENT:

Query 1: Display Patient Name, Doctor Name, and Department for all patients.

sql
SELECT P.PatientName, D.DocName, D.Department
FROM Patient P, Doctor D
WHERE P.DocID = D.DocID;

Query 2: Display Patient Name and OPD Fee for patients admitted under the 'Cardiology' department.

sql
SELECT P.PatientName, D.OPD_Fee
FROM Patient P, Doctor D
WHERE P.DocID = D.DocID AND D.Department = 'Cardiology';

Query 3: Display Doctor Name and total number of patients admitted under each doctor.

sql
SELECT D.DocName, COUNT(P.PCode) AS TotalPatients
FROM Doctor D, Patient P
WHERE D.DocID = P.DocID
GROUP BY D.DocName;

4. DDL vs DML Review

SQL statements are divided into categorized sub-languages based on their functional purpose:

mermaid
graph TD
    SQL["SQL Commands"] --> DDL["DDL (Data Definition Language)<br><i>Schema & Structure</i>"]
    SQL --> DML["DML (Data Manipulation Language)<br><i>Row Data & Content</i>"]
    SQL --> TCL["TCL (Transaction Control)<br><i>COMMIT, ROLLBACK</i>"]
    
    DDL --> C["CREATE TABLE"]
    DDL --> A["ALTER TABLE"]
    DDL --> D["DROP TABLE"]
    
    DML --> S["SELECT"]
    DML --> I["INSERT INTO"]
    DML --> U["UPDATE ... SET"]
    DML --> DEL["DELETE FROM"]

Essential DDL & DML Commands Cheat Sheet

Command TypeCommandPurposeExample Syntax
DDLCREATE TABLEDefines new table structureCREATE TABLE Item (ItemId INT PRIMARY KEY, Name VARCHAR(30), Price DECIMAL(8,2));
DDLALTER TABLE ... ADDAdds a new columnALTER TABLE Item ADD Category VARCHAR(20);
DDLALTER TABLE ... MODIFYModifies column data typeALTER TABLE Item MODIFY Name VARCHAR(50);
DDLALTER TABLE ... DROPRemoves an existing columnALTER TABLE Item DROP COLUMN Category;
DDLDROP TABLEDeletes entire table and structureDROP TABLE Item;
DMLINSERT INTOAdds new row(s)INSERT INTO Item VALUES (101, 'Mouse', 450.00);
DMLUPDATE ... SETModifies existing dataUPDATE Item SET Price = Price * 1.10 WHERE ItemId = 101;
DMLDELETE FROMDeletes row(s)DELETE FROM Item WHERE ItemId = 101;

5. Board Exam Traps: DELETE vs DROP

CAUTION

  • DELETE FROM TableName; DML Command: Deletes all data rows from the table, but the table schema/structure remains intact.
  • DROP TABLE TableName; DDL Command: Permanently removes both the table schema definition AND all stored data from the database.
  • ALTER TABLE TableName DROP COLUMN Col; DDL Command: Removes an attribute column from the table definition.

6. Board Examination Join Drills

Question 1 (CBSE 2023 Paper)

Consider two tables STAFF with 5 columns and 8 rows, and SALARY_GRADE with 3 columns and 4 rows.

  1. What will be the Degree and Cardinality of the Cartesian product of STAFF and SALARY_GRADE?
  2. What will be the Degree of an Equi-join on Grade?

Answers:

  1. Degree=5+3=8, Cardinality=8×4=32.
  2. In an Equi-join where columns are preserved or referenced, Degree=5+3=8. (If Natural Join, Degree=81=7).

Flügel Information Practices Academic Treatise (CBSE Code 065) • Contact Editorial TeamPrivacy Policy