T-Sql 101

T-SQL 101: 22 Using the LIKE operator in T-SQL

I’ve discussed standard operators in previous posts but an interesting additional one is the LIKE operator. This allows for basic pattern matching in T-SQL predicates.

Let’s look at a simple example:

WHERE ProductName LIKE ‘Grant%’

The % symbol is a wild-card that matches any set of characters, including no characters at all. So this expression would match the following and more:

Grant Grants Cola Grants Lemonade

If we’re looking for a word that’s contained inside a string, we can use it like this:

2019-06-17

T-SQL 101: 21 Comparison operators in T-SQL

We use comparison operators in T-SQL expressions. And the expressions are often used in WHERE clauses. For example:

WHERE CreditLimit = 10000

The = (equals) sign shown is a simple one but T-SQL has a number of operators.

Most people writing T-SQL code are familiar with these:

= Equals <> Does not equal < Less than > Greater than <= Less than or equals >= Greater than or equals

However, less people seem to be aware of the other operators added a while back:

2019-06-10

T-SQL 101: 20 Filtering rows to return with WHERE clause predicates

I showed earlier how we can retrieve data from a table using a SELECT clause. It can also be used to determine which columns are returned, and which table the data is being retrieved from. But we don’t always want all the rows to be returned. The WHERE clause fixes that.

It’s important to understand that the WHERE clause limits the rows returned to ones where the expression in the WHERE clause evaluates to a logical TRUE value.

2019-06-03

T-SQL 101: 19 Querying literals, expressions, and functions in T-SQL

Apart from data just in a table SQL server can select other things like the ones shown here:

If I say SELECT 2, it just returns the value 2.

If I say SELECT ‘Hello’, it just returns Hello.

Both of those are examples of what’s called a literal value, which is an exact value that doesn’t change.

SELECT 4 + 5 is an example of an expression. This is where we can work something out to get the value that needs to be returned. No surprise, that will return 9 just as you’d.

2019-05-27

T-SQL 101: 18 - Removing duplicate rows by using DISTINCT

If I query rows from a SQL Server table, I don’t always want all the rows returned. In particular, I might not want any duplicates. I can remove them by using DISTINCT.

Here’s a simple example. I want a list of the sizes that products can come in, so I execute this query:

Note that although I get a list of sizes, I get a row returned for every row in the table. If I add DISTINCT to the query, look at the effect that it has:

2019-05-20

T-SQL 101: 17 Paginating rows returned from SQL Server T-SQL queries

When I need to display a potentially large number of rows on a screen, I often need to show them in pages. This is called paginating the rows.

For example, if I’m showing bank transactions, I might want to show 25 per page. The challenge is that I also need to be able to ask for a specific page number. So my query becomes: give me page 7 where there are 25 rows per page. In that case, I want to skip the first 150 rows, then ask for the next 25 rows.

2019-05-13

T-SQL 101: 16 Ordering the output of a T-SQL query

When you run a query against SQL Server, you might want the rows to be returned in a specific order. However, the first thing you should ask yourself is whether that’s needed at all.

Sorting can be a very expensive operation if there is no suitable index that’s already sorted the data for you. And the question is: should SQL Server be the one doing the sorting? In many cases, the client application that’s reading the data might be a better place to do the sorting. That’s not always true but it’s worth considering. You often have way more clients than your single database server, and sorting data unnecessarily in the database server could limit your ability to scale your system to higher volumes.

2019-05-06

T-SQL 101: 15 Using column and table aliases in SQL Server queries

Look carefully at the following queries:

I’ve shown several examples here of what are called aliases. There are two basic types of aliases.

Column Alias

In the first query, I’ve used a column alias. I wanted to query the Description column in the dbo.Products table but I wanted the column called ProductName instead. So I follow the column with the word AS and then the name I want to use for the alias.

2019-04-29

T-SQL 101: 14 Using two part names for SQL Server tables and other objects

If you look carefully at the following simple query:

you’ll notice that I didn’t just say FROM Cinemas, I said FROM dbo.Cinemas. The “dbo.” part is the name of the schema. I talked about schemas in an earlier post. And all through these T-SQL 101 blog posts, I keep mentioning that you should always use the schema name as well as the object name.

By why?

[caption id=“attachment_2806” align=“alignnone” width=“312”] Image by Ken Treloar[/caption]

2019-04-22

T-SQL 101: 13 Quoting (Delimiting) Identifiers in T-SQL

If you look carefully at the following two queries in the image below:

you’ll notice there are a couple of differences. In the first query, the word Description is blue, and in the second, it isn’t blue but it has square brackets around it. The second is an example of quoting or delimiting an identifier.

In this case, the reason that the word was blue in the first query is that somewhere in SQL Server, this word is part of the syntax of the language itself. That’s much the same as SELECT or ORDER BY. So SQL Server Management Studio (SSMS) color-codes it the same way it does for the word SELECT.

2019-04-15