Sql-Server

SQL Interview: 21: Using ISNULL vs COALESCE

SQL Interview: 21: Using ISNULL vs COALESCE

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Development Level: Medium

Question:

When writing T-SQL code, you often need to replace NULL values. The core two functions provided are ISNULL and COALESCE.

What is the difference between them?

Answer:

ISNULL only takes a single value to check and replaces it when NULL.

2025-02-15

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

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

SDU Tools: List Constraints with System Names in SQL Server

SDU Tools: List Constraints with System Names in SQL Server

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. I do a lot of reviewing of database designs and one of my least favourite things to see is the use of default system names for constraints. So we added a tool that can help to find when this has happened. It’s called ListConstraintsWithSystemNames.

The procedure takes three parameters.

2025-02-13

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

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

SQL Interview: 20: Difference between a login and a user

SQL Interview: 20: Difference between a login and a user

This is a post in the SQL Interview series. These aren’t trick or gotcha questions, they’re just questions designed to scope out a candidate’s knowledge around SQL Server and Azure SQL Database.

Section: Security Level: Intro

Question:

SQL Server can use both logins and users.

What is the difference between them?

Answer:

A login provides access to the server, but not necessarily to any databases.

A user is used to provide access to a database. It might be mapped to a login, but it is possible for databases to authenticate users directly.

2025-02-11

T-SQL 101: 113 Finding Common Data with INTERSECT

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

SDU Tools: Format Australian Phone Number

SDU Tools: Format Australian Phone Number

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. One request that we had some while back, was the ability to format phone numbers using Australian phone number format. To make that easy, we added the FormatAustralianPhoneNumber function.

The only parameter for this function is the phone number that needs to be formatted.

It starts by finding any digits. Then if it finds either 6, 7, 8, or (the standard) 10 digits, it formats the number appropriately.

2025-02-09

T-SQL 101: 112 Excluding Data with EXCEPT

T-SQL 101: 112 Excluding Data with EXCEPT

We saw how UNION and UNION ALL worked in the last T-SQL 101 post. Sometimes you want to work with two (or more) row sets in other ways.

The EXCEPT clause says that I want all the distinct entries in the first row set unless they also exist in the second row set.

In some database engines, this operator is called MINUS, but EXCEPT is the ANSI SQL standard, and that’s what SQL Server uses.

2025-02-09

SDU Tools: Languages in SQL Server T-SQL

SDU Tools: Languages in SQL Server T-SQL

Our free SDU Tools for developers and DBAs, now includes a very large number of tools, with procedures, functions, and views. Applications often need to display a list of languages for users to choose from. To make that easy, we have now included a view called Languages.

The Languages view returns details of all the world’s languages, based on ISO 639-1. For each language, the view returns:

  • FamilyName
  • LanguageName
  • NativeLanguageName
  • ISO2CharacterCode
  • ISO3CharacterCode

The FamilyName indicates the group of languages that the language belongs to. The LanguageName is what we call it in English. The NativeLanguageName is what locals call the language. And then there are the 2 and 3 character codes from the ISO standard.

2025-02-08

T-SQL 101: 111 Using UNION and UNION ALL

T-SQL 101: 111 Using UNION and UNION ALL

There are times when you need to connect together two sets of results into a single result set. The UNION statement is the way we do that.

In the example shown above, I have two SELECT queries. Notice that I could just highlight either one of them, and run them, and I’d see those values. But if I run the whole query, I’ll get back a single set of results that combines data from each of the queries. Note the results:

2025-02-08