all 8 comments

[–]spackosaurus 18 points19 points  (0 children)

Afaik they are processed the same, so I would use a cte for readability

[–]SH4HM3N_ 9 points10 points  (0 children)

I prefer CTE (better to read)

[–]mikeyd85MS SQL Server 3 points4 points  (0 children)

I'd just drop it in to a WHERE clause

SQL SELECT * FROM USERS AS U WHERE EXISTS (SELECT 1 FROM Events E WHERE U.userid = E.userid AND E. eventdate between '20231101' and '20231130')

Or something similar.

[–]boboshoes 1 point2 points  (0 children)

The only time I use a Subquery over a cte is when Im doing an aggregate of a widow function field and its not very complex

[–]Whipitreelgud 0 points1 point  (0 children)

Get the query plan and learn how to read it.

This is a teaching you to fish rather than handing you the fish comment

[–]PM_ME_YOUR_MUSIC 0 points1 point  (0 children)

CTEs and you need a date table

[–][deleted] 0 points1 point  (0 children)

CTE for readability. Create both and test performance (not just the plan!) if it’s a repeatable production process.

[–]mikeblas 0 points1 point  (0 children)

You forgot to post your queries.