4

I have a table called table1

It has 100 columns: {col1, col2, ...,col100}

I understand how to SELECT rows not containing null values in a specific column for instance col1:

SELECT *
FROM table1
WHERE col1 IS NOT NULL

How do I SELECT all rows that do not contain null values in any column

Attempt

SELECT *
FROM table1
WHERE * IS NOT NULL

but this returns an error in MySQL (which I am using)

8
  • 4
    WHERE col1 IS NOT NULL AND col2 IS NOT NULL AND ..... Commented Apr 13, 2021 at 14:18
  • 1
    You mean there should not be any single null in any of the column/row? Commented Apr 13, 2021 at 14:18
  • 2
    If you have 100 columns to check, you will write 100 x and colx is not null Commented Apr 13, 2021 at 14:19
  • 3
    100 columns?!? Perhaps time to redesign the database. Commented Apr 13, 2021 at 14:19
  • 2
    If they are all numeric data type columns, you can save some typing: WHERE col1 + col2 + ... + col100 IS NOT NULL. Commented Apr 13, 2021 at 14:37

4 Answers 4

6

You need to explicitly list each column. I would recommend:

select t.*
from t
where col1 is not null and col2 is not null and . . . 

Some people might prefer a more concise (but slower) method such as:

where concat(col1, col2, col3, . . . ) is not null

This is not actually a simple way to express this, although you can construct the query using metadata table or a spreadsheet.

Sign up to request clarification or add additional context in comments.

Comments

2

While I would recommend writing out each column name and refactoring your table as suggested, here's an option using dynamic sql:

SET @sql = NULL;
SELECT CONCAT('SELECT * FROM table1 WHERE ', 
              GROUP_CONCAT(c.COLUMN_NAME SEPARATOR ' IS NOT NULL AND '), 
              ' IS NOT NULL') INTO @sql
FROM INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = 'table1'
ORDER BY c.ORDINAL_POSITION;

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

Comments

1

To return rows with no nulls in any columns: (in PostgresSQL)

SELECT *  
FROM *tablename*  
WHERE *tablename*.* IS NOT NULL

4 Comments

This wont work.
To VLQ Queue reviewers: Wrong answers are not Very Low Quality. Please vote "Looks OK".
@Shmiel Thanks for spotting! This code works in Postgres SQL not mysql db fiddle postgres
0

Get the length of each column and multiply them. If the result is not null then all columns are non null values. Try following sample code. Since I am not using multiple AND clause, it is a better approach.

CREATE TABLE #Temp (OdSal INT,NewSal INT,EmpName CHAR(20),IsCurrentEmp BIT)

INSERT INTO #Temp VALUES
(100,150,'Vikas',1),        -- No null records
(NULL,NULL,NULL,NULL),      -- all records are null
(NULL,NULL,'Nayanthara',1), -- more than 1 column is null
(NULL,150,'Priyamani',1)    -- only one column is null

--Will return only one row
SELECT * FROM #Temp
WHERE LEN(OdSal) * LEN(NewSal) * LEN(EmpName) * LEN(IsCurrentEmp) IS NOT NULL

Result of above code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.