MySQL and NULL Values
In general, using DISTINCT comes very handy when the result set or the target table doesn’t contain the NULL values. But, assuming the target table contains the NULL values. See what happens
If we don’t want NULL values in our result set, then we have to be more specific in our query and let SQL know that we don’t want NULL values in our result set and this can be done using the following clause and a comparison operator:
WHERE Clause
WHERE Clause is another most commonly used filter that allows us to be more specific and fetch the records based on some condition.
- Fetches the records that fulfill the specified condition
- It can be used with SELECT, UPDATE, and DELETE.
Syntax
SELECT column_names FROM Table_name WHERE specify_condition;
IS NOT NULL Operator
IS NOT NULL is a comparison operator that ensures that our result set will not contain the NULL values.
In our case, we need to specify the condition where the column’s value is not NULL. So we’ll use this comparison operator.
Query
SELECT DISTINCT email FROM employees WHERE email IS NOT NULL;
Output:
Note: If we’re using DISTINCT and specifying the column names explicitly then DISTINCT is applied to all the columns specified after the DISTINCT clause. Example:
SELECT DISTINCT name, email FROM employees;
DISTINCT is applied to both columns ‘name’ and ’email’.
MySQL DISTINCT Clause
MySQL is a relational database management system that can store data and we can query the stored data using SQL. SQL is a standard language to manipulate the database. The data fetching process can be applied with many filters as we might need only some specific data, or we might want to exclude some data then we can apply a filter using something called – ‘CLAUSE‘ in SQL. In this article, we’ll be discussing the ‘DISTINCT‘ clause.
Contact Us