SQL (Structured Query Language) is a robust tool for managing relational databases. It allows us to store, retrieve, and manipulate data efficiently, making it indispensable for database management and analysis. One common requirement is to fetch the latest records in a one-to-many relationship, a scenario where each parent record in one table is linked to multiple child records in another.
In this article, we will explain how to retrieve the last records in a one-to-many relationship using SQL joins, along with practical examples.
Understanding Relationships in SQL
Relationships in SQL refer to the associations or connections between tables in a relational database. These relationships are established using foreign keys, which are columns in a table that refer to the primary key in another table. Relationships help organize and structure data, allowing for efficient data retrieval and maintaining data integrity.
There are different types of relationships: one-to-one, one-to-many, many-to-many, and self-referencing.
Relationships in SQL1. One-to-One Relationship in SQL
- Definition: Each record in Table A is associated with one and only one record in Table B, and vice versa.
- Setup: Include a foreign key in one of the tables that references the primary key of the other table.
- For example: Tables 'users' and 'user_profiles', where each user has a single corresponding profile.
Table Creation Queries
CREATE TABLE users (
user_id INT PRIMARY KEY,
username VARCHAR(50)
);
CREATE TABLE user_profiles (
profile_id INT PRIMARY KEY,
user_id INT UNIQUE,
profile_data VARCHAR(255),
FOREIGN KEY (user_id) REFERENCES users(user_id)
);
Data Insertion Queries
-- Insert data into Table A
INSERT INTO TableA (user_id, username) VALUES
(1, 'ramesh'),
(2, 'riya'),
(3, 'akhil');
-- Insert data into Table B
INSERT INTO TableB (profile_id, user_id, profile_data) VALUES
('p01', 1, 'xyz'),
('p02', 2, 'abc'),
('p03', 3, 'gfg');
Output
one-to-one relatonship2. One-to-Many Relationship in SQL
- Definition: Each record in Table A can be associated with multiple records in Table B, but each record in Table B is associated with only one record in Table A.
- Setup: Include a foreign key in the "many" side table (Table B) that references the primary key of the "one" side table (Table A).
- For example: Tables 'departments' and 'employees', where each department can have multiple employees, but each employee belongs to one department.
Table Creation Queries
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
Data Insertion Queries
-- Insert data into Departments Table
INSERT INTO Departments (department_id, department_name) VALUES
('d1', 'technical'),
('d2', 'accounts'),
('d3', 'pr'),
('d4', 'product management');
-- Insert data into Employees Table
INSERT INTO Employees (employee_id, employee_name, department_id) VALUES
('e01', 'Ramesh', 'd3'),
('e02', 'Riya', 'd1'),
('e03', 'Neha', 'd2'),
('e04', 'Mayank', 'd1'),
('e05', 'Kritila', 'd4'),
('e06', 'Anuj', 'd4'),
('e07', 'Sam', 'd1'),
('e08', 'Gurpreet', 'd2');
Output
One-to-many relationship3. Many-to-Many Relationship in SQL
- Definition: Each record in Table A can be associated with multiple records in Table B, and vice versa.
- Setup: Create an intermediate table (also known as a junction or linking table) that contains foreign keys referencing both related tables.
- For example: Tables students and courses, where each student can enroll in multiple courses, and each course can have multiple students.
Table Creation Queries
CREATE TABLE students (
student_id INT PRIMARY KEY,
student_name VARCHAR(50)
);
CREATE TABLE courses (
course_id INT PRIMARY KEY,
course_name VARCHAR(50)
);
CREATE TABLE student_courses (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
1. STUDENTS
INSERT INTO Students (student_id, student_name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');
2. COURSES
INSERT INTO Courses (course_id, course_name) VALUES
(101, 'Mathematics'),
(102, 'History'),
(103, 'Computer Science');
3. STUDENT COURSES
INSERT INTO Student_Courses (student_id, course_id) VALUES
(1, 101),
(1, 102),
(2, 102);
Output
Many-to-many relationship4. Self-Referencing Relationship in SQL
Definition: A table has a foreign key that references its primary key.
Setup: Include a foreign key column in the same table that references its primary key.
For example : A table employees with a column manager_id referencing the same table's employee_id.
Table Creation Queries
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(50),
manager_id INT,
FOREIGN KEY (manager_id) REFERENCES employees(employee_id)
);
Data Insertion Queries
INSERT INTO Employees (employee_id, employee_name, manager_id) VALUES
(1, 'Alice', NULL),
(123, 'Bob', 1),
(3, 'Charlie', 1);
Output
Self-Referencing RelationshipSQL joins
A join is a mechanism that combines rows from two or more tables based on a related column between them. The purpose of using joins is to retrieve data from multiple tables in a single result set. The common columns used for joining tables are usually primary and foreign keys. There are several types of joins in SQL
SQL joinsConsider the following database with the Employee and Department tables:
Database1. INNER JOIN
The INNER JOIN keyword returns only the rows where there is a match in both tables based on the specified condition, effectively filtering out non-matching rows from the result set.
Syntax
SELECT *
FROM table1
INNER JOIN table2 ON table1.column = table2.column;
Example:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
Output
INNER JOIN outputExplanation:
The SQL query selects employee_id, employee_name, and department_name from employees and departments tables, joining them on department_id. It retrieves information about employees and their corresponding department names.
2. LEFT (OUTER) JOIN
Returns all rows from the left table and the matching rows from the right table. If there is no match, NULL values are returned for columns from the right table.
Syntax
SELECT *
FROM table1
LEFT JOIN table2 ON table1.column = table2.column;
Example:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;
Output
LEFT (OUTER) JOIN outputExplanation:
The SQL query uses a LEFT JOIN to retrieve employee_id, employee_name, and department_name from employees and departments, showing all employees and their associated department names, including those without a match.
3. RIGHT (OUTER) JOIN
Returns all rows from the right table and the matching rows from the left table. If there is no match, NULL values are returned for columns from the left table.
Syntax
SELECT *
FROM table1
RIGHT JOIN table2 ON table1.column = table2.column;
Example:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;
Output
RIGHT (OUTER) JOIN outputExplanation:
The SQL query employs a RIGHT JOIN to fetch employee_id, employee_name, and department_name from employees and departments, displaying all departments and their corresponding employees, including unmatched departments without employees.
4. FULL (OUTER) JOIN
Returns all rows when there is a match in either the left or the right table. If there is no match, NULL values are returned for columns from the non-matching table.
Syntax
SELECT *
FROM table1
FULL JOIN table2 ON table1.column = table2.column;
Example:
SELECT employees.employee_id, employees.employee_name, departments.department_name
FROM employees
FULL JOIN departments ON employees.department_id = departments.department_id;
Output
FULL (OUTER) JOINExplanation:
The SQL query employs a FULL JOIN to retrieve employee_id, employee_name, and department_name from employees and departments, displaying all records from both tables, matching on department_id and including unmatched rows from both tables.
How to Select the Last Records
To select the last records in a one-to-many relationship, identifying the most recent entry for each parent record is crucial. Consider the below given database for performing the following methods:
DatabaseHere are several methods to accomplish this:
1. Using Subquery with 'LIMIT' and 'ORDER BY'
The above SQL query retrieves records from a parent-child relationship in a SQL database. It employs a subquery to find the latest child record for each parent based on the 'created_at' timestamp.
Example:
SELECT *
FROM parent p
JOIN child c ON p.parent_id = c.parent_id
WHERE c.child_id = (
SELECT child_id
FROM child
WHERE parent_id = p.parent_id
ORDER BY created_at DESC
LIMIT 1
);
Output
Using Subquery with LIMIT and ORDER BY outputExplanation:
The main query then joins the parent and child tables, filtering the results to include only the rows where the child ID matches the one obtained from the subquery. This effectively selects the most recent child record for each parent in a one-to-many relationship, ensuring only the latest child entry per parent is included in the final result set.
2. Using Subquery with MAX
The given SQL query retrieves the latest records from a one-to-many relationship between a "parent" and "child" table in a SQL database. It does so by joining the "parent" and "child" tables based on the common parent_id, and additionally using a subquery to identify the maximum created_at timestamp for each parent_id in the "child" table.
Example
SELECT p.*, c.*
FROM parent p
JOIN child c ON p.parent_id = c.parent_id
JOIN (
SELECT parent_id, MAX(created_at) AS max_created_at
FROM child
GROUP BY parent_id
) AS latest_child ON c.parent_id = latest_child.parent_id
AND c.created_at = latest_child.max_created_at;
Output
Using Subquery with MAX outputExplanation:
The main join condition includes a comparison with the latest_child subquery, ensuring that only the rows with the maximum created_at for each parent_id are selected. The result set includes all columns from both the "parent" and "child" tables for the latest records in the one-to-many relationship.
3. Using Correlated Subquery
In the given SQL query using a correlated subquery, it selects records from a one-to-many relationship between a parent and child table
Example
SELECT p.*, c.*
FROM parent p
JOIN child c ON p.parent_id = c.parent_id
WHERE c.created_at = (
SELECT MAX(created_at)
FROM child
WHERE parent_id = p.parent_id
);
Output
Using correlated subqueryExplanation:
The query retrieves all columns from the parent and child tables for rows where the child's creation timestamp is the maximum within each group of children sharing the same parent. Essentially, it returns the latest child record for each parent based on the "created_at" timestamp, providing a concise way to obtain the most recent child entry for each parent in the relationship
4. Using ROW_NUMBER() Window Function
In the given example SQL query, the ROW_NUMBER() window function is used to assign a unique row number to each record in the result set based on the descending order of the "created_at" column within each partition defined by the "parent_id."
Example
WITH RankedChild AS (
SELECT
p.*,
c.*,
ROW_NUMBER() OVER (PARTITION BY p.parent_id ORDER BY c.created_at DESC) AS rn
FROM parent p
JOIN child c ON p.parent_id = c.parent_id
)
SELECT *
FROM RankedChild
WHERE rn = 1;
Output
Using ROW_NUMBER() Window Function outputExplanation:
The query joins the "parent" and "child" tables on the "parent_id" and selects all columns from both tables along with the calculated row number. The final output, obtained by filtering rows where the row number (rn) is equal to 1, retrieves the latest records for each parent in a one-to-many relationship, effectively selecting the most recently created child record for each parent.
5. Using LEFT JOIN and IS NULL
The given SQL query retrieves records from a one-to-many relationship between a "parent" table (denoted as p) and a "child" table (denoted as c). It uses a LEFT JOIN to match rows from the parent table with corresponding rows in the child table based on the parent_id.
Example
SELECT p.*, c.*
FROM parent p
LEFT JOIN child c ON p.parent_id = c.parent_id
LEFT JOIN child c2 ON p.parent_id = c2.parent_id AND c.created_at < c2.created_at
WHERE c2.parent_id IS NULL;
Output
Using LEFT JOIN and IS NULL outputExplanation:
Additionally, it employs a self-join on the child table (c2) to identify the latest records by comparing their created_at timestamps. The WHERE clause ensures that only rows with no later child records (c2.parent_id IS NULL) are included, effectively selecting the latest records for each parent in the one-to-many relationship.
Conclusion
In conclusion, Selecting the last records in a one-to-many relationship using SQL joins can be approached in various ways. We can use subqueries with LIMIT
and ORDER BY
, use MAX
in subqueries, employ correlated subqueries, utilize the ROW_NUMBER
()
window function, or employ a LEFT JOIN
with a condition. Each method aims to retrieve the latest records from the related table, whether it's based on timestamps, maximum values, or window functions. The choice depends on our specific needs, database structure, and performance considerations.
Similar Reads
SQL Tutorial Structured Query Language (SQL) is the standard language used to interact with relational databases. Mainly used to manage data. Whether you want to create, delete, update or read data, SQL provides the structure and commands to perform these operations. Widely supported across various database syst
8 min read
Basics
What is SQL?Structured Query Language (SQL) is the standard language used to interact with relational databases. Allows users to store, retrieve, update, and manage data efficiently through simple commands. Known for its user-friendly syntax and powerful capabilities, SQL is widely used across industries.How Do
6 min read
SQL Data TypesSQL data types define the kind of data a column can store, dictating how the database manages and interacts with the data. Each data type in SQL specifies a set of allowed values, as well as the operations that can be performed on the values.SQL data types are broadly categorized into several groups
4 min read
SQL OperatorsSQL operators are symbols or keywords used to perform operations on data in SQL queries. These operations can include mathematical calculations, data comparisons, logical manipulations, other data-processing tasks. Operators help in filtering, calculating, and updating data in databases, making them
5 min read
SQL Commands | DDL, DQL, DML, DCL and TCL CommandsSQL commands are the fundamental building blocks for communicating with a database management system (DBMS). It is used to interact with the database with some operations. It is also used to perform specific tasks, functions, and queries of data. SQL can perform various tasks like creating a table,
7 min read
SQL Database OperationsSQL databases or relational databases are widely used for storing, managing and organizing structured data in a tabular format. These databases store data in tables consisting of rows and columns. SQL is the standard programming language used to interact with these databases. It enables users to cre
3 min read
SQL CREATE TABLEIn SQL, creating a table is one of the most essential tasks for structuring your database. The CREATE TABLE statement defines the structure of the database table, specifying column names, data types, and constraints such as PRIMARY KEY, NOT NULL, and CHECK. Mastering this statement is fundamental to
5 min read
Queries & Operations
SQL SELECT QueryThe SQL SELECT query is one of the most frequently used commands to retrieve data from a database. It allows users to access and extract specific records based on defined conditions, making it an essential tool for data management and analysis. In this article, we will learn about SQL SELECT stateme
4 min read
SQL INSERT INTO StatementThe SQL INSERT INTO statement is one of the most essential commands for adding new data into a database table. Whether you are working with customer records, product details or user information, understanding and mastering this command is important for effective database management. How SQL INSERT I
6 min read
SQL UPDATE StatementIn SQL, the UPDATE statement is used to modify existing records in a table. Whether you are updating a single record or multiple records at once, SQL provides the necessary functionality to make these changes. Whether you are working with a small dataset or handling large-scale databases, the UPDATE
6 min read
SQL DELETE StatementThe SQL DELETE statement is an essential command in SQL used to remove one or more rows from a database table. Unlike the DROP statement, which removes the entire table, the DELETE statement removes data (rows) from the table retaining only the table structure, constraints, and schema. Whether you n
4 min read
SQL | WHERE ClauseThe SQL WHERE clause allows filtering of records in queries. Whether you are retrieving data, updating records, or deleting entries from a database, the WHERE clause plays an important role in defining which rows will be affected by the query. Without WHERE clause, SQL queries would return all rows
4 min read
SQL | AliasesIn SQL, aliases are temporary names assigned to columns or tables for the duration of a query. They make the query more readable, especially when dealing with complex queries or large datasets. Aliases help simplify long column names, improve query clarity, and are particularly useful in queries inv
4 min read
SQL Joins & Functions
SQL Joins (Inner, Left, Right and Full Join)SQL joins are fundamental tools for combining data from multiple tables in relational databases. For example, consider two tables where one table (say Student) has student information with id as a key and other table (say Marks) has information about marks of every student id. Now to display the mar
4 min read
SQL CROSS JOINIn SQL, the CROSS JOIN is a unique join operation that returns the Cartesian product of two or more tables. This means it matches each row from the left table with every row from the right table, resulting in a combination of all possible pairs of records. In this article, we will learn the CROSS JO
3 min read
SQL | Date Functions (Set-1)SQL Date Functions are essential for managing and manipulating date and time values in SQL databases. They provide tools to perform operations such as calculating date differences, retrieving current dates and times and formatting dates. From tracking sales trends to calculating project deadlines, w
5 min read
SQL | String functionsSQL String Functions are powerful tools that allow us to manipulate, format, and extract specific parts of text data in our database. These functions are essential for tasks like cleaning up data, comparing strings, and combining text fields. Whether we're working with names, addresses, or any form
7 min read
Data Constraints & Aggregate Functions
SQL NOT NULL ConstraintIn SQL, constraints are used to enforce rules on data, ensuring the accuracy, consistency, and integrity of the data stored in a database. One of the most commonly used constraints is the NOT NULL constraint, which ensures that a column cannot have NULL values. This is important for maintaining data
3 min read
SQL PRIMARY KEY ConstraintThe PRIMARY KEY constraint in SQL is one of the most important constraints used to ensure data integrity in a database table. A primary key uniquely identifies each record in a table, preventing duplicate or NULL values in the specified column(s). Understanding how to properly implement and use the
5 min read
SQL Count() FunctionIn the world of SQL, data analysis often requires us to get counts of rows or unique values. The COUNT() function is a powerful tool that helps us perform this task. Whether we are counting all rows in a table, counting rows based on a specific condition, or even counting unique values, the COUNT()
7 min read
SQL SUM() FunctionThe SUM() function in SQL is one of the most commonly used aggregate functions. It allows us to calculate the total sum of a numeric column, making it essential for reporting and data analysis tasks. Whether we're working with sales data, financial figures, or any other numeric information, the SUM(
5 min read
SQL MAX() FunctionThe MAX() function in SQL is a powerful aggregate function used to retrieve the maximum (highest) value from a specified column in a table. It is commonly employed for analyzing data to identify the largest numeric value, the latest date, or other maximum values in various datasets. The MAX() functi
4 min read
AVG() Function in SQLSQL is an RDBMS system in which SQL functions become very essential to provide us with primary data insights. One of the most important functions is called AVG() and is particularly useful for the calculation of averages within datasets. In this, we will learn about the AVG() function, and its synta
4 min read
Advanced SQL Topics
SQL SubqueryA subquery in SQL is a query nested within another SQL query. It allows you to perform complex filtering, aggregation, and data manipulation by using the result of one query inside another. Subqueries are often found in the WHERE, HAVING, or FROM clauses and are supported in SELECT, INSERT, UPDATE,
5 min read
Window Functions in SQLSQL window functions are essential for advanced data analysis and database management. It is a type of function that allows us to perform calculations across a specific set of rows related to the current row. These calculations happen within a defined window of data and they are particularly useful
6 min read
SQL Stored ProceduresStored procedures are precompiled SQL statements that are stored in the database and can be executed as a single unit. SQL Stored Procedures are a powerful feature in database management systems (DBMS) that allow developers to encapsulate SQL code and business logic. When executed, they can accept i
7 min read
SQL TriggersA trigger is a stored procedure in adatabase that automatically invokes whenever a special event in the database occurs. By using SQL triggers, developers can automate tasks, ensure data consistency, and keep accurate records of database activities. For example, a trigger can be invoked when a row i
7 min read
SQL Performance TuningSQL performance tuning is an essential aspect of database management that helps improve the efficiency of SQL queries and ensures that database systems run smoothly. Properly tuned queries execute faster, reducing response times and minimizing the load on the serverIn this article, we'll discuss var
8 min read
SQL TRANSACTIONSSQL transactions are essential for ensuring data integrity and consistency in relational databases. Transactions allow for a group of SQL operations to be executed as a single unit, ensuring that either all the operations succeed or none of them do. Transactions allow us to group SQL operations into
8 min read
Database Design & Security
Introduction of ER ModelThe Entity-Relationship Model (ER Model) is a conceptual model for designing a databases. This model represents the logical structure of a database, including entities, their attributes and relationships between them. Entity: An objects that is stored as data such as Student, Course or Company.Attri
10 min read
Introduction to Database NormalizationNormalization is an important process in database design that helps improve the database's efficiency, consistency, and accuracy. It makes it easier to manage and maintain the data and ensures that the database is adaptable to changing business needs.Database normalization is the process of organizi
6 min read
SQL InjectionSQL Injection is a security flaw in web applications where attackers insert harmful SQL code through user inputs. This can allow them to access sensitive data, change database contents or even take control of the system. It's important to know about SQL Injection to keep web applications secure.In t
7 min read
SQL Data EncryptionIn todayâs digital era, data security is more critical than ever, especially for organizations storing the personal details of their customers in their database. SQL Data Encryption aims to safeguard unauthorized access to data, ensuring that even if a breach occurs, the information remains unreadab
5 min read
SQL BackupIn SQL Server, a backup, or data backup is a copy of computer data that is created and stored in a different location so that it can be used to recover the original in the event of a data loss. To create a full database backup, the below methods could be used : 1. Using the SQL Server Management Stu
4 min read
What is Object-Relational Mapping (ORM) in DBMS?Object-relational mapping (ORM) is a key concept in the field of Database Management Systems (DBMS), addressing the bridge between the object-oriented programming approach and relational databases. ORM is critical in data interaction simplification, code optimization, and smooth blending of applicat
7 min read