Sql-Server

T-SQL 101: 107 Using other outer joins with RIGHT OUTER JOIN, FULL OUTER JOIN

After discussing LEFT OUTER JOIN, you might wonder if there is also a RIGHT OUTER JOIN. There is, and it’s the reverse of the left one. What a RIGHT OUTER JOIN from Products to ProductGroups says is that I want at least one row for every row in ProductGroups. You can see that in the query example above. The product group Hot Food currently has no products in it. The RIGHT OUTER JOIN causes it to be returned, as similar to what happens with a LEFT OUTER JOIN, the columns from the non-matching table are returned as NULL.

2025-02-03

SDU Tools: Calculate Age in Months

Another request that I received a while back, for a new function in our free SDU Tools for developers and DBAs, was to be able to find someone’s age in months. The same would apply to anything where the distance between two dates needs to be measured in months. In response, we added a new function CalculateAgeInMonths. It takes two parameters: @StartingDate date - the date to calculate from (could be a birth date if it’s an age) @CalculationDate date - the date to calculate the age to

2025-02-02

T-SQL 101: 106 Using LEFT OUTER JOIN

The next type of join that I want to discuss in these posts is a left outer join. We use this mostly, when we know that not all rows are matching. For example, in the query shown above, I wanted to add up the ordered quantity values for all products. The problem is that if I used an INNER JOIN, I would only get products that match at least one order line.

2025-02-02

SQL Down Under show 92 with guest Joey D'Antoni discussing working across SQL Server and PostgreSQL is now published!

It was great to catch up with Joey D’Antoni today and to have him on a SQL Down Under podcast. Joey D’Antoni is an Architect and SQL Server MVP with over two decades of experience, working in both Fortune 500 and smaller firms. He holds a BS in Computer Information Systems from Louisiana Tech University and an MBA from North Carolina State University. He is a Microsoft Data Platform MVP and VMware vExpert.

2025-02-01

T-SQL 101: 105 Using modern join syntax

There are two ways you can write common join syntax. There’s an older way to write the query and a new way to write the query. In the image above, you can see the new way written first, and then the old way written below. The newer syntax was introduced as part of ANSI SQL, back in 1992. It has been recommended for over 30 years now. Comparison When you look at the older syntax, you’ll notice that I’m doing two things in the WHERE clause.

2025-02-01

SQL Interview: 15: Permission to create a temporary table

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: Medium Question: User1 has CONNECT permission to a SQL Server and is a member of the db_datawriter role in the database Database1. User2 has CONNECT permission to the same SQL Server and is a member of the db_datareader role in the database Database1.

2025-01-31

T-SQL 101: 104 Using an INNER JOIN

The most common form of join between two tables is what’s called an inner join. In the example shown above, I’ve selected some columns from different tables. I’ve got description and product ID from the product table and I’ve got the product group name from the product group. What I wanted was a list of products but I want to know what group they’re part of. So to do that I had to use the products table, but I also had to join to the product groups table so I could get the name of the product group.

2025-01-31

T-SQL 101: 103 What is a Foreign Key ?

A foreign key is a situation where we store the primary key of one table in another table. It’s very useful. For example, we might need to know which sales transactions were for which customers. Again, keep in mind that like primary keys, there could be more than one column in a key. While a foreign key can reference a unique key, it’s almost always the primary key. In the example shown above, the column OrderID is the primary key of the Orders table.

2025-01-30

T-SQL 101: 102 What is a Primary Key ?

In the previous post, we saw how CROSS JOIN operations work. Now it’s rare for us to want to have every combination of the rows in each table. What we usually end up joining on are primary keys and foreign keys. So we should start by defining what primary keys are. The primary key of a table is a value or combination of values that uniquely identifies a particular row in a table.

2025-01-29

T-SQL 101: 101 Using CROSS JOIN

The simplest way to join two tables is what’s called a cross-join. So this is where I say from products, cross-join, sales territories. SELECT * FROM dbo.Products CROSS JOIN dbo.SalesTerritories; Now what that will do though is it will give me every combination of rows from the two tables. So I’ll get one row from the second table for each row in the first table. This is often called a Cartesian product of the two tables and it can quickly end up generating a very large number of rows.

2025-01-28