Example of SQL Server NULL

Let’s create a simple example to illustrate the use of NULL in SQL Server.

Step 1: Creating a Sample Table

Query

CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
PhoneNumber VARCHAR(15) NULL
);

In this example, the “PhoneNumber” column is set to allow NULL values.

Step 2: Inserting Data

Query:

INSERT INTO Employees (EmployeeID, FirstName, LastName, PhoneNumber)
VALUES
(1, 'John', 'Doe', '123-456-7890'),
(2, 'Jane', 'Smith', NULL),
(3, 'Bob', 'Johnson', '987-654-3210');

Here, the second employee, Jane Smith, doesn’t have a phone number, so we use NULL.

Step 3: Retrieving Data

Query:

SELECT * FROM Employees;

Output:

OUTPUT

Explanation:

This output displays the data stored in the Employees table. Jane Smith’s entry shows NULL in the PhoneNumber column, reflecting the intentional absence of a phone number for her record. This shows that NULL means simply Nothing to enter hence similarly there is no such info provided and null is displayed as output.

The screenshot visually represents the result of the query execution. The output matches the tabular representation, emphasizing that Jane Smith’s phone number is indeed NULL.

SQL Server NULL Values

In SQL Server, NULL represents the absence of a value in a column. It signifies missing or undefined data, distinct from zero or an empty string. SQL Server uses a three-valued logic, and handling NULL is crucial for accurate querying and reporting. Conditions like IS NULL and IS NOT NULL are used to check for NULL values, and functions like COALESCE assist in managing and substituting NULL in expressions.

Prerequisite:

Before starting to learn the concept of the NULL one only needs proper concentration at first and a very basic knowledge of database commands like Insert, and Create table, and then you are set to go on this journey.

Similar Reads

NULL Values

In the world of SQL Server, think of NULL as a special player in the database game—it’s like the blank space in a crossword puzzle waiting to be filled in. Now, imagine your database as a library of tables, each holding different pieces of information. In these tables, every little square, or cell, is like a box eager to be filled with data....

Example of SQL Server NULL

Let’s create a simple example to illustrate the use of NULL in SQL Server....

COALESCE and ISNULL Functions

In addition to using NULL, SQL Server provides two functions—COALESCE and ISNULL—to handle situations where NULL values need to be managed or replaced with default values....

Conclusion

Understanding SQL Server NULL is vital for effective database management. By considering it as an empty placeholder, you can better handle missing or unknown information in your database tables. We have already understood the concept using real-life analogies and cases. This Null concept not only will be helpful in just SQL but also at other places of software engineering....

Contact Us