SQL CTE Example

A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query

This code example is a short 'get started' piece of code designed to help with the syntax

 

WITH itemSales (itemnmbr, xtndprce) AS 
    SELECT 
            l.itemnmbr,
            l.xtndprce
       FROM sop10200 l
    union all
    SELECT 
            l.itemnmbr,
            l.xtndprce
       FROM sop30300 l
    
  
select itemnmbr, 
        sum(xtndprce) as xtndprce 
    from itemsales 
    group by itemnmbr
    order by 1

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences