Joins

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Research, understand and comprehend as well as give an example script about the topic SQL

joins and aggregate functions.

Joins

1) Define Joins

Joins are operations performed on relational databases to combine rows from two or

more tables based on related columns. Joins allow us to retrieve data from multiple

tables based on the relationships defined between them. By specifying the columns and

conditions for the join, we can create a new result set that includes data from the

combined tables.

2) Provide the different types of Joins and how it is being utilize

a) Inner Join: Returns only the matching rows between two tables based on the join

condition. It combines rows from both tables where the join condition is met.

b) Left Join (or Left Outer Join): Returns all the rows from the left table and the

matching rows from the right table based on the join condition. If there are no

matching rows in the right table, NULL values are returned.

c) Right Join (or Right Outer Join): Returns all the rows from the right table and the

matching rows from the left table based on the join condition. If there are no

matching rows in the left table, NULL values are returned.

d) Full Outer Join: Returns all the rows from both tables, matching rows where the join

condition is met, and NULL values where there is no match.


e) Cross Join (or Cartesian Join): Returns the Cartesian product of the two tables,

resulting in all possible combinations of rows between them.

3) Provide an example of each type

a) Inner Join Example:

SELECT Orders.OrderID, Customers.CustomerName

FROM Orders

INNER JOIN Customers

ON Orders.CustomerID = Customers.CustomerID;

b) Left Join Example:

SELECT Customers.CustomerName, Orders.OrderID

FROM Customers

LEFT JOIN Orders

ON Customers.CustomerID = Orders.CustomerID;

c) Right Join Example:

SELECT Orders.OrderID, Customers.CustomerName

FROM Orders

RIGHT JOIN Customers

ON Orders.CustomerID = Customers.CustomerID;
d) Full Outer Join example:

SELECT Customers.CustomerName, Orders.OrderID

FROM Customers

FULL OUTER JOIN Orders

ON Customers.CustomerID = Orders.CustomerID;

e) Cross Join Example:

SELECT Customers.CustomerName, Orders.OrderID

FROM Customers

CROSS JOIN Orders;

Aggregate Functions

1) Define aggregate functions

Aggregate functions are SQL functions that operate on a set of values and return a single

calculated value. These functions perform calculations across multiple rows and are

commonly used with the GROUP BY clause to calculate summarized data.

2) Provide the common aggregate functions

a) SUM: Calculates the sum of a column's values.

b) AVG: Calculates the average of a column's values.


c) COUNT: Returns the number of rows in a table or the number of non-null values in a

column.

d) MIN: Returns the minimum value from a column.

e) MAX: Returns the maximum value from a column.

3) Provide an example of each type

a) SUM Example:

SELECT SUM(Quantity) AS TotalQuantity

FROM Orders;

b) AVG Example:

SELECT AVG(Price) AS AveragePrice

FROM Products;

c) COUNT Example

SELECT COUNT(*) AS TotalCustomers

FROM Customers;

d) MIN Example

SELECT MIN(Price) AS LowestPrice

FROM Products;

e) MAX Example
SELECT MAX(Price) AS HighestPrice

FROM Products;

Additional:

1) Research and understand about BETWEEN

The BETWEEN operator is used to select values within a range. It is typically used in the

WHERE clause of a SQL query to specify a range of values to be included.

Example:

SELECT * FROM Products

WHERE Price BETWEEN 10 AND 50;

2) Research and understand about HAVING

The HAVING clause is used in conjunction with the GROUP BY clause to filter the result

set based on aggregated values. It is similar to the WHERE clause, but it operates on the

result of aggregate functions.

Example:

SELECT Category, AVG(Price) AS AveragePrice

FROM Products

GROUP BY Category

HAVING AVG(Price) > 100;


3) Research and understand about GROUP BY

The GROUP BY clause is used to group rows in a result set based on one or more

columns. It is often used in combination with aggregate functions to calculate summary

statistics for each group.

Example:

SELECT Category, COUNT(*) AS TotalProducts

FROM Products

GROUP BY Category;

You might also like