0% found this document useful (0 votes)
39 views3 pages

D22

The document creates two database tables, Customerss and Orders, defines their columns and constraints, and inserts sample data. It then poses a series of questions and provides the SQL queries to retrieve specific data, such as customer names for orders placed in Paris, total order amount by city, and customer who spent the most. The questions also cover adding new columns, inserting new records, and retrieving customer and order details.

Uploaded by

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

D22

The document creates two database tables, Customerss and Orders, defines their columns and constraints, and inserts sample data. It then poses a series of questions and provides the SQL queries to retrieve specific data, such as customer names for orders placed in Paris, total order amount by city, and customer who spent the most. The questions also cover adding new columns, inserting new records, and retrieving customer and order details.

Uploaded by

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

create database D2

use D2

CREATE TABLE Customerss (


ID INT PRIMARY KEY,
Name VARCHAR(50) NOT NULL,
City VARCHAR(50) NOT NULL,
CONSTRAINT CHK_City CHECK (City IN ('Paris', 'London', 'Berlin')),
CONSTRAINT UNQ_NameCity UNIQUE (Name, City)
);
INSERT INTO Customerss (ID, Name, City)
VALUES (1, 'John', 'Paris'),
(2, 'Emily', 'London'),
(3, 'Michael', 'Berlin'),
(4, 'Sophia', 'Paris'),
(5, 'William', 'London');

CREATE TABLE Orders (


Order_ID INT PRIMARY KEY,
Order_Date DATE NOT NULL,
Amount DECIMAL(10, 2) CHECK (Amount > 0),
Customer_ID INT,
CONSTRAINT FK_CustomerID FOREIGN KEY (Customer_ID) REFERENCES Customerss(ID)
);

INSERT INTO Orders (Order_ID, Order_Date, Amount, Customer_ID)


VALUES (1, '2022-01-01', 100, 1),
(2, '2022-02-01', 200, 2),
(3, '2022-03-01', 150, 3),
(4, '2022-04-01', 300, 4),
(5, '2022-05-01', 250, 5);

select * from Customerss


select * from Orders

--Question 1:
--Write an SQL query to retrieve the names of all customers who have placed orders
in Paris.

SELECT C.Name
FROM Customerss C
JOIN Orders O ON C.ID = O.Customer_ID
WHERE C.City = 'Paris';

--Question 2:
--Write an SQL query to retrieve the total order amount for each city.

SELECT C.City, SUM(O.Amount) AS Total_Amount


FROM Customerss C
JOIN Orders O ON C.ID = O.Customer_ID
GROUP BY C.City;

--Question 3:
--Write an SQL query to retrieve the customer name and order details for all orders
placed in 2022.
SELECT C.Name, O.Order_ID, O.Order_Date, O.Amount
FROM Customerss C
JOIN Orders O ON C.ID = O.Customer_ID
WHERE O.Order_Date >= '2022-01-01' AND O.Order_Date < '2023-01-01';

--Question 4:
--Write an SQL query to retrieve the customer who has placed the highest total
amount of orders.

SELECT C.Name, SUM(O.Amount) AS Total_Amount


FROM Customerss C
JOIN Orders O ON C.ID = O.Customer_ID
GROUP BY C.Name
ORDER BY Total_Amount DESC
LIMIT 1;

SELECT TOP 1 C.Name, SUM(O.Amount) AS Total_Amount


FROM Customerss C
JOIN Orders O ON C.ID = O.Customer_ID
GROUP BY C.Name
ORDER BY Total_Amount DESC;

--Question 5:
Write an SQL query to retrieve the customer names who have not placed any orders.

SELECT C.Name
FROM Customerss C
LEFT JOIN Orders O ON C.ID = O.Customer_ID
WHERE O.Order_ID IS NULL;

Q)Question 1:
Add two new columns to the "Customerss" table: "Email" (VARCHAR(100)) and "Phone"
(VARCHAR(20)).

Insert two new records into the "Customerss" table with the following details:

Name: 'Emma', City: 'Paris', Email: '[email protected]', Phone: '123-456-7890'


Name: 'Daniel', City: 'London', Email: '[email protected]', Phone: '987-654-3210'

ALTER TABLE Customerss


ADD Email VARCHAR(100),
Phone VARCHAR(20);

INSERT INTO Customerss (ID, Name, City, Email, Phone)


VALUES (6, 'Emma', 'Paris', '[email protected]', '123-456-7890'),
(7, 'Daniel', 'London', '[email protected]', '987-654-3210');

select * from Customerss


select * from Orders

Write an SQL query to retrieve the customer names and their respective order
details (order ID, order date, and amount) for customers who have placed orders.

SELECT C.Name, O.Order_ID, O.Order_Date, O.Amount


FROM Customerss C
LEFT JOIN Orders O ON C.ID = O.Customer_ID;

Write an SQL query to retrieve the customer names, their respective order dates,
and the total order amount for customers who have placed orders in Berlin.

Solution:

sql
Copy
SELECT C.Name, O.Order_Date, SUM(O.Amount) AS Total_Order_Amount
FROM Customerss C
JOIN Orders O ON C.ID = O.Customer_ID
WHERE C.City = 'Berlin'
GROUP BY C.Name, O.Order_Date;

You might also like