Grokking SQL for Tech Interviews
Ask Author
Back to course home

0% completed

Handle NULLs in SQL
Table of Contents

What are Null Values?

Handling Null Values

  1. Allowing Null Values in Columns:
  1. Checking for Null Values:

What are Null Values?

Null values in MySQL indicate the absence of a value in a column. Unlike an empty string or zero, null is not a valid data value and typically represents missing or unknown information. Columns in a MySQL table can be defined to allow or disallow null values based on the data requirements.

Handling Null Values

1. Allowing Null Values in Columns:

When defining a table, you can specify whether a column allows null values or not. This is done using the NULL attribute in the column definition.

CREATE TABLE students ( id INT, email VARCHAR(255) NULL, name VARCHAR(255) );

In the above example, email allows null values, while name does not.

2. Checking for Null Values:

To check if a column contains null values, you can use the IS NULL or IS NOT NULL condition in a WHERE clause.

SELECT * FROM students WHERE email IS NULL;

This query retrieves rows where email contains null values.

Mark as Completed

Table of Contents

What are Null Values?

Handling Null Values

  1. Allowing Null Values in Columns:
  1. Checking for Null Values: