Press "Enter" to skip to content

Category: Syntax

The QUALIFY Operator in Spark SQL

Koen Verbeeck looks at a bit of non-standard syntax:

Once upon a time I had a series about some cool stuff in Snowflake: features in their SQL dialect that were not available in the T-SQL dialect at the time (and some still aren’t). I followed up with some posts about interesting Fabric features (some of them are more “ironic” in nature as they describe extremely basic features who were originally missing in Fabric, like folders).

Recently I started working with Databricks on a project, and I mainly have to use the serverless SQL endpoint. Like Snowflake, Databricks also has a pretty amazing SQL dialect. In this blog post, I’m going to talk about one of my favorite new additions to the world of window functions: QUALIFY.

QUALIFY has been around since the Teradata days and it filters window functions in your SELECT statement. That makes it pretty useful, as it means you don’t need common table expressions or sub-queries to filter the results of a SQL query using a window function, like getting records where the RANK() is in the top 3.

Leave a Comment

Fuzzy Matching in SQL Server

Warwick Rudd looks at a few functions:

Every organisation I work with has the same customer in its database three times: “Jon Smith”, “John Smith” and “John Smiht”. Regex can tell you whether a value is well formed. It can’t tell you that two values are probably the same thing. That’s the job of fuzzy string matching, and SQL Server 2025 now has it built in.

I will say that I needed to turn on “preview features” for SQL Server 2025 CU11 in a container, though they did announce recently that these functions are GA. It might be a difference in the container versus other installations, or maybe it’ll be totally GA in CU12.

Warwick digs in a bit into the calculations and also points out that this is going to be fairly expensive because of the nature of the comparison. You likely won’t notice on a few thousand rows, but if you have millions of rows, performance will not be great at all.

Leave a Comment

What Was New for SQL Developers in 2012

Jeff Taylor starts a retrospective:

In my session, “What’s New in SQL Server for the Developer”, I always start with a show of hands, and usually the room has everything from SQL Server 2000 all the way up to 2025, and a few folks running Azure SQL. That’s a big spread. It’s also exactly why I built the session the way I did. A lot of us are still writing T-SQL like it’s 2008, even though the engine underneath has had better tools for years.

So I’m turning my session into a series, one post per version, with the examples from my slides. Today we start where the session starts: SQL Server 2012.

I love this approach. I have a very similar talk to Jeff’s, though, for the sake of time, I have my starting point as “2016 and a couple of things that were really important in 2012.”

Leave a Comment

Fuzzy String Matching now GA

Abhiman Tiwari promotes a feature:

MICROSFT isn’t MICROSOFT. MARHTA isn’t MARTHA. INV-1O08 isn’t INV-1008. To an equality check, each pair is simply different. To your business, each is a near miss worth catching.

Today, fuzzy string matching is generally available in Microsoft SQL. EDIT_DISTANCE tells you MICROSFT is one edit from MICROSOFT. JARO_WINKLER_SIMILARITY scores MARTHA and MARHTA at 96 out of 100. It all runs in T-SQL, right where your data lives, with no external libraries, models, or custom code.

As of CU11, this is now GA in SQL Server 2025.

Leave a Comment

IS DISTINCT FROM

Andy Brownsword tries out a shortcut:

Comparisons involving NULL-able values and columns are an early learning experience. I recently stumbled upon a relatively new way to handle these which I thought would be interesting T-SQL to share.

I think the two reasons you don’t see this in production are as follows. First, it requires a recent version of SQL Server, 2022 or later. A lot of the T-SQL code that went into training language models was for older versions of SQL Server. Second, because there’s no performance difference, people don’t spend a lot of time changing existing code to use this syntax. It’s a bit clearer in terms of intent, but otherwise doesn’t move the needle for people.

Leave a Comment

DML in Postgres

Grant Fritchey tries out some syntax:

Once your PostgreSQL tables are built, the next step is putting them to work. This guide walks through the core data operations every PostgreSQL user needs: inserting records with INSERT, retrieving them with SELECT, filtering and combining tables with WHERE and JOIN, and modifying or removing data with UPDATE and DELETE.

You’ll also learn how PostgreSQL handles IDENTITY columns, sequences, aliases, and referential integrity.

Click through for Grant’s notes.

Comments closed

The Limits of CHOOSE()

Steve Jones performs a test:


I was looking over the CHOOSE() function while editing an article at SQL Server Central. I realized there isn’t a documentation note on how many parameters can be included. I decided to test.

This post looks at the tests and results.

Click through to see how many parameters Steve was able to cram in before the whole thing fell apart. This is definitely a place where Swart’s 10% Rule comes into play.

Comments closed

ROLLUP, CUBE, and GROUPING SETS in T-SQL

Louis Davidson works with three amigos:

To get the total, there are a few ways, which I will demonstrate in the next sections on the aggregation functions, ROLLUP, CUBE, and then GROUPING SETS. Grouping sets are the most flexible, in that they let you do what the other two can do, but with more control. But knowing the others is important because it is how most ad hoc queries will be written.

It’s uncommon that I use GROUPING SETS() in my code, but every time I do, I’m thankful that it exists.

Comments closed

XACT_ABORT vs TRY_CATCH

Jared Westover explains the difference:

After creating a SQL script and a pull request, a savvy developer left a comment on the PR because they were confused about why I was using both XACT_ABORT and TRY…CATCH. They thought that including both in one script was redundant. That got me thinking: is it true that setting XACT_ABORT ON makes TRY…CATCH redundant?

The most interesting part of this post is the cases in which you would use both in the same script.

Comments closed

The TABLESAMPLE Process in Postgres

Radim Marek provides an explanation:

At least Postgres avoids flushing its own cache by reading large tables through a small ring buffer. Nevertheless the disk still perform full read. On a primary server handling production traffic is shared with everything else. When all you need is exploratory confirmation “roughly how many” or “are there any representative samples” reading the whole terabyte is too expensive.

SQL Standard comes with TABLESAMPLE to help with that. It goes in the FROM clause right after the table name, and the rest of the query runs against the subset of pages it picks.

Read on to learn more.

Comments closed