AssignmentSQL
AssignmentSQL
SQL joins and subqueries allow you to specify queries that reclaim data from
related tables in a relational database. SQL joins and subqueries allow you to
request data from more than one table and bring about complex results.
SQL Joins:
Joins are used to merge rows from two or more tables where there exists a related
column between them. The four types of Joins are:
1. Inner Join: Data returns only rows where matching data exists in both tables.
2. Left Join: Data returns all rows from the left table and matched rows from the
right table.
In case of no match, the result shows NULL for the right table columns.
3. Right Join: Opposite to Left Join, it returns all rows from the right table and
matched rows from the left table.
5. Full Outer Join: Returns records where there is a match in either of the tables.
Where there is no match, NULL values are used for missing columns from either
side.
Subqueries
A subquery is a query within another query. It can be used to get information from
data to use in the main query or to filter results. It may visible in different clauses
like SELECT, FROM or WHERE.
Subquery in the WHERE clause: Which filters rows based on the outcome of
another query.The subquery in the SELECT clause can return summary data or
calculations.Taken together, joins and subqueries allow for very flexible and
powerful querying of the database.
Customer Table:
Orders Table:
Products Table:
OrderDetails Table:
Query 1:
Retrieve the names of customers who have placed orders along with the order dates
(Use an inner join).
Input:
Output:
Explanation:
INNER JOIN: This joins the Customers and Orders tables where there is a match
on the CustomerID (customers who have placed orders).
The query selects the CustomerName from the Customers table and the
corresponding OrderDate from the Orders table.
This will return a list of customer names and the dates of their orders.
Query 2:
Find the list of products that have never been ordered (Use a subquery).
Input:
SELECT ProductName
FROM Products
WHERE ProductID NOT IN (SELECT ProductID FROM OrderDetails);
Output:
Explanation:
1. The subquery (SELECT ProductID FROM OrderDetails) retrieves all the
ProductID values that have been ordered (i.e., products that appear in the
OrderDetails table).
2. The outer query selects the ProductName from the Products table where the
ProductID does not appear in the subquery result, which means those products
have never been ordered.
This will return the list of products that have not been ordered.
Query 3:
List all customers and the total amount they have spent on their orders (Use joins
and aggregate functions).
Input:
SELECT Customers.CustomerName,
SUM(Products.Price * OrderDetails.Quantity) AS TotalAmountSpent
FROM (Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID)
INNER JOIN (Products INNER JOIN OrderDetails ON Products.ProductID =
OrderDetails.ProductID)
ON Orders.OrderID = OrderDetails.OrderID
GROUP BY Customers.CustomerName;
Output:
Explanation:
1. INNER JOIN: Joins the Customers, Orders, OrderDetails, and Products tables to
get the necessary information.
2. SUM(Products.Price * OrderDetails.Quantity): Calculates the total amount spent
by multiplying the price of each product by the quantity ordered.
3. GROUP BY Customers.CustomerName: Groups the results by each customer to
ensure that the total amount spent is calculated per customer.
This query will return each customer's name along with the total amount they have
spent on all their orders.
Query 4:
Find the order details (product name, quantity, price) for a given customer whose
CustomerID is 3 (Use a join).
Input:
SELECT Products.ProductName, OrderDetails.Quantity, Products.Price
FROM (Orders
INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID)
INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID
WHERE Orders.CustomerID = 3;
Output:
Explanation:
1. JOIN between OrderDetails and Orders: This allows you to match the order
details (like quantity) with the specific customer who placed the order.
2. JOIN between OrderDetails and Products: This retrieves the product information
(name, price) based on the product IDs in the order details.
3. WHERE Orders.CustomerID = 3: This filters the results to only show the orders
placed by the customer with CustomerID = 3.
Query 5:
Retrieve the most recent order placed by each customer (Use subqueries).
Input:
SELECT Orders.CustomerID, Orders.OrderID, Orders.OrderDate
FROM Orders
WHERE Orders.OrderDate = (
SELECT MAX(O.OrderDate)
FROM Orders AS O
WHERE O.CustomerID = Orders.CustomerID);
Output:
Explanation:
1. Subquery: The inner subquery (SELECT MAX(O.OrderDate) ...) finds the most
recent OrderDate for each customer (CustomerID).
2. WHERE clause: The outer query selects all columns from the Orders table
where the OrderDate matches the maximum date from the subquery for each
customer.
This will return the most recent order (with its OrderID and OrderDate) for each
customer.
Query 6:
Get a list of all countries where customers who ordered products are located.
Ensure no duplicates are returned (Use a join and DISTINCT).
Input:
SELECT DISTINCT Customers.Country
FROM Customers
INNER JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
Output:
Explanation:
1. Inner Join: This joins the Customers and Orders tables based on the CustomerID
to ensure you're only selecting customers who have placed orders.
2. Distinct: Ensures that each country is listed only once, eliminating duplicates.
This query will return a list of countries where customers who placed orders are
located, with no duplicate entries.
Conclusion:
SQL joins and subqueries are powerful tools for data retrieval, each serving
distinct purposes.
SQL Joins:
Joins allow for combining data from multiple tables based on a shared column,
making them ideal for relational databases where data is often split across various
tables. For instance, if you have a customers table and an orders table, an Inner
Join lets you retrieve orders with customer details only when there’s a match in
both tables. On the other hand, a Left Join is useful when you want to get all
customers.
Subqueries:
Subqueries allow you to perform a query inside another query, adding flexibility to
filter or compute results dynamically. For example, you can use a subquery in the
WHERE clause to find customers who have placed the most recent order.
Subqueries are especially helpful when you need results from a derived set of data,
or for more complex filtering conditions.