All posts by Basavaraj Biradar

Difference between RIGHT JOIN and RIGHT OUTER JOIN in Sql Server

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

Let us prove with examples that there is no difference between RIGHT JOIN and RIGHT OUTER 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] Difference between LEFT OUTER JOIN and RIGHT OUTER JOIN

RIGHT JOIN and RIGHT OUTER JOIN produces the same result

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

RIGHT JOIN

RIGHT OUTER JOIN

SELECT * 
FROM Customers C 
  RIGHT JOIN Orders O
    ON O.CustomerId = C.CustomerId

RESULT:
right-join-result

SELECT * 
FROM Customers C 
  RIGHT OUTER JOIN Orders O
    ON O.CustomerId = C.CustomerId

RESULT:
right-outer-join-result

RIGHT JOIN and RIGHT OUTER JOIN Execution Plan Comparison

From the below execution plans comparison, we can see that both RIGHT JOIN and RIGHT OUTER 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 RIGHT JOIN and RIGHT OUTER JOIN. This feature provides an option to highlight the similar operations and we can see that both RIGHT JOIN and RIGHT OUTER JOIN is producing the similar operations and they are highlighted in the same color.


right-join-vs-right-outer-join-execution-plan-comparision

RIGHT JOIN and RIGHT OUTER JOIN produces the same performance counters

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


right-join-vs-right-outer-join-execution-properties-comparision

CONCLUSION

From the above various examples we can see that there is absolutely NO DIFFERENCE between RIGHT JOIN and RIGHT OUTER JOIN. Even though both produces the same result and performance, I would prefer using RIGHT OUTER JOIN instead of just RIGHT JOIN. As it is more readable and leaves no ambiguity. Please share which one you use and reason for the same.

ALSO READ

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