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

SQL Assignment Day7

The document provides the schema for three tables - SalesDetails, Orders_2, and ProductCatalog. It includes sample data and queries. The task is to rewrite an existing query to group and sort the results while referencing columns by their one-part names and ordering the output in descending order of SalesTerritoryID and ProductID.

Uploaded by

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

SQL Assignment Day7

The document provides the schema for three tables - SalesDetails, Orders_2, and ProductCatalog. It includes sample data and queries. The task is to rewrite an existing query to group and sort the results while referencing columns by their one-part names and ordering the output in descending order of SalesTerritoryID and ProductID.

Uploaded by

noadviceno
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

SQL ASSIGNMENT DAY 7

/* You have a database that contains the tables as shown in the exhibit. (Click the
Exhibit button.)

You have the following query:

You need to recreate the query to meet the following requirements:


✑ Reference columns by using one-part names only.
✑ Sort aggregates by SalesTerritoryID, and then by ProductID. -- means reduce the rows
by using group by function--
✑ Order the results in descending order from SalesTerritoryID to ProductID.
✑ The solution must use the existing SELECT clause and FROM clause.
Which code segment should you use?
To answer, type the correct code in the answer area. */

create table dbo.SalesDetails(OrderId int , SalesTerritoryID int, Product_ID int,


UnitPrice int, OrderQty int, DiscountAmount int, ExtendedAmount int, FOREIGN KEY(OrderId)
References Orders_2(Order_ID),FOREIGN KEY(Product_ID) References
ProductCatalog(ProductID));

create table dbo.Orders_2(Order_ID int PRIMARY KEY NOT NULL, OrderDate DATE,
SalesTerritoryID int, TotalDue int)

create table dbo.ProductCatalog(CatID int, CatName varchar(20), ProductID int PRIMARY KEY
NOT NULL, ProdName varchar(20), UnitPrice int)

insert into Orders_2 values(1,'2009/07/21',2,34)


insert into Orders_2 values(2,'2009/03/25',2,37)
insert into Orders_2 values(3,'2009/02/24',1,35)

insert into ProductCatalog values(1,'snacks',1,'tiger',23)


insert into ProductCatalog values(1,'snacks',2,'parle-g',32)
insert into ProductCatalog values(2,'chocolate',3,'silk',88)
insert into SalesDetails values(1,2,1,23,2,33,22)
insert into SalesDetails values(2,1,1,32,1,32,21)
insert into SalesDetails values(3,1,2,88,4,36,25)
insert into SalesDetails values(1,2,1,23,4,32,11)
insert into SalesDetails values(2,2,2,24,3,22,12)
insert into SalesDetails values(3,1,3,45,2,12,14)

select * from SalesDetails;


select * from Orders_2;
select * from ProductCatalog;

select SalesTerritoryID, Product_ID,


AVG(UnitPrice) AS Avg_Unit_Price,
MAX(OrderQty) AS Max_Order_Qty,
MAX(DiscountAmount) AS Max_Discount_Amount
from SalesDetails
group by SalesTerritoryID, Product_ID
order by SalesTerritoryID DESC, Product_ID DESC
You need to create a query for a report. The query must meet the following requirements:
NOT use object delimiters.

✑ Return the most recent orders first.


✑ Use the first initial of the table as an alias.
✑ Return the most recent order date for each customer.
✑ Retrieve the last name of the person who placed the order.
✑ Return the order date in a column named MostRecentOrderDate that appears as the last
column in the report.
The solution must support the ANSI SQL-99 standard.
Which code segment should you use?
To answer, type the correct code in the answer area.*/

create table dbo.Customers(Customer_Id int PRIMARY KEY NOT NULL, FirstName varchar(20),
LastName varchar(20));

create table dbo.Orders(Order_Id int PRIMARY KEY NOT NULL, Order_Date Date, Customer_ID
int FOREIGN KEY REFERENCES Customers(Customer_Id));

insert into Customers Values(1,'guru','baran');


insert into Customers Values(2,'tamil','kumar');
insert into Customers Values(3,'hari','karan');
insert into Customers Values(4,'vignesh','waran');

insert into Orders Values(1,'2000/07/17',4);


insert into Orders Values(2,'2000/07/12',3);
insert into Orders Values(3,'2000/07/15',4);
insert into Orders Values(4,'2000/07/13',1);
insert into Orders Values(5,'2000/07/18',2);
select * from Customers;
select * from Orders;

select C.LastName,MAX(O.Order_Date) AS MostRecentOrderDate


from Customers AS C
join Orders AS O
ON O.Customer_ID = C.Customer_Id
Group by C.LastName
Order by MostRecentOrderDate DESC

You might also like