SQL Intermediate

 1.Business Expansion

select ua.id, ua.first_name, ua.last_name, cu.id, cu.customer_name,count(cu.id)

from customer cu, user_account ua, contact c

where cu.id = c.customer_id and c.user_account_id = ua.id

group by ua.id, ua.first_name, ua.last_name, cu.id, cu.customer_name

having count(cu.id) > 1


2.Customer Spending


SELECT c.customer_name, CAST(SUM(i.total_price), AS DECIMAL(9,6)) AS total

FROM customer c

INNER JOIN invoice i ON c.id=i.customer_id

GROUP BY c.customer_name

HAVING SUM(i.total_price)<0.25*(SELECT AVG(total_price) FROM invoice)

ORDER BY total DESC

Post a Comment

0 Comments