Skip to content
Aabha AI Academy
Skip to reading

SQL · Module 4

Answers and explanations · Calculate amounts and time periods

Compare these references after your own attempt. You can read them at any time; they do not record a live model or vendor run.

Exercise 04a

Two mugs at 250 give 500 before discount. The line discount is 50 total, leaving 450. It is not 50 per mug.

SELECT quantity*unit_price AS before_discount,line_discount,
 quantity*unit_price-line_discount AS line_sales FROM aabha_sql.order_items WHERE order_id=1005;

Expected output:

before_discount line_discount line_sales
500.00 50.00 450.00

Runnable answer: sql/solutions/04a.sql

Exercise 04b

Order 1008 is in February and receives 0; 1009 and 1012 are in March and receive 1. The next-month upper bound prevents overlap.

SELECT order_id,CASE WHEN order_date>=DATE '2025-03-01'
 AND order_date<DATE '2025-04-01' THEN 1 ELSE 0 END AS in_march
FROM aabha_sql.orders WHERE order_id IN (1008,1009,1012) ORDER BY order_id;

Expected output:

order_id in_march
1008 0
1009 1
1012 1

Runnable answer: sql/solutions/04b.sql

Return to this module