SQL Null

HTML
CSS
C#
SQL

SQL Null

First of all we should know what a null value is. Null values are used to represent missing unknown data. A NULL value is different from a zero value or a field that contains spaces. A field with a NULL value is one that has been left blank during record creation!

There can be two conditions:

  1. Where SQL is NULL
  2. Where SQL is NOT NULL

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.

We will have to use the IS NULL and IS NOT NULL operators instead.

Example:

SHOW ME the names of people in the IT department.

SELECT column_names FROM table_name WHERE column_name IS NOT NULL;

There is an example of student table:

student

Let’s see the query to get all the records where marks is NULL:

SELECT SIR_NAME, NAME, MARKS FROM STUDENTS WHERE MARKS IS NULL ;

It will return the following records:

records

Let’s see the query to get all the records where marks is NOT NULL:

SELECT SIR_NAME, FIRSTNAME, MARKS FROM STUDENTS WHERE MARKS IS NOT NULL;

It will return the following records:

Course Video

1. Write SQL Query to get all the columns from Person.Person table where title is not null.

OUTPUT 

2. Write SQL Query to get BusinessEntityID, TerritoryID, Bonus From Sales_SalesPerson where SalesQuota IS NULL

OUTPUT 

3. Write SQL Query to get BuinessEntityID, PersonType from  Person.Person Where title is Null.

OUTPUT 

4. Write a SQL Query to get all the columns from Person.Person where suffix is Null.

OUTPUT 

5. Write Sql query to get all the columns from Sales.SalesPerson table  where TerritoryId is null.

OUTPUT 
NOTE: Use NORTHWIND DATABASE For Practicing these Questions.

6. Devise a SQL query to extract the CustomerID, CompanyName, ContactName, Region, and Country fields from the Customers table. Specifically, the query should focus on retrieving records where the Region column has non-null values are included in the results. (i.e., it does not contains a NULL). [OUTPUT: 31 ROWS]

7. Compose an SQL query to retrieve a detailed set of columns from the “Orders” table. Specifically, the query should fetch the OrderID, CustomerID, EmployeeID, OrderDate, ShippedDate, ShipName, and ShipPostalCode fields. However, ensure that the output only includes those records where the ShipPostalCode column contains null values. [OUTPUT: 19 ROWS].

8.  Devise an SQL query to fetch the complete set of columns from the “Suppliers” table. The query should be structured to include only those records where the Fax field is populated with non-null values. Ensure that the query retrieves all available columns for these specific suppliers. [OUTPUT: 13 ROWS]

9. Formulate an SQL query to extract specific details from the “Suppliers” table. The query should retrieve the SupplierID, CompanyName, Address, and City columns. However, restrict the results to include only those records where the Country column contains ‘USA’, ‘France’, ‘Germany’, ‘Canada’, and simultaneously ensure that the Fax field is null for these selected suppliers. [OUTPUT: 6 ROWS]

10. Compose an SQL query to retrieve all columns from the “Orders” table. However, ensure that the output consists only of records where the ShipRegion field is populated with non-null values. [OUTPUT: 323 ROWS]