Junior — Middle
Kas on SQL-is ühine tabeliavaldis (CTE)?
sobes.tech AI
Vastus AI-lt
The Common Table Expression (CTE) in SQL is a temporary result set of a query that can be used within the main SQL query. CTEs are defined using the WITH keyword and allow structuring complex queries, improving their readability and maintainability.
Example of using CTE:
WITH RecentOrders AS (
SELECT OrderID, CustomerID, OrderDate
FROM Orders
WHERE OrderDate > '2024-01-01'
)
SELECT CustomerID, COUNT(OrderID) AS OrderCount
FROM RecentOrders
GROUP BY CustomerID;
Here, the CTE RecentOrders forms a subset of orders, and then the main query aggregates data by customer.