0% found this document useful (0 votes)
8 views

joins

The document outlines the creation of two SQL tables, 'book' and 'author', along with sample data insertion for each. It provides examples of various SQL join operations, including simple join, inner join, left join, right join, full join, and natural join, demonstrating how to retrieve combined data from both tables. The output of each join operation is also presented, showcasing the relationships between books and their authors.

Uploaded by

Saripilli Vasu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

joins

The document outlines the creation of two SQL tables, 'book' and 'author', along with sample data insertion for each. It provides examples of various SQL join operations, including simple join, inner join, left join, right join, full join, and natural join, demonstrating how to retrieve combined data from both tables. The output of each join operation is also presented, showcasing the relationships between books and their authors.

Uploaded by

Saripilli Vasu
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

CREATE TABLE book (

book_id INT,
title VARCHAR(25),
author_id INT
);

INSERT INTO book VALUES (1, 'The Great Gatsby', 101);


INSERT INTO book VALUES (2, 'To Kill a Mockingbird', 102);
INSERT INTO book VALUES (3, 'Moby-Dick', 103);
INSERT INTO book VALUES (4, 'The Catcher in the Rye', 104);
INSERT INTO book VALUES (5, 'Greek Mythology', NULL);

CREATE TABLE author (


author_id INT,
name VARCHAR(25)
);

INSERT INTO author VALUES (101, 'F. Scott Fitzgerald');


INSERT INTO author VALUES (102, 'Harper Lee');
INSERT INTO author VALUES (103, 'Herman Melville');
INSERT INTO author VALUES (104, 'J.D. Salinger');
INSERT INTO author VALUES (105, 'Walt Whitman');

SQL> select * from book;

BOOK_ID TITLE AUTHOR_ID


---------- ------------------------- ----------
1 The Great Gatsby 101
2 To Kill a Mockingbird 102
3 Moby-Dick 103
4 The Catcher in the Rye 104
5 Greek Mythology

SQL> select * from author;

AUTHOR_ID NAME
---------- -------------------------
101 F. Scott Fitzgerald
102 Harper Lee
103 Herman Melville
104 J.D. Salinger
105 Walt Whitman

example for simple join:-

SELECT title,name FROM book JOIN author ON book.author_id = author.author_id;


TITLE NAME
------------------------- -------------------------
The Great Gatsby F. Scott Fitzgerald
To Kill a Mockingbird Harper Lee
Moby-Dick Herman Melville
The Catcher in the Rye J.D. Salinger

INNER JOIN example:-


SELECT title,name FROM book INNER JOIN author ON book.author_id = author.author_id;
output:-
TITLE NAME
------------------------- -------------------------
The Great Gatsby F. Scott Fitzgerald
To Kill a Mockingbird Harper Lee
Moby-Dick Herman Melville
The Catcher in the Rye J.D. Salinger

LEFT Join example:-


SELECT title,name FROM book LEFT JOIN author ON book.author_id = author.author_id;
output:-
TITLE NAME
------------------------- -------------------------
The Great Gatsby F. Scott Fitzgerald
To Kill a Mockingbird Harper Lee
Moby-Dick Herman Melville
The Catcher in the Rye J.D. Salinger
Greek Mythology

RIGHT JOIN example:-


SELECT title, name FROM book RIGHT JOIN author ON book.author_id =
author.author_id;
ouput:
TITLE NAME
------------------------- -------------------------
The Great Gatsby F. Scott Fitzgerald
To Kill a Mockingbird Harper Lee
Moby-Dick Herman Melville
The Catcher in the Rye J.D. Salinger
Walt Whitman

FULL JOIN example:-


SELECT title, name FROM book FULL JOIN author ON book.author_id = author.author_id;
output:-
TITLE NAME
------------------------- -------------------------
The Great Gatsby F. Scott Fitzgerald
To Kill a Mockingbird Harper Lee
Moby-Dick Herman Melville
The Catcher in the Rye J.D. Salinger
Walt Whitman Greek Mythology
6 rows selected.

NATURAL JOIN example:-


SELECT title,name FROM book NATURAL JOIN author;
output:-
TITLE NAME
------------------------- -------------------------
The Great Gatsby F. Scott Fitzgerald
To Kill a Mockingbird Harper Lee
Moby-Dick Herman Melville
The Catcher in the Rye J.D. Salinger

You might also like