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

Database - Lec4 - Joins and Subqueries in SQL

Uploaded by

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

Database - Lec4 - Joins and Subqueries in SQL

Uploaded by

usman
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Joins and Subqueries in

SQL
Module 1. Introduction to Database and Relational Models
SQL joins
SQL joins combine rows from two or more tables based on a related column.

Types of Joins:
1. INNER JOIN
2. LEFT (OUTER) JOIN
3. RIGHT (OUTER) JOIN
4. FULL (OUTER) JOIN
5. CROSS JOIN
Inner Join
The INNER JOIN selects records that have matching values in both tables.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Inner join
SELECT customers.name, orders.product
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id;

customer_i name country order_id customer_id product name product


d
101 1 Laptop John Laptop
1 John USA
102 2 Phone John Tablet
2 Sarah UK
103 1 Tablet Sarah Phone
3 David Canada
Left (Outer) Join
The LEFT JOIN returns all records from the left table, and the matched records
from the right table. If there is no match, the result is NULL on the right side.

SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Left join
SELECT customers.name, orders.product
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id;

customer_i name country order_id customer_id product name product


d
101 1 Laptop John Laptop
1 John USA
102 2 Phone John Tablet
2 Sarah UK
103 1 Tablet Sarah Phone
3 David Canada
David NULL
Right (Outer) Join
The RIGHT JOIN returns all records from the right table, and the matched
records from the left table. If there is no match, the result is NULL on the left
side.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Right join
SELECT customers.name, orders.product
FROM customers
RIGHT JOIN orders
ON customers.customer_id = orders.customer_id;

customer_i name country order_id customer_id product


d
101 1 Laptop
1 John USA
102 2 Phone
2 Sarah UK
103 1 Tablet
3 David Canada
Full (Outer) Join
The FULL JOIN returns all records when there is a match in either left or right
table. If there is no match, the result is NULL in columns where there is no match.

SELECT column_name(s)
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
Cross Join
The CROSS JOIN returns the Cartesian product of two tables, i.e., it pairs every
row from the first table with every row from the second table.

SELECT column_name(s)
FROM table1
CROSS JOIN table2;
Subqueries
A subquery (also known as an inner query or nested query) is a query embedded
inside another query.
1. In WHERE clause
SELECT name
FROM customers
WHERE customer_id IN (SELECT customer_id FROM orders);
2. In FROM clause
SELECT AVG(order_count)
FROM (SELECT COUNT(*) as order_count FROM orders GROUP BY
customer_id);
WITH clause
The WITH clause in SQL, also known as Common Table Expressions (CTE),
allows you to define temporary result sets that can be referred to within a SELECT,
INSERT, UPDATE, or DELETE statement.
It helps to make complex queries more readable and easier to maintain by
breaking them into smaller, manageable parts.
employee_id name manager_id
WITH clause 1 Alice NULL

WITH recursive_cte AS ( 2 Bob 1


SELECT employee_id, name, manager_id 3 Charlie 2
FROM employees
WHERE manager_id IS NULL 4 David 2
UNION ALL
SELECT e.employee_id, e.name, e.manager_id
FROM employees e
INNER JOIN recursive_cte r
ON e.manager-id = r.employee_id
)
SELECT employee_id, name, manager_id
FROM recursive_cte;

You might also like