You should use IS NOT NULL. (The comparison operators = and <> both give UNKNOWN with NULL on either side of the expression.)

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

Just for completeness I'll mention that in MySQL you can also negate the null safe equality operator but this is not standard SQL.

SELECT *
FROM table 
WHERE NOT (YourColumn <=> NULL);

Edited to reflect comments. It sounds like your table may not be in first normal form in which case changing the structure may make your task easier. A couple of other ways of doing it though...

SELECT val1 AS val
FROM  your_table
WHERE val1 IS NOT NULL
UNION ALL
SELECT val2 
FROM  your_table
WHERE val2 IS NOT NULL
/*And so on for all your columns*/

The disadvantage of the above is that it scans the table multiple times once for each column. That may possibly be avoided by the below but I haven't tested this in MySQL.

SELECT CASE idx
         WHEN 1 THEN val1
         WHEN 2 THEN val2
       END AS val
FROM   your_table
        /*CROSS JOIN*/
       JOIN (SELECT 1 AS idx
                   UNION ALL
                   SELECT 2) t
HAVING val IS NOT NULL  /*Can reference alias in Having in MySQL*/
Answer from Martin Smith on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ mysql โ€บ mysql_notnull.asp
MySQL NOT NULL Constraint
By default, a column can hold NULL values. To define a NOT NULL constraint when creating a table, add NOT NULL after the data type of the column name.
๐ŸŒ
Sqliz
sqliz.com โ€บ mysql โ€บ not-null
MySQL NOT NULL
In this article, we will discuss how to use NOT NULL constrain. In MySQL, the NOT NULL is used to constrain a column cannot include a NULL value.
Top answer
1 of 12
541

You should use IS NOT NULL. (The comparison operators = and <> both give UNKNOWN with NULL on either side of the expression.)

SELECT * 
FROM table 
WHERE YourColumn IS NOT NULL;

Just for completeness I'll mention that in MySQL you can also negate the null safe equality operator but this is not standard SQL.

SELECT *
FROM table 
WHERE NOT (YourColumn <=> NULL);

Edited to reflect comments. It sounds like your table may not be in first normal form in which case changing the structure may make your task easier. A couple of other ways of doing it though...

SELECT val1 AS val
FROM  your_table
WHERE val1 IS NOT NULL
UNION ALL
SELECT val2 
FROM  your_table
WHERE val2 IS NOT NULL
/*And so on for all your columns*/

The disadvantage of the above is that it scans the table multiple times once for each column. That may possibly be avoided by the below but I haven't tested this in MySQL.

SELECT CASE idx
         WHEN 1 THEN val1
         WHEN 2 THEN val2
       END AS val
FROM   your_table
        /*CROSS JOIN*/
       JOIN (SELECT 1 AS idx
                   UNION ALL
                   SELECT 2) t
HAVING val IS NOT NULL  /*Can reference alias in Having in MySQL*/
2 of 12
25

You can filter out rows that contain a NULL value in a specific column:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE somecolumn IS NOT NULL

If you want to filter out rows that contain a null in any column then try this:

SELECT col1, col2, ..., coln
FROM yourtable
WHERE col1 IS NOT NULL
AND col2 IS NOT NULL
-- ...
AND coln IS NOT NULL

Update: Based on your comments, perhaps you want this?

SELECT * FROM
(
    SELECT col1 AS col FROM yourtable
    UNION
    SELECT col2 AS col FROM yourtable
    UNION
    -- ...
    UNION
    SELECT coln AS col FROM yourtable
) T1
WHERE col IS NOT NULL

And I agre with Martin that if you need to do this then you should probably change your database design.

๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_ref_is_not_null.asp
SQL IS NOT NULL
ADD ADD CONSTRAINT ALL ALTER ALTER COLUMN ALTER TABLE ALTER VIEW AND ANY AS ASC BACKUP DATABASE BETWEEN CASE CHECK COLUMN CONSTRAINT CREATE CREATE DATABASE CREATE INDEX CREATE OR REPLACE VIEW CREATE TABLE CREATE PROCEDURE CREATE UNIQUE INDEX CREATE VIEW DATABASE DEFAULT DELETE DESC DISTINCT DROP DROP COLUMN DROP CONSTRAINT DROP DATABASE DROP DEFAULT DROP INDEX DROP TABLE DROP VIEW EXEC EXISTS FOREIGN KEY FROM FULL OUTER JOIN GROUP BY HAVING IN INDEX INNER JOIN INSERT INTO INSERT INTO SELECT IS NULL IS NOT NULL JOIN LEFT JOIN LIKE LIMIT NOT NOT NULL OR ORDER BY OUTER JOIN PRIMARY KEY PROCEDURE RIGHT JOIN ROWNUM SELECT SELECT DISTINCT SELECT INTO SELECT TOP SET TABLE TOP TRUNCATE TABLE UNION UNION ALL UNIQUE UPDATE VALUES VIEW WHERE MySQL Functions
๐ŸŒ
Techieclues
techieclues.com โ€บ tutorials โ€บ mysql โ€บ mysql-null
MySQL NULL: ISNULL, IS NOT NULL, IFNULL, COALESCE
March 5, 2021 - Below example, the query returns the records when the value for email column is NULL. ... We can get an exactly opposite result here when we use IS NOT NULL, it returns all non-null values.
Find elsewhere
๐ŸŒ
Dot Net Tutorials
dotnettutorials.net โ€บ home โ€บ not null constraint in mysql
NOT NULL Constraint in MySQL - Dot Net Tutorials
May 10, 2021 - By default, the database column stores NULL value means no value in the data row. By defining NOT NULL Constraint to the table column, the default behavior of the database column changes and it does not accept NULL values.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ mysql โ€บ mysql-is-not-null.htm
MySQL - IS NOT NULL Operator
UPDATE CUSTOMERS SET SALARY = 20000 ... the CUSTOMERS table. In MySQL, we can delete all the non-null rows in a specific column(s) using the DELETE statement with IS NOT NULL operator....
๐ŸŒ
W3Schools
w3schools.com โ€บ mysql โ€บ mysql_null_values.asp
MySQL NULL Values - IS NULL and IS NOT NULL
If a field in a table is optional, it is possible to insert or update a record without adding any value to this field. This way, the field will be saved with a NULL value. A NULL value represents an unknown, missing, or inapplicable data in a database field...
๐ŸŒ
i2tutorials
i2tutorials.com โ€บ home โ€บ mysql โ€“ tutorial โ€บ mysql โ€“ is not null
MySQL - IS NOT NULL | i2tutorials
February 23, 2023 - Using the MySQL IS NOT NULL condition, we can verify that the expression doesn't contain any NULL values. This statement is used with the SELECT, INSERT, UPDATE, and DELETE statements.
๐ŸŒ
W3Schools
w3schools.com โ€บ mysql โ€บ mysql_constraints.asp
MySQL Constraints
MySQL Create DB MySQL Drop DB MySQL Create Table MySQL Drop Table MySQL Alter Table MySQL Constraints MySQL Not Null MySQL Unique MySQL Primary Key MySQL Foreign Key MySQL Check MySQL Default MySQL Create Index MySQL Auto Increment MySQL Dates MySQL Views MySQL Injection MySQL Prepared Statements
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_mysql_ifnull.asp
MySQL IFNULL() Function
ADD ADD CONSTRAINT ALL ALTER ALTER COLUMN ALTER TABLE ALTER VIEW AND ANY AS ASC BACKUP DATABASE BETWEEN CASE CHECK COLUMN CONSTRAINT CREATE CREATE DATABASE CREATE INDEX CREATE OR REPLACE VIEW CREATE TABLE CREATE PROCEDURE CREATE UNIQUE INDEX CREATE VIEW DATABASE DEFAULT DELETE DESC DISTINCT DROP DROP COLUMN DROP CONSTRAINT DROP DATABASE DROP DEFAULT DROP INDEX DROP TABLE DROP VIEW EXEC EXISTS FOREIGN KEY FROM FULL OUTER JOIN GROUP BY HAVING IN INDEX INNER JOIN INSERT INTO INSERT INTO SELECT IS NULL IS NOT NULL JOIN LEFT JOIN LIKE LIMIT NOT NOT NULL OR ORDER BY OUTER JOIN PRIMARY KEY PROCEDURE RIGHT JOIN ROWNUM SELECT SELECT DISTINCT SELECT INTO SELECT TOP SET TABLE TOP TRUNCATE TABLE UNION UNION ALL UNIQUE UPDATE VALUES VIEW WHERE MySQL Functions
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ sql_ref_not_null.asp
SQL NOT NULL
ADD ADD CONSTRAINT ALL ALTER ALTER COLUMN ALTER TABLE ALTER VIEW AND ANY AS ASC BACKUP DATABASE BETWEEN CASE CHECK COLUMN CONSTRAINT CREATE CREATE DATABASE CREATE INDEX CREATE OR REPLACE VIEW CREATE TABLE CREATE PROCEDURE CREATE UNIQUE INDEX CREATE VIEW DATABASE DEFAULT DELETE DESC DISTINCT DROP DROP COLUMN DROP CONSTRAINT DROP DATABASE DROP DEFAULT DROP INDEX DROP TABLE DROP VIEW EXEC EXISTS FOREIGN KEY FROM FULL OUTER JOIN GROUP BY HAVING IN INDEX INNER JOIN INSERT INTO INSERT INTO SELECT IS NULL IS NOT NULL JOIN LEFT JOIN LIKE LIMIT NOT NOT NULL OR ORDER BY OUTER JOIN PRIMARY KEY PROCEDURE RIGHT JOIN ROWNUM SELECT SELECT DISTINCT SELECT INTO SELECT TOP SET TABLE TOP TRUNCATE TABLE UNION UNION ALL UNIQUE UPDATE VALUES VIEW WHERE MySQL Functions
๐ŸŒ
MariaDB
mariadb.com โ€บ docs โ€บ server โ€บ reference โ€บ sql-structure โ€บ operators โ€บ comparison-operators โ€บ is-not-null
IS NOT NULL | Server | MariaDB Documentation
See also NULL Values in MariaDB. ... SELECT 1 IS NOT NULL, 0 IS NOT NULL, NULL IS NOT NULL; +---------------+---------------+------------------+ | 1 IS NOT NULL | 0 IS NOT NULL | NULL IS NOT NULL | +---------------+---------------+------------------+ | 1 | 1 | 0 | +---------------+---------------+------------------+
๐ŸŒ
IncludeHelp
includehelp.com โ€บ mysql โ€บ mysql-is-not-null-operator.aspx
MySQL IS NOT NULL Operator
You can simply use this operator with other operators, as you can see the gender column also has null values so we can write a condition that will give us all the values excluding null values from the student department and gender column, in case we will use AND operator between two IS NOT NULL condition.
๐ŸŒ
MySQL Tutorial
mysqltutorial.org โ€บ home โ€บ mysql basics โ€บ mysql not null constraint
MySQL NOT NULL Constraint
January 30, 2024 - Third, modify the column with a NOT NULL constraint. Consider the following example. ... INSERT INTO tasks(title ,start_date, end_date) VALUES('Learn MySQL NOT NULL constraint', '2017-02-01','2017-02-02'), ('Check and update NOT NULL constraint to your database', '2017-02-01',NULL);Code language: SQL (Structured Query Language) (sql)
Top answer
1 of 2
13

It actually doesn't accept NULL values it considers it as empty string. That's because you have your server in non-strict mode. That controls how MySQL handles invalid or missing values in inserts and updates. You can read more about modes here: http://dev.mysql.com/doc/refman/5.6/en/sql-mode.html#sql-mode-strict

mysql> insert into cities(state_id) values (20);
Query OK, 1 row affected, 1 warning (0.07 sec)

mysql> show warnings;
+---------+------+-------------------------------------------+
| Level   | Code | Message                                   |
+---------+------+-------------------------------------------+
| Warning | 1364 | Field 'name' doesn't have a default value |
+---------+------+-------------------------------------------+

mysql> select name is null from cities where id = LAST_INSERT_ID();
+--------------+
| name is null |
+--------------+
|            0 |
+--------------+
1 row in set (0.00 sec)

mysql> SET sql_mode = 'STRICT_ALL_TABLES';
Query OK, 0 rows affected (0.00 sec)

mysql> insert into cities(state_id) values (20);
ERROR 1364 (HY000): Field 'name' doesn't have a default value
2 of 2
7

To Kรกroly Nagy's point,

You can use

SET SQL_MODE = 'STRICT_ALL_TABLES';

I'm sure you may already know but that is equivilent to:

SET @@SESSION.SQL_MODE = 'STRICT_ALL_TABLES';

Which, must be set for every session. And you can checky this by running this after the above statement.

SELECT @@SESSION.SQL_MODE; -- 'STRICT_ALL_TABLES'

SELECT @@GLOBAL.SQL_MODE; -- 'NO_ENGINE_SUBSTITUTION'

If you have access please set this at the global level:

SET @@GLOBAL.SQL_MODE = 'STRICT_ALL_TABLES';

Which becomes the default for every new session thereafter.

Cheers, Jay ;-]

NOTE: Tested On MySQL Version 5.6.23-log & MySQL Workbench 6.2.5

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ set-not-null-attribute-to-an-existing-column-in-mysql
Set NOT NULL attribute to an existing column in MySQL
December 31, 2019 - Here is the query to set NOT NULL attribute to an existing column โˆ’ ยท mysql> alter table DemoTable1949 modify UserName varchar(20) not null; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ why-mysql-not-null-shouldn-t-be-added-to-primary-key-field
Why MySQL NOT NULL shouldn't be added to primary key field?
July 30, 2019 - You do not need to add NOT NULL to primary key field because it gets NOT NULL automatically. Primary key is combination of both NOT NULL and Unique Key. Here is the demo of primary key field.