The Easy Guide on How to Use Subqueries in SQL Server

Total: 7 Average: 5

Do you use SQL subqueries or avoid using them?

Let’s say the chief credit and collections officer asks you to list down the names of people, their unpaid balances per month, and the current running balance and wants you to import this data array into Excel. The purpose is to analyze the data and come up with an offer making payments lighter to mitigate the effects of the COVID19 pandemic.

Do you opt to use a query and a nested subquery or a join? What decision will you make?

Read More

Auto Create Statistics and Auto Update Statistics

Total: 8 Average: 4.1

Statistics comprises lightweight objects that are used by SQL Server Query optimizer to determine the optimal way to retrieve data from the table. SQL Server optimizer uses the histogram of column statistics to choose the optimal query execution plan. If a query uses a predicate which already has statistics, the query optimizer can get all the required information from the statistics to determine the optimal way to execute the query. SQL Server creates statistics in two ways:

  1. When a new index is created on a column.
  2. If the AUTO_CREATE_STATISTICS option is enabled.

In this article, Auto Create Statistics and Auto Update Statistics options are analyzed. They are database specific and can be configured using SQL Server management studio and T-SQL Query. Read More

Advanced SQL: Variations and Different Use cases of T-SQL Insert Statement

Total: 6 Average: 3.8

In my previous article, I demonstrated:

  1. Insert the output of the table-valued function in the SQL table.
  2. Insert the output of the table-valued function that is created on the remote database server.

In this article, I am going to demonstrate:

  1. Copy data between two tables, created in a different schema.
  2. Copy data between two tables, created in different databases on the same server.
  3. Copy data between two tables created, in different databases resides the different server (Cross server query)

Read More

SQL Server Bulk Insert – Part 2

Total: 3 Average: 4.3

In the previous part of this article, we discussed how to import CSV files to SQL Server with the help of BULK INSERT statement. We discussed the main methodology of bulk insert process and also the details of BATCHSIZE and MAXERRORS options in scenarios.  In this part, we will go through some other options (FIRE_TRIGGERS, CHECK_CONSTRAINTS and TABLOCK) of bulk insert process in various scenarios.

Read More

Basics of Running T-SQL Statements from Command Line using SQLCMD

Total: 12 Average: 3.6

This article is about developing a basic understanding of sqlcmd utility to run T-SQL commands directly from the command prompt without the need of SSMS (SQL Server Management Studio).

The article also highlights the importance of using a lightweight sqlcmd utility to perform some basic database tasks that would otherwise require getting connected to the database through a pre-installed database tool such as SSMS (SQL Server Management Studio) or SSDT (SQL Server Data Tools) followed by getting it ready to run SQL scripts against the desired database(s).

The sqlcmd utility can be a real time-saver for database developers and DBAs since they can straight away run the required SQL scripts from the command-line.

Read More

MERGE: Updating Source and Target Tables Located on Separate Servers

Total: 8 Average: 4.1

What is the MERGE statement?

Using the MERGE statement, we can change data in a target table based on data in a source table. Using it, we can execute INSERT, UPDATE and DELETE on the target tables within a single query block. It joins both tables using columns, common in both tables like the primary key. Based on how column data matches, changes apply to data of target table. The following image illustrates how “MERGE” works:

MERGE statement in SQL Server Read More

Understanding PIVOT, UNPIVOT, and Reverse PIVOT Statements

Total: 37 Average: 4.4

PIVOT and UNPIVOT in SQL are familiar and helpful. We use the T-SQL PIVOT operator to transform the data from table rows into columns. The UNPIVOT operator serves for the reverse goal: it turns the already pivoted columns back into the table rows.

At the time, introducing T-SQL PIVOT and UNPIVOT made a significant improvement in database tasks. Those processes that required many repeated operations before became much faster and simpler. No wonder that these operators are in constant and high demand now.

This article will study the concepts of PIVOT, UNPIVOT, and Reversing a PIVOT on practical examples.

Read More