About 3,090,000 results
Open links in new tab
  1. sql - When to use Common Table Expression (CTE) - Stack Overflow

    Jan 5, 2017 · 31 One of the scenarios I found useful to use CTE is when you want to get DISTINCT rows of data based on one or more columns but return all columns in the table.

  2. sql server - Keeping it simple and how to do multiple CTE in a query ...

    Example C, "Using multiple CTE definitions in a single query," calls this out explicitly. Sadly, this example is not provided in the documentation for SQL 2008 and older (i.e., the example wasn't …

  3. sql - What are the advantages/disadvantages of using a CTE? - Stack ...

    Sep 10, 2008 · A CTE is not much more than syntactic sugar. It enhances the readability and allows to avoid repetition. Just think of it as a placeholder for the actual statement specified in the WITH() …

  4. SQL - Use a reference of a CTE to another CTE - Stack Overflow

    The CTE can be used in a from clause at any level of nesting within other subqueries. The CTEs -- as with everything in SQL -- need to be defined before they are used.

  5. sql server - How to nest CTE properly - Stack Overflow

    Dec 20, 2016 · On a side note: See if you can break the habit of starting your CTE definitions with ;WITH and instead get into the habit of ending all your SQL statements with a semi-colon.

  6. sql - Referencing a CTE in a where clause - Stack Overflow

    Mar 7, 2025 · I'm trying to use a CTE at the top of my query to be referenced in WHERE clause in a separate CTE further down. For Example: with ref as ( null email, ' ("720884","70540"...

  7. SQL Server CTE and recursion example - Stack Overflow

    Nov 9, 2015 · I never use CTE with recursion. I was just reading an article on it. This article shows employee info with the help of Sql server CTE and recursion. It is basically showing employees and …

  8. sql - Insert into from CTE - Stack Overflow

    Jun 3, 2014 · INSERT INTO T_SOME_VARIABLE WITH cte AS ( SELECT 1 AS tmp_id FROM dual ) SELECT tmp_id FROM cte; -- this throws error, I just wanted to say. It's good, I've used your solution …

  9. sql - How to remove duplicate rows using CTE? - Stack Overflow

    May 30, 2021 · How to remove duplicate rows using CTE? Asked 4 years, 7 months ago Modified 1 year, 9 months ago Viewed 16k times

  10. sql - Difference between CTE and SubQuery? - Stack Overflow

    IMO, anyone who thinks a CTE is less readable that a gigantic blob of interwoven subqueries hasn't seen the garbage pile of confusing saw-teeth-shaped queries in use across the majority of enterprise …