Learn how to nest CASE statements in this SQL tutorial, nesting CASE statements can be used for layers of conditional logic but can become complex and difficult to read so always remember to add comments to your code, I also like to indent my case statements to improve readability. The other issue with nested CASE statements is that they are not very dynamic but we could overcome that problem by storing the values in a separate table then joining to that table. To follow along with this SQL tutorial run the below code, also includes nested case statements created in the video, replace greater than and less than with actual symbols: CREATE TABLE ( CustomerKey INT IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Customers_CustomerKey PRIMARY KEY (CustomerKey), FirstName VARCHAR(50) NULL, LastName VARCHAR(50) NULL, BusinessName VARCHAR(50) NULL, CustomerType CHAR(1) ); INSERT INTO (FirstName, LastName, BusinessName, CustomerType) VALUES ('Albert', 'Gunner', NULL, 'P'), (NULL, NULL, 'Beach Store', 'B'), ('Catherine', 'Smith', NULL, 'P'), (NULL, NULL, 'Duncan''s Hair', 'B'), ('Erin', 'Fairclough', NULL, 'P'), (NULL, NULL, 'Gaming Zone', 'B'), ('Henry', 'Long', NULL, 'P'); CREATE TABLE ( OrderKey INT IDENTITY(1, 1) NOT NULL CONSTRAINT PK_Orders_OrderKey PRIMARY KEY (OrderKey), CustomerKey INT NULL, OrderDate DATE NULL, OrderAmount DECIMAL(8, 2) ); INSERT INTO (CustomerKey, OrderDate, OrderAmount) VALUES (1, '20220501', ), (1, '20220602', ), (2, '20220501', ), (2, '20220602', ), (3, '20220501', ), (3, '20220602', ), (3, '20220501', ), (4, '20220602', ), (4, '20220501', ), (4, '20220602', ), (4, '20220501', ), (5, '20220602', ), (5, '20220501', ), (6, '20220602', ), (7, '20220501', ), (7, '20220602', ); SELECT *, CASE WHEN CustomerType = 'P' THEN CASE WHEN NoOfOrders (greater than or equal to) 3 THEN CASE WHEN TotalAmount (greater than or equal to) THEN ELSE END WHEN NoOfOrders (greater than or equal to) 2 THEN CASE WHEN TotalAmount (greater than or equal to) THEN ELSE END ELSE END WHEN CustomerType = 'B' THEN CASE WHEN NoOfOrders (greater than or equal to) 3 THEN CASE WHEN TotalAmount (greater than or equal to) THEN ELSE END WHEN NoOfOrders (greater than or equal to) 2 THEN CASE WHEN TotalAmount (greater than or equal to) THEN ELSE END ELSE END END AS Discount FROM AS Cust INNER JOIN ( SELECT CustomerKey, COUNT(OrderKey) AS NoOfOrders, SUM(OrderAmount) AS TotalAmount FROM GROUP BY CustomerKey ) AS Ord ON = ;











