Skip to content
Open

22_1 #212

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion 1-select-tasks/22.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
Create a SQL query to display the name of each customer along with their most expensive purchased product:

| CompanyName | ProductName | PricePerItem |
| CompanyName | ProductName | PricePerItem |

SELECT
[cc].[CompanyName],
(SELECT TOP(1) [p].[ProductName]
FROM [dbo].[Products] [p]
JOIN [dbo].[Order Details] [OD] ON [OD].[ProductID]=[p].[ProductID]
JOIN [dbo].[Orders] [o] ON [o].[OrderID]=[OD].[OrderID]
JOIN [dbo].[Customers] [c] ON [c].[CustomerID]=[o].[CustomerID]
WHERE [p].[UnitPrice]=(
SELECT MAX([OD].[UnitPrice])
FROM [dbo].[Orders] [o]
JOIN [dbo].[Order Details] [OD] ON [OD].[OrderID]=[o].[OrderID]
WHERE [o].[CustomerID]=[c].[CustomerID] ) AND [c].[CustomerID]=[cc].[CustomerID]) AS [ProductName],
(SELECT MAX([OD].[UnitPrice])
FROM [dbo].[Orders] [o] JOIN [dbo].[Order Details] [OD] ON [OD].[OrderID]=[o].[OrderID]
WHERE [o].[CustomerID]=[cc].[CustomerID] ) AS [PricePerItem]
FROM
[dbo].[Customers] [cc]