All posts by Basavaraj Biradar

Difference between JOIN and INNER JOIN in Sql Server

Frankly speaking, in Sql Server there is no difference between JOIN and INNER JOIN. They produce the same result and also the same performance.

Let us prove with examples that there is no difference between JOIN and INNER JOIN. Execute the following script to create a demo database with two tables Customers and Orders with sample data as shown in the following image:

joins-demo-tables

CREATE DATABASE SqlHintsJoinDemo
GO
USE SqlHintsJoinDemo
GO
--Create Customers Table and Insert records
CREATE TABLE Customers 
( CustomerId INT, Name VARCHAR(50) )
GO
INSERT INTO Customers
VALUES(1,'Shree'), (2,'Kalpana'), (3,'Basavaraj')
GO
--Create Orders Table and Insert records into it
CREATE TABLE Orders (OrderId INT, CustomerId INT)
GO
INSERT INTO Orders 
VALUES(100,1), (200,4), (300,3)
GO

[ALSO READ] LEFT OUTER JOIN vs RIGHT OUTER

JOIN and INNER JOIN produces the same result

Below table demonstrates that both JOIN and INNER JOIN produces the same result.

JOIN

INNER JOIN

SELECT C.Name 
FROM Customers C 
  JOIN Orders O
    ON O.CustomerId = C.CustomerId

RESULT:
join

SELECT C.Name 
FROM Customers C 
  INNER JOIN Orders O
    ON O.CustomerId = C.CustomerId

RESULT:
inner-join

JOIN and INNER JOIN Execution Plan Comparison

From the below execution plans comparison, we can see that both JOIN and INNER JOIN are producing the same plan and performance. Here I have used the Sql Server 2016 “Compare Execution Plans” feature to compare the execution plan produced by JOIN and INNER JOIN. This feature provides an option to highlight the similar operations and we can see that both JOIN and INNER JOIN is producing the similar operations and they are highlighted in the same color.


execution-plan-comparision

[ALSO READ] Compare Execution Plans in Sql Server 2016

JOIN and INNER JOIN produces the same performance counters

The below performance counters (i.e. execution plan properties) comparison shows that both JOIN and INNER JOIN produces the same performance result


execution-plan-properties-comparision

CONCLUSION

From the above various examples we can see that there is absolutely NO DIFFERENCE between JOIN and INNER JOIN. Even though both produces the same result and performance, I would prefer using INNER JOIN instead of just JOIN. As it is more readable and leaves no ambiguity. If we just write JOIN instead of INNER JOIN it is not obvious what it means or what the developer was intended while writing it. Whether the Junior Developer OR the developer moved from other Database technologies while writing JOIN, he would have thought it behaves as OUTER JOIN. Just avoid such confusions and to make it more readable and obvious I would prefer writing INNER JOIN instead of JOIN.

ALSO READ

Sql Server Error: Incorrect time syntax in time string used with WAITFOR

ERROR SCENARIO

We can get this exception if we try to execute statement like below to to SLEEP or WAIT for 90 seconds

WAITFOR DELAY '00:00:90.000'

RESULT:

Msg 148, Level 15, State 1, Line 1
Incorrect time syntax in time string ’00:00:90.000′ used with WAITFOR.

WHY THIS EXCEPTION?

The parameter which is passed to the WAITFOR DELAY control flow statement should be valid time. In this case the second part specified is 90 which is invalid, the valid value for the second part is between 0 and 59. Any value other than this will raise any exception. Similarly, valid value for the time part is from 0 to 59, valid value for the hour part is from 0 to 23 and for milliseconds the valid value is from 000 to 999.

WHAT IS THE SOLUTION?

To resolve this issue, we can convert 90 seconds as 1 Minute and 30 Seconds and pass it as parameter to the WAITFOR DELAY statement as shown in the below example:

WAITFOR DELAY '00:01:30.000'

RESULT:
wait-for-1-minute-and-30-seconds-in-sql-server

SLEEP Command in Sql Server

In Sql Server to PAUSE OR SLEEP OR WAIT the execution of the script for specified period of time say 10 hours or 10 minutes or 10 seconds or 10 millisecond etc we can use the WAITFOR DELAY command.

SYNTAX
waitfor-delay-in-syntax

Let us understand WAITFOR DELAY command with extensive list of examples:

Example 1: WAIT or SLEEP for 10 MilliSeconds

To wait for 10 MilliSeconds, we can write the WAITFOR DELAY statement like below

WAITFOR DELAY '00:00:00.010'

RESULT:
wait-for-10-milliseconds-in-sql-server

Example 2: WAIT or SLEEP for 10 Seconds

To wait for 10 Seconds, we can write the WAITFOR DELAY statement like below

 
WAITFOR DELAY '00:00:10.000'

RESULT:
wait-for-10-seconds-in-sql-server

Example 3: WAIT or SLEEP for 10 Minutes

To wait for 10 Minutes, we can write the WAITFOR DELAY statement like below

WAITFOR DELAY '00:10:00.000'

RESULT:
wait-for-10-minutes-in-sql-server

Example 4: WAIT or SLEEP for 10 Hours

To wait for 10 Hours, we can write the WAITFOR DELAY statement like below

WAITFOR DELAY '10:00:00.000'

Example 5: WAIT or SLEEP for 90 Seconds or 1 Minute and 30 Seconds

To wait for 90 Seconds or 1 Minute and 30 Seconds, we can write the WAITFOR DELAY statement like below:

WAITFOR DELAY '00:01:30.000'

RESULT:
wait-for-1-minute-and-30-seconds-in-sql-server

Example 6: Passing Variable for the WAITFOR DELAY statement

We can pass a local variable to the WAITFOR DELAY control flow statement as shown in the below example

DECLARE @WaitForTime AS VARCHAR(12) = '00:00:10.000'
WAITFOR DELAY @WaitForTime

RESULT:
passing-local-variable-as-a-parameter-for-the-waitfor-delay-statement

Sql Server Error: A fatal scripting error occurred. Incorrect syntax was encountered while parsing GO

ERROR SCENARIO

We can get exception like this if we try to execute the following statement where T-SQL statement like SELECT is on the Same line as GO Statement

GO SELECT 'Basavaraj Biradar'

RESULT:

A fatal scripting error occurred.
Incorrect syntax was encountered while parsing GO.

WHY THIS EXCEPTION?

GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio, sqlcmd and osql utilities. These utilities send all statements after the previous GO statement and before the current GO statement as one Batch to the Sql Server engine for execution. These utilities mandates that a Transact-SQL statement cannot be on the same line as a GO command.

WHAT IS THE SOLUTION?

To resolve this issue we can move the Transact SQL Statement here the SELECT statement to the next line from the one on the GO statement line as shown in the below script:

GO 
SELECT 'Basavaraj Biradar'

RESULT:
transact-sql-statement-shouldnt-be-on-the-same-line-as-go-statement

GO Statement in Sql Server

GO statement is used as a Batch separator in Sql Server. Batch is nothing but one or more Sql Server statements sent to the Sql Server engine as one set of statements.

GO is not a Transact-SQL statement, instead it is a command recognized by the Sql Server Management Studio (i.e. SSMS), SQLCMD and OSQL utilities. These utilities send all statements after the previous GO statement and before the current GO statement as one Batch to the Sql Server engine for execution. So, it means everything in that batch is local to that batch. In other words any variables declared in the current batch will not be visible in the next batch (i.e. variables declared before the GO statement are not accessible after the GO statement).

GO Statement can also be used to execute batch of T-Sql statement multiple times. Let us understand GO statement with extensive list of examples:

Example 1: GO Statement the Batch Separator example

DECLARE @Name NVARCHAR(50) = 'Basavaraj Biradar'
SELECT @Name AS 'Name'
GO
DECLARE @Name NVARCHAR(50) = 'Shreeganesh Biradar'
SELECT @Name AS 'Name'

RESULT:
sql-go-statement-batch-separator

Even though here we see that the same variable @Name is declared twice, but it is still working. Reason is between these two variable declarations we have a batch separator GO statement. Variables declared before the GO statement are not accessible after the GO statement. Basically SSMS sends the first batch (i.e. Batch 1) of statements to the SQL Engine first, once its execution is over it sends the second batch of statements (i.e. Batch 2) after the GO statement to the SQL Engine for execution.

Example 2: Execute batch of T-Sql statements multiple times in SSMS

GO statement also has an integer optional parameter, this parameter value signals Sql Server to execute the batch of T-Sql Statement prior to the GO statement to be executed for the specified number of times. Let us understand this with following example:

PRINT 'Hello'
GO 5

RESULT:
sql-go-to-execute-batch-of-t-sql-statements-multiple-times

Example 3: T-Sql statements shouldn’t be on the same line as that of the GO Statement

GO SELECT 'Basavaraj Biradar'

RESULT:

A fatal scripting error occurred.
Incorrect syntax was encountered while parsing GO.

To avoid this issue, we can re-write the above example as below:

GO 
SELECT 'Basavaraj Biradar'

RESULT:
transact-sql-statement-shouldnt-be-on-the-same-line-as-go-statement

Example 4: Comment can be on the same line as GO statement

We can write comment on the same line as that of the GO statement

GO  --Comment can be on the same line as GO

RESULT:
sql-go-comment-can-be-on-the-same-line-as-go-statement

Example 5: GO statement can’t be part of the definition of the Stored Procedure, Function, View etc

We can’t add a GO statement in the definition of the Stored Procedure/Function/View etc

CREATE PROCEDURE GOStatementDemo
AS
BEGIN
	SELECT 'Basavaraj Biradar'
	GO	
	SELECT 'Shreeganesh Biradar'
END

RESULT:

Msg 102, Level 15, State 1, Procedure GOStatementDemo, Line 4 [Batch Start Line 0]
Incorrect syntax near ‘Basavaraj Biradar’.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ‘END’.