You are on page 1of 5

-- Aggregating Data

-- 1. Exercise: Find the average monthly rate for each service type in service_packages. Use the ROUND function here
to make result set neater

SELECT service_type, ROUND(AVG(monthly_rate), 2) AS avg_monthly_rate

FROM service_packages

GROUP BY service_type;

-- 2. Exercise: Identify the customer who has used the most data in a single service_usage record. (covers ORDER BY
and LIMIT that we did in last class)

SELECT customer_id, data_used

FROM service_usage

ORDER BY data_used DESC

LIMIT 1;

-- 3. Exercise: Calculate the total minutes used by all customers for mobile services.

SELECT SUM(minutes_used) AS total_minutes_used

FROM service_usage

WHERE service_type = 'mobile';

-- 4. Exercise: List the total number of feedback entries for each rating level.

SELECT rating, COUNT(*) AS total_entries

FROM feedback

GROUP BY rating;
-- 5. Exercise: Calculate the total data and minutes used per customer, per service type.

SELECT customer_id, service_type,

SUM(data_used) AS total_data_used,

SUM(minutes_used) AS total_minutes_used

FROM service_usage

GROUP BY customer_id, service_type;

-- 7. Exercise: Group feedback by service impacted and rating to count the number of feedback entries.

-- HAVING clause

-- 8. Exercise: Show the total amount due by each customer, but only for those who have a total amount greater than
$100.

SELECT customer_id, SUM(amount_due) AS total_amount_due

FROM transactions

GROUP BY customer_id

HAVING total_amount_due > 100;

-- 9. Determine which customers have provided feedback on more than one type of service, but have a total rating
less than 10.

SELECT customer_id

FROM feedback
GROUP BY customer_id

HAVING COUNT(DISTINCT service_type) > 1 AND SUM(rating) < 10;

-- Conditional Expressions and CASE Statements

-- 1. Exercise: Categorize customers based on their subscription date: ‘New’ for those subscribed after 2023-01-01,
‘Old’ for all others.

SELECT

customer_id,

subscription_date,

CASE

WHEN subscription_date > '2023-01-01' THEN 'New'

ELSE 'Old'

END AS customer_category

FROM

customers;

-- 2. Exercise: Provide a summary of each customer’s billing status, showing ‘Paid’ if the payment_date is not null, and
‘Unpaid’ otherwise.

SELECT

customer_id,

CASE

WHEN payment_date IS NOT NULL THEN 'Paid'

ELSE 'Unpaid'

END AS billing_status

FROM

transactions;
-- 4. Exercise: In service_usage, label data usage as ‘High’ if above the average usage, ‘Low’ if below.

SELECT

customer_id,

data_used,

CASE

WHEN data_used > (SELECT AVG(data_used) FROM service_usage) THEN 'High'

ELSE 'Low'

END AS data_usage_label

FROM

service_usage;

-- 5. Exercise: For each feedback given, categorise the service_impacted into ‘Digital’ for ‘streaming’ or ‘broadband’
and ‘Voice’ for ‘mobile’.

SELECT

feedback_id,

service_impacted,

CASE

WHEN service_impacted IN ('streaming', 'broadband') THEN 'Digital'

WHEN service_impacted = 'mobile' THEN 'Voice'

ELSE 'Unknown'

END AS service_category

FROM

feedback;
-- 6. Exercise: Update the discounts_applied field in billing to 10% of amount_due for bills with a payment_date past
the due_date, otherwise set it to 5%.

UPDATE billing

SET discounts_applied =

CASE

WHEN payment_date > due_date THEN 0.10 * amount_due -- 10% discount for late payments

ELSE 0.05 * amount_due -- 5% discount for on-time payments

END;

-- 7. Exercise: Classify each customer as ‘High Value’ if they have a total amount due greater than $500, or ‘Standard
Value’ if not.

SELECT

customer_id,

total_due,

CASE

WHEN total_due > 500 THEN 'High Value'

ELSE 'Standard Value'

END AS classification

FROM

customers;

-- 8. Exercise: Mark each feedback entry as ‘Urgent’ if the rating is 1 and the feedback text includes ‘outage’ or ‘down’.

-- 9. Exercise: In billing, create a flag for each bill that is ‘Late’ if the payment_date is after the due_date, ‘On-Time’ if it’s
the same, and ‘Early’ if before.

You might also like