T-Sql 101

T-SQL 101: 122 Simplifying Queries with Common Table Expressions (CTEs) in SQL Server T-SQL

One of the challenges with subqueries is they can get very, very messy if you need to use them more than once. So for example, in the first query above, I’ve created a derived table called BulkProducts. But what if I needed to derive that same table again? Then I would end up with yet another copy of all that code. (And obviously it might be far more complex)

A more common problem is that in a SELECT statement, I might be selecting subqueries but every single time I might be needing to calculate a sub-query to get the values.

2025-02-28

T-SQL 101: 121 Using VALUES Clauses as Row Constructors in SQL Server T-SQL

SQL Server 2008 and later support a VALUES clause. We can use this to construct a table on the fly.

In the example above, you can pretty much ignore the outer query as I just put it there to select from the table that was derived in the VALUES clause.

With VALUES, I put a list of rows that I want to include. They each must have the same number of columns. There can be up to 1000 rows at a time.

2025-02-26

T-SQL 101: 120 Nested vs Correlated Subqueries in SQL Server T-SQL

In the previous T-SQL 101 post, I mentioned that some subqueries depend upon the outer query and others don’t. That’s the distinction between what are called nested and correlated subqueries.

Nested Subquery

I mentioned that you can also use a subquery to return a table of values. In the first example above, I’ve selected some columns from order lines and then said inner join to a derived table of products where their outer quantity is greater than 24.

2025-02-24

T-SQL 101: 119 Checking for Existence with EXISTS Subqueries

Sometimes what we’re interested in is just whether or not a row exists.

In the example shown, I’m saying I want to select product ID and description from the products table where there is at least one order line that has the picked quantity less than the ordered quantity. That means that someone ordered a certain number, but for some reason, we didn’t pick (i.e. got ready for shipment) the quantity that they wanted.

2025-02-22

T-SQL 101: 118 Creating Lists from Subqueries in SQL Server T-SQL

Another way that we can use a subquery is in place of a list of values. I’ve previously shown how the IN operator allows us to supply a set of values, and we check if something or some value is contained in that list.

In this example query, I’m saying that I want a list of all the cinemas that have a credit rating that isn’t OK. Perhaps it’s something to do with not paying their bills, or defaulted in some way.

2025-02-20

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

T-SQL 101: 116 Using Subqueries in SQL Server T-SQL

Subqueries allow us to reuse data from one query in another query. There are three basic ways that this can work:

Row sets (tables) - a subquery can return an entire row set with columns. The basic structure of that is shown in the example above.

Lists - a type of row set that has a single column. It’s often used with an IN clause.

Scalar values - a single value returned from a query i.e., one row with one column. These are often used in place of values in expressions or predicates.

2025-02-16

T-SQL 101: 115 Repetitively selecting with CROSS APPLY, OUTER APPLY

We saw in previous post that we had a function called GetRecentOrders that gets the most recent orders for a particular cinema.

But what if we want to do that for all the cinemas?

If I’d like the two most recent orders for every cinema, that becomes messy. I can’t just do that with a join, like an inner join or outer join. The function provided is fine. We can use that, but we want to call it for every row in the table.

2025-02-14

T-SQL 101: 114 Selecting from Table-Valued Functions

In previous posts, we’ve seen how to query tables. Another type of object that you might need to query is a table-valued function.

Table-valued functions (TVFs) are predefined code i.e., somebody’s written the code, and they take parameters, but they return row set of data, much like a table.

So in the example above, I’m saying I want to declare a Cinema ID variable, and I’ve said let’s have Cinema 27. Then I’ve said I wanted to select from GetRecentOrders. It’s  a TVF that takes two parameters: the Cinema ID, and the maximum number of orders to return. In the case, I’ve said that I want the last two orders for cinema 27.

2025-02-12

T-SQL 101: 113 Finding Common Data with INTERSECT

Another interesting operator is INTERSECT. We saw how EXCEPT takes a set of rows and removes any duplicates, and removes any rows that are also contained in a second set of rows.

INTERSECT is similar in the way it works, but it only returns the rows that are common to both row sets.

You could replace an INTERSECT statement with a WHERE EXISTS clause but if you need to deal with NULLable columns, and as the number of columns increases, you can see that INTERSECT becomes quite an elegant solution.

2025-02-10