---
title: "SQL join returns null records when there is no data in the database in SQL Server Why?"  
description: "SQL join returns null records when there is no data in the database in SQL Server Why?"  
author: "Sandra Emily"  
published: 2023-07-30  
updated: 2023-07-31  
canonical: https://www.mindstick.com/forum/159370/sql-join-returns-null-records-when-there-is-no-data-in-the-database-in-sql-server-why  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# SQL join returns null records when there is no data in the database in SQL Server Why?

SQL [join](https://www.mindstick.com/articles/1487/join-sleep-and-interrupt-methods-in-c-sharp-threading) returns [null](https://www.mindstick.com/forum/33922/how-to-use-null-coalescing-operator-in-c-sharp) [records](https://www.mindstick.com/forum/34640/how-to-create-a-stored-procedure-for-display-all-records) when there is no [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) in the [database in SQL](https://www.mindstick.com/forum/158348/how-do-you-create-a-new-database-in-sql-server) [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) Why?

## Replies

### Reply by Aryan Kumar

Yes, a SQL join returns null records when there is no data in the [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server). This is because a join is a way of combining data from two or more tables. If there is no data in one of the tables, then the join will return null records for the rows in the other table that do not have a corresponding row in the first table.

For example, the following query will return a null record for the `CustomerID` column if there is no customer with the ID 100:

SQL

```plaintext
SELECT CustomerID, FirstName, LastName
FROM Customers
WHERE CustomerID = 100;
```

If there is no customer with the ID 100, then the `CustomerID` column will be null for the row that is returned by the query.

There are a few ways to avoid null records when there is no data in the database. One way is to use the `ISNULL` function. The `ISNULL` function returns a null value if the expression that it is passed is null, and it returns the specified value if the expression is not null.

For example, the following query will return the string "No customer found" if there is no customer with the ID 100:

SQL

```plaintext
SELECT CustomerID, FirstName, LastName
FROM Customers
WHERE CustomerID = 100
OR ISNULL(CustomerID, 'No customer found');
```

Another way to avoid null records when there is no data in the database is to use the `OUTER JOIN`. The `OUTER JOIN` will return all of the rows from the first table, even if there are no corresponding rows in the second table.

For example, the following query will return all of the rows from the `Customers` table, even if there are no customers with the ID 100:

SQL

```plaintext
SELECT CustomerID, FirstName, LastName
FROM Customers
LEFT OUTER JOIN Customers ON Customers.CustomerID = Customers.CustomerID;
```

The `LEFT OUTER JOIN` will return a null value for the `CustomerID` column if there is no customer with the ID 100 in the `Customers` table.


---

Original Source: https://www.mindstick.com/forum/159370/sql-join-returns-null-records-when-there-is-no-data-in-the-database-in-sql-server-why

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
