SQL
Retrieve Related Data with INNER JOIN
Combine data from two or more related tables using an INNER JOIN to fetch comprehensive records, such as posts with their associated author details.
SELECT
p.title AS post_title,
p.content,
u.username AS author_name,
u.email AS author_email
FROM
posts p
INNER JOIN
users u ON p.author_id = u.id
WHERE
p.published = TRUE;
How it works: This query fetches data from both the 'posts' and 'users' tables by using an INNER JOIN. It links posts to their authors using the 'author_id' in the 'posts' table and the 'id' in the 'users' table. The query retrieves the post's title and content, along with the author's username and email, but only for posts that are marked as 'published'.