Cards Issued Difference

Write a query that outputs the name of each credit card and the difference in issued amount between the month with the most cards issued, and the least cards issued. Order the results according to the biggest difference.

table name: monthly_cards_issued


Solution:

select card_name, max(issued_amount)- min(issued_amount) AS difference from monthly_cards_issued group by card_name order by difference desc

Output:


SQL Script:

CREATE TABLE [dbo].[monthly_cards_issued](
       [card_name]
[nvarchar](50) NULL,
       [issued_amount]
[int] NULL,
       [issued_month]
[int] NULL,
       [issue_year]
[int] NULL
) ON [PRIMARY]
GO
INSERT [dbo].[monthly_cards_issued] ([card_name],
[issued_amount],
[issued_month],
[issue_year]) VALUES (N'Chase Freedom
Flex', 55000, 1, 2021)
GO
INSERT [dbo].[monthly_cards_issued] ([card_name],
[issued_amount],
[issued_month],
[issue_year]) VALUES (N'Chase Freedom
Flex', 60000, 2, 2021)
GO
INSERT [dbo].[monthly_cards_issued] ([card_name],
[issued_amount],
[issued_month],
[issue_year]) VALUES (N'Chase Freedom
Flex', 65000, 3, 2021)
GO
INSERT [dbo].[monthly_cards_issued] ([card_name],
[issued_amount],
[issued_month],
[issue_year]) VALUES (N'Chase Freedom
Flex', 70000, 4, 2021)
GO
INSERT [dbo].[monthly_cards_issued] ([card_name],
[issued_amount],
[issued_month],
[issue_year]) VALUES (N'Chase Sapphire
Reserve',
170000, 1, 2021)
GO
INSERT [dbo].[monthly_cards_issued] ([card_name], [issued_amount], [issued_month], [issue_year]) VALUES (N'Chase Sapphire
Reserve',
175000, 2, 2021)
GO
INSERT [dbo].[monthly_cards_issued] ([card_name],
[issued_amount],
[issued_month],
[issue_year]) VALUES (N'Chase Sapphire
Reserve',
180000, 3, 2021)
GO


Comments (0)