SQL Session 4 Manual
SQL Session 4 Manual
Introduction
This document aims to provide a foundational understanding of working with multiple tables in
databases, focusing on database relationships. It's designed to serve as a preparatory guide for
students who are about to dive into these essential topics in database management systems.
1. One-to-Many Relationships
● Definition: This occurs when a record in one table can be associated with multiple records
in another table.
● Example: A single customer having multiple orders; here, the customer table has a one-
to-many relationship with the orders table.
2. Many-to-Many Relationships
● Definition: This exists when multiple records in a table are associated with multiple
records in another table.
● Example: Students and courses; a student can enroll in many courses, and a course can
have many students.
3. One-to-One Relationships
● Definition: This relationship exists when a single record in one table is associated with a
single record in another table.
● Example: Person and Passport; each person has at most one passport, and each passport
is assigned to at most one person. In this scenario, the person's record in the 'Person'
table will have a unique identifier (like a Social Security Number) that matches a unique
identifier in the 'Passport' table (like a Passport Number).
4. Many-to-One Relationships
● Definition: This relationship exists when multiple records in one table are associated with
a single record in another table.
● Example: Employees and Department; multiple employees can work in the same
department, but each employee belongs to only one department. In this case, many
employee records in the 'Employees' table will be linked to a single department record in
the 'Department' table through a department identifier.
1. INNER JOIN
● Usage: Returns all records from the left table (table1), and the matched records from the
right table (table2).
● Syntax Example: SELECT * FROM table1 LEFT JOIN table2 ON table1.column_name =
table2.column_name;
● Application: Useful when you want all records from the left table, regardless of whether
they have matches in the right table.
● Usage: Selects records that have matching records in either the left or right table.
● Syntax Example: SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column_name
= table2.column_name;
● Application: Use when you want to combine all records from both tables, with NULL in
place where there is no match.
Section 2: Subqueries
1. Definition
● A subquery is a query nested inside another query, such as SELECT, INSERT, UPDATE, or
DELETE. Also known as inner queries or nested queries.
2. Applications
● Used for complex queries, like when you want to compare values in a column with a set
of values returned by another query.
Types of Subqueries
1. Single-Row Subqueries
2. Multiple-Row Subqueries
3. Multiple-Column Subqueries
Correlated Subqueries
● A subquery that references one or more columns from the outer SQL query.
● Evaluated once for each row processed by the outer query.
● Example: SELECT * FROM table1 t1 WHERE EXISTS (SELECT 1 FROM table2 t2 WHERE t1.id
= t2.id);