ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)

Set your Publisher ID in src/config/ads.ts to display live ads.

MySQL Relational Database Tutorial• 6 min read

Views & Common Table Expressions (CTEs)

Simplify complex queries with reusable Virtual Views and modern Common Table Expressions (WITH clauses).

Common Table Expressions (CTEs)

Introduced in MySQL 8.0, CTEs (WITH clause) create temporary named result sets that make nested queries readable and modular.

Using a CTE to Calculate Customer Tiers
WITH CustomerSpend AS (
    SELECT UserID, SUM(OrderTotal) AS Total
    FROM Orders
    GROUP BY UserID
)
SELECT UserID, Total,
       CASE 
           WHEN Total > 1000 THEN 'VIP'
           WHEN Total > 250 THEN 'Regular'
           ELSE 'New'
       END AS CustomerTier
FROM CustomerSpend;
ADVERTISEMENT
Google AdSense Slot (inContentBanner)

Set your Publisher ID in src/config/ads.ts to display live ads.

Test Yourself With Exercise

+15 XP

Which SQL clause introduces a Common Table Expression (CTE) in MySQL 8.0+?