This month's discount Comprehensive Database Performance Health Check · Testimonials
SQLAuthority with Pinal Dave
Home
  • Consulting
    • Health Check
  • Free Videos
  • All Articles
    • AI
    • SQL Performance
    • SQL Tips and Tricks
    • Interview Questions and Answers
    • SQL Puzzle
    • SQL Video
    • SQLAuthority News
    • Personal
  • Books
  • Consulting
Gouache painting of a line of stepping stones across a pond with the last one vermilion

Generate a Date Range: List All Dates Between Two Dates

January 13, 2021
Pinal Dave
SQL Tips and Tricks
CTE, SQL DateTime, SQL Scripts, SQL Server

To generate a date range in SQL Server, number the days between a start date and an end date and add each number to the start. This post shows a recursive CTE, its limit of 100 recursions, GENERATE_SERIES, a version for older servers, and how to handle times and missing days.

Read More
SQL SERVER - CTE can be Updated

SQL SERVER – CTE can be Updated

May 19, 2014
Pinal Dave
SQL Tips and Tricks
CTE, SQL Scripts, SQL View

Users often confuse a CTE with a temp table, but a CTE works like a view, and a CTE can be updated just like one. Matthew Spieth’s tip, with my notes.

Read More
A potato ricer above regular portions and a smaller final remainder

Loan Amortization Schedules With a Recursive CTE

April 16, 2014
Pinal Dave
SQL Tips and Tricks
CTE, Mathematics, SQL Datatype

Why does the last loan payment leave a few cents behind? A recursive CTE, a rounding rule and a final reconciliation fix it.

Read More
Execution plan for SQL server query.

SQL SERVER – Does Use of CTE Change the Order of Join in Inner Join

March 24, 2014
Pinal Dave
SQL Tips and Tricks
CTE, Execution Plan, SQL Joins

A reader said he uses a CTE to control join order. Does a CTE change the order of join in an inner join? Here is what I found.

Read More
A bulb planter beside separate clusters of shoots and empty planting hollows

Cohort Retention Tables in T-SQL: From Raw Events to a Grid

October 6, 2013
Pinal Dave
SQL Tips and Tricks
Business Intelligence, CTE, SQL DateTime, SQL Group By

Build a retention grid from raw orders with five rows you can check by eye. Includes the DISTINCT rule and why unfinished months show NULL.

Read More
A hand lifts the top glass from a pyramid of stacked glasses, a red napkin at its base.

Walking Foreign Key Chains to Find a Safe Delete Order

November 3, 2012
Pinal Dave
SQL Tips and Tricks
CTE, SQL Constraint and Keys, SQL Delete, SQL Server

Map foreign key chains from a parent table with a recursive catalog query, then list deepest children first and flag cascade rules.

Read More
A rotary egg beater rests between separate mixing bowls and a jug of already beaten mixture

CTE Referenced Twice: Why the Query Runs It Twice

April 16, 2012
Pinal Dave
SQL Performance
CTE, Execution Plan, SQL Group By, Temp Table

Writing a CTE name two times can mean doing the work two times. Two small demos show when a temp table helps and when it only adds cost.

Read More
Previous 1 2 3 4 Next
  • Books
  • Testimonials
  • Privacy Policy

© 2006 – 2026 All rights reserved. pinal @ SQLAuthority.com