0% found this document useful (0 votes)
3 views27 pages

Sql Join

The document provides an overview of SQL Joins, explaining their purpose in combining data from multiple tables for efficient data retrieval and complex queries. It details various types of joins including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and Natural Join, along with their syntax and examples. Additionally, it covers SQL Views, their creation, and the distinction between updatable and non-updatable views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views27 pages

Sql Join

The document provides an overview of SQL Joins, explaining their purpose in combining data from multiple tables for efficient data retrieval and complex queries. It details various types of joins including INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, and Natural Join, along with their syntax and examples. Additionally, it covers SQL Views, their creation, and the distinction between updatable and non-updatable views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

SQL Joins

• Enabling the combination of data from multiple tables.


• Efficient data retrieval
• Generating meaningful observations and solving
complex business queries.
• It can access data from multiple tables simultaneously
using common key values shared across different tables.
SQL Joins – Example
Query:
SELECT s.roll_no, s.name, s.address, s.phone, s.age, sc.course_id
FROM Student s
JOIN StudentCourse sc ON s.roll_no = sc.roll_no;
ROLL_NO NAME ADDRESS PHONE AGE COURSE_ID
1 HARSH DELHI XXXXXXXXXX 18 1
2 PRATIK BIHAR XXXXXXXXXX 19 2
3 RIYANKA SILGURI XXXXXXXXXX 20 2
4 DEEP RAMNAGAR XXXXXXXXXX 18 3
5 SAPTARHI KOLKATA XXXXXXXXXX 19 1
Types of JOIN in SQL

• INNER JOIN
• LEFT JOIN
• RIGHT JOIN
• FULL JOIN
• Natural Join
SQL INNER JOIN
• The INNER JOIN keyword selects all rows from both the
tables as long as the condition is satisfied.
• Syntax
• SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
INNER JOIN table2
ON table1.matching_column = table2.matching_column;
INNER JOIN Example
• Query:
• SELECT StudentCourse.COURSE_ID, Student.NAME, Student.AGE
FROM Student
INNER JOIN StudentCourse
ON Student.ROLL_NO = StudentCourse.ROLL_NO;
SQL LEFT JOIN
• LEFT JOIN returns all the rows of the table on the left side of the join
and matches rows for the table on the right side of the join.
• Syntax
• SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
LEFT JOIN table2
ON table1.matching_column = table2.matching_column;
LEFT JOIN Example
• SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
LEFT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SQL RIGHT JOIN
• RIGHT JOIN returns all the rows of the table on the right side of the
join and matching rows for the table on the left side of the join.

Syntax

SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
RIGHT JOIN table2
ON table1.matching_column = table2.matching_column;
RIGHT JOIN Example
• Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
RIGHT JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
SQL FULL JOIN
• FULL JOIN creates the result-set by combining results of both LEFT
JOIN and RIGHT JOIN. The result-set will contain all the rows from
both tables.
Syntax
SELECT table1.column1,table1.column2,table2.column1,....
FROM table1
FULL JOIN table2
ON table1.matching_column = table2.matching_column;
FULL JOIN Example
• Query:
SELECT Student.NAME,StudentCourse.COURSE_ID
FROM Student
FULL JOIN StudentCourse
ON StudentCourse.ROLL_NO = Student.ROLL_NO;
FULL JOIN Example - Output
NAME COURSE_ID
HARSH 1
PRATIK 2
RIYANKA 2
DEEP 3
SAPTARHI 1
DHANRAJ NULL
ROHIT NULL
NIRAJ NULL
NULL 4
NULL 5
NULL 4
SQL Natural Join (?)
• Natural join can join tables based on the common columns in the
tables being joined. A natural join returns all rows by matching values
in common columns having same name and data type of columns and
that column should be present in both tables.
• Both table must have at least one common column with same
column name and same data type.
• The two table are joined using Cross join.
• DBMS will look for a common column with same name and data
type. Tuples having exactly same values in common columns are kept
in result.
Natural join Example

Employee
Department
Emp_na
Emp_id Dept_id Dept_id Dept_name
me
10 IT
1 Ram 10
30 HR
2 Jon 30 40 TIS

3 Bob 50
Problem: Find all Employees and their respective departments.
• Solution Query: (Employee) ? (Department)

Emp_id Emp_name Dept_id Dept_id Dept_name

1 Ram 10 10 IT

2 Jon 30 30 HR

Employee data Department data


SQL Views
• Type of virtual table that simplifies how users interact with data
across one or more tables.
• Does not store data on disk; instead, it dynamically retrieves data
based on a pre-defined query each time it’s accessed.

• A view in SQL is a saved SQL query that acts as a virtual table. It can
fetch data from one or more tables and present it in a customized
format, allowing developers to:
• Simplify Complex Queries
• Enhance Security
• Present Data Flexibly
EXAMPLE
• Student Detail table
EXAMPLE
• Student Marks Table
CREATE A VIEW
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2…..
FROM table_name
WHERE condition;
Parameters:
•view_name: Name for the View
•table_name: Name of the table
•condition: Condition to select rows
Creating View From a Single
Table
• CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS FROM StudentDetails WHERE S_ID < 5;

SELECT * FROM DetailsView;


Creating View From a Single
Table
• CREATE VIEW StudentNames AS SELECT S_ID, NAME FROM
StudentDetails ORDER BY NAME;
• SELECT * FROM StudentNames;
Creating View From Multiple Tables

• CREATE VIEW MarksView AS SELECT StudentDetails.NAME,


StudentDetails.ADDRESS, StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
SELECT * FROM MarksView;
Updatable Views
• These are views that you can use to INSERT, UPDATE, or DELETE
records in the base table(s). However, certain conditions must be met
for a view to be updatable.
• Characteristics of Updatable Views:
• Based on a single table (usually)
• No GROUP BY, DISTINCT, aggregate functions (e.g., SUM, AVG,
etc.).
• No JOINs (with rare exceptions in some RDBMSs).
• Must include the primary key of the base table.
• No subqueries or UNIONs.
Example:
• CREATE VIEW emp_view AS SELECT emp_id, name, department
FROM employees
WHERE department = 'Sales’;

• UPDATE emp_view SET name = 'John' WHERE emp_id = 101;


Non-Updatable Views
• These are views that cannot be used to directly modify data in the base
tables.
❌ Causes of Non-Updatability:
• JOINs
• GROUP BY
• DISTINCT
• Aggregate functions
• UNION/UNION ALL
• Subqueries
• Set operations
📌 Example:
• CREATE VIEW sales_summary AS
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy