Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
108 views6 pages

Join Practice Quries

dbms joins practice

Uploaded by

2kad28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views6 pages

Join Practice Quries

dbms joins practice

Uploaded by

2kad28
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Join Practice Quries

These queries use different types of joins (INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL
OUTER JOIN) and conditions to handle the respective scenarios.

Create a e- commerce database with below tables and insert some sample data then execute
given queries.

create database Flipkart;

use flipkart;

CREATE TABLE Customers (

CustomerID INT PRIMARY KEY AUTO_INCREMENT,

CustomerName VARCHAR(100) NOT NULL,

ContactEmail VARCHAR(100),

ContactPhone VARCHAR(20),

Address VARCHAR(255)

);

CREATE TABLE Orders (

OrderID INT PRIMARY KEY AUTO_INCREMENT,

CustomerID INT,

ProductID INT,

OrderDate DATE,

ShippedDate DATE,

Quantity INT DEFAULT 1,

FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID) ON DELETE CASCADE

);

CREATE TABLE Products (

ProductID INT PRIMARY KEY AUTO_INCREMENT,

ProductName VARCHAR(100) NOT NULL,

Price DECIMAL(10, 2)
);

Inserting sample data:

INSERT INTO Customers (CustomerName, ContactEmail, ContactPhone, Address)

VALUES

('John Doe', '[email protected]', '1234567890', '123 Elm St'),

('Alice Smith', '[email protected]', '0987654321', '456 Oak St'),

('Bob Johnson', '[email protected]', '9876543210', '789 Pine St'),

('Charles King', '[email protected]', '8765432109', '321 Maple St');

INSERT INTO Products (ProductName, Price)

VALUES

('Phone', 699.99),

('Laptop', 1299.99),

('Tablet', 499.99),

('Smartwatch', 199.99),

('Headphones', 149.99);

INSERT INTO Orders (CustomerID, ProductID, OrderDate, ShippedDate, Quantity)

VALUES

(1, 1, '2024-01-05', '2024-01-07', 1), -- John Doe ordered a Phone

(2, 2, '2024-01-10', '2024-01-15', 1), -- Alice Smith ordered a Laptop

(3, 1, '2024-01-12', NULL, 2), -- Bob Johnson ordered 2 Phones (not shipped yet)

(1, 4, '2024-01-15', '2024-01-17', 1), -- John Doe ordered a Smartwatch

(4, 3, '2024-02-01', '2024-02-03', 1); /* Charles King ordered a Tablet */

==========================================================================

1. Find the names of customers and the products they ordered:

SELECT Customers.CustomerName, Products.ProductName

FROM Customers
JOIN Orders ON Customers.CustomerID = Orders.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID;

2. List the customer names and the products they ordered but only include customers who
have ordered "Phone":

SELECT Customers.CustomerName, Products.ProductName

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID

WHERE Products.ProductName = 'Phone';

3. Count how many orders each customer has placed:

SELECT Customers.CustomerName, COUNT(Orders.OrderID) AS OrderCount

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID

GROUP BY Customers.CustomerName;

4. List all customers and their orders, including customers who have not placed any orders
(LEFT JOIN):

SELECT Customers.CustomerName, Products.ProductName

FROM Customers

LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID

LEFT JOIN Products ON Orders.ProductID = Products.ProductID;

5. List all products from the Orders table and their respective customer names, including
orders without a customer (RIGHT JOIN):
SELECT Products.ProductName, Customers.CustomerName

FROM Orders

RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID;

6. List all customers and all orders (FULL OUTER JOIN):

SELECT Customers.CustomerName, Products.ProductName

FROM Customers

FULL OUTER JOIN Orders ON Customers.CustomerID = Orders.CustomerID

FULL OUTER JOIN Products ON Orders.ProductID = Products.ProductID;

===========================================================================

SELECT Customers.CustomerName, Products.ProductName

FROM Customers

LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID

LEFT JOIN Products ON Orders.ProductID = Products.ProductID

UNION

SELECT Customers.CustomerName, Products.ProductName

FROM Orders

RIGHT JOIN Customers ON Orders.CustomerID = Customers.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID;

7. Find all customers who placed orders in January 2024 along with the products they ordered:

SELECT Customers.CustomerName, Products.ProductName, Orders.OrderDate

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID

WHERE Orders.OrderDate BETWEEN '2024-01-01' AND '2024-01-31';


8. Using aliases to list customer names and products for customers whose names start with 'A':

SELECT C.CustomerName, P.ProductName

FROM Customers AS C

JOIN Orders AS O ON C.CustomerID = O.CustomerID

JOIN Products AS P ON O.ProductID = P.ProductID

WHERE C.CustomerName LIKE 'A%';

9. List customer names, product names, and order IDs by joining Customers, Orders, and
Products tables:

SELECT Customers.CustomerName, Products.ProductName, Orders.OrderID

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID;

10. Find the customer names and products for the customer who has placed the most orders:

SELECT Customers.CustomerName, Products.ProductName

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID

JOIN Products ON Orders.ProductID = Products.ProductID

GROUP BY Customers.CustomerName, Products.ProductName

ORDER BY COUNT(Orders.OrderID) DESC

LIMIT 1;
11. List customer names and order dates, and for orders not shipped, show "Not Shipped":

SELECT Customers.CustomerName, Orders.OrderDate,

COALESCE(Orders.ShippedDate, 'Not Shipped') AS ShippedStatus

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID;

12. List each product and the number of orders placed for that product:

SELECT Products.ProductName, COUNT(Orders.OrderID) AS OrderCount

FROM Products

JOIN Orders ON Products.ProductID = Orders.ProductID

GROUP BY Products.ProductName;

13. Find customers who have placed more than two orders:

SELECT Customers.CustomerName, COUNT(Orders.OrderID) AS OrderCount

FROM Customers

JOIN Orders ON Customers.CustomerID = Orders.CustomerID

GROUP BY Customers.CustomerName

HAVING COUNT(Orders.OrderID) > 2;

14. Find all customers who doesnot order any thing during the month of January 2024.

15. Find products which are not sold one item till now.

16. Find most frequently sold proproducts.

You might also like