Sql-Interview

SQL Interview: 19: Store why an index exists

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:

While developers don’t mind adding indexes to a database, most people are very nervous about ever removing them.

Tools let us see whether or not an index is being used, but it’s useful to know why the index was added in the first place.

2025-02-07

SQL Interview: 18: Table Truncation and Foreign Keys

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: Administration Level: Medium

Question:

You have two tables:

  • dbo.Customers
  • dbo.Orders

The dbo.Orders table has a column CustomerID that is declared as a foreign key to the CustomerID column in the dbo.Customers table. In the dbo.Customers table, CustomerID is the primary key.

2025-02-06

SQL Interview: 17: Using NULLIF

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:

You see the following statement in a query that you are reviewing.

What is the purpose of the NULLIF function in this statement?

SELECT @a / NULLIF(@b, 0);

What was the author trying to achieve?

2025-02-05

SQL Interview: 16: UNION and Column Aliases

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: Intro

Question:

I execute the following query:

SELECT st.SalesTerritoryID AS [ID],
       st.SalesTerritoryName AS [Name],
       st.DateCreated
FROM dbo.SalesTerritories AS st

UNION

SELECT p.ProductID AS [Product],
       p.[Description],
       p.DateCreated AS [StartDate]
FROM dbo.Products AS p;

What alias will be returned for the each column in the returned results?

2025-02-04

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

SQL Interview: 14: Set operations using EXCEPT

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:

UNION and UNION ALL are commonly used to combine two sets of rows into a single set of rows.

EXCEPT is another set operator.

Can you explain what it does?

Answer:

EXCEPT is used to remove any rows in the first set of rows, if the same rows appear in the second set.

2021-04-29

SQL Interview: 13: Impact of optimize for adhoc workloads

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: Server Tuning Level: Advanced

Question:

SQL Server 2008 introduced a server option for optimize for adhoc workloads.

Can you explain the problem that it is designed to solve, what most commonly causes the problem, and what impact this option has when you enable it?

2021-04-27

SQL Interview: 12: Using UNION vs UNION ALL

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: Intro

Question:

Consider the following code:

SELECT CustomerID, CustomerName 
FROM Sales.Customers
WHERE BusinessCategory = 'Cafe'

UNION

SELECT CustomerID, CustomerName
FROM Sales.Customers
WHERE City = 'Toronto';

A: What would be the difference in results if the query was changed to use a UNION ALL instead of the UNION?

2021-04-22

SQL Interview: 11 Adding a column in the middle of a table's columns

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 you add a column to an existing SQL Server table, the column is added to the end of the table. The column will end up with the highest column_id.

If a developer asks you how a column can be added to the middle of a table instead, what is your advice and how would you approach the request?

2021-04-21

SQL Interview: 10 System-defined default names

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 you define a column default with code like below:

USE tempdb;
GO

DROP TABLE IF EXISTS dbo.Customers;
GO

CREATE TABLE dbo.Customers
(
    CustomerID int IDENTITY(1,1)
        CONSTRAINT PK_dbo_Customers PRIMARY KEY,
    TradingName nvarchar(100) NOT NULL,
    CreatedDate datetime2(3) NOT NULL DEFAULT (SYSDATETIME())
);
GO

the system will define the name of the default. Can you give examples of why specifying the name of the default instead of letting the system supply it would be a good practice?

2021-04-15