T-SQL 101: 117 Creating Scalar Subqueries in SQL Server T-SQL
The first example of subqueries types in this series is what’s called a scalar subquery. In this case, I’m trying to work out which orders were on the last day that we delivered any orders. We deliver orders on various days, but on the last day that we did deliver orders, tell me which orders they were.
The problem with this is that we don’t know what the delivery date was. If I did, I could just use that date value in a WHERE clause. So what we can do instead is to use a subquery, where we work out the maximum delivery date from the orders table, and then that will return us back a date. Finally, we ask for all the orders for that particular day.
2025-02-18