Questions tagged [sql-null]

In the SQL query languages of relational databases, NULL is a special value for "unknown". Use that tag for questions concerning the SQL NULL value. Please also use the "ansi-sql" tag if your question is about the SQL standard or a tag that indicates which database you are using.

In , “NULL” stands for the “unknown” value.

In a , it has a special , so that the following holds:

  • NULL AND X is the same as NULL
  • NULL OR X is the same as X
  • NOT NULL is the same as NULL

Also, most s and , when operating on a NULL (unknown) , will produce a NULL result.

That holds for the in particular, so the X = NULL will not produce TRUE or FALSE, but NULL.

Use x IS NULL to test for NULL-ness and X IS NOT DISTINCT FROM Y to test if X and Y are either both NULL or both not NULL and equal.

database deviates from the SQL standard by treating empty strings as NULL, so don't rely on that if you want to write SQL.

Two guidelines for proper use of NULL:

  • Define your database columns as NOT NULL (which is not the default) wherever possible.

    This improves the quality of your data and makes your queries simpler and consequently faster, since they don't have to deal with the oddities of NULL.

    It is trivial to change a column from to , but not vice versa!

  • Use NULL for unknown values, not for values that are known to be absent, infinite values and the like. That will make your SQL intuitively do the right thing.

    For example, a missing comment had better be an empty string than a NULL, so that string operations work as expected.

    Infinite values are better represented by infinity (if your SQL dialect supports that) or values beyond the normal range, so that comparisons have the intended result.

242 questions
-1
votes
1 answer

Find rows with same non-null values

This seems like a rather straightforward problem, yet I have not been able to find the solution: In a table test, I have some subset of columns which I am interested in, say a,b,c,d,e,f. Some or most of these columns are NULL, but at least one is…
Radio Controlled
  • 825
  • 8
  • 23
-1
votes
1 answer

SQL Join query returns null instead of names

I'm trying to join two tables to get the video title that is not borrowed in the copy table, and I think I've done everything correctly except the table values return null? Instead of the actual names it just says null, I'm new to SQL and don't…
poop
  • 5
  • 3
-1
votes
2 answers

How can I filter out subtotals when using WITH ROLLUP in MySQL?

I'm trying to use GROUP ... WITH ROLLUP to get a series of totals for multiple grouped values, but without subtotals. So in my contrived example, I have a table fruits which looks like this: SELECT * FROM fruits ORDER BY…
Lou
  • 2,200
  • 2
  • 33
  • 66
-1
votes
1 answer

Combining multiple columns into one new column, while keeping the original column

I have below table, where I am trying to merge the columns in yellow in one column, while maintaining the original columns; the data in the highlighted yellow columns is populated based on the interaction type they fall into if the interaction type…
NewCode
  • 109
  • 1
  • 8
-1
votes
1 answer

select conditionally with conditional joins in mysql

I am trying to join two tables, people and sales, and displaying results based on a where condition which should be used to join the tables. My current attempt is showing only one result but I want all the rows in the people to be shown regardless…
Hamza Zahir
  • 69
  • 1
  • 7
-1
votes
1 answer

SQL: Is there a way for me to call another table.field when my initial table.field = NULL?

Morning All! Essentially, I found out that a potential table.field I'd like to use may not have the information I want, so, I might have to look into a separate field. My below thought process is: If table.field1 = NULL then pull the value from…
-1
votes
1 answer

SQL Server find results within partition

I have the following table: ID Date ------------------- 1 Null 1 1/2/2020 2 Null 2 12/2/2020 3 Null For every ID which has at least one non-null date, I need to classify as 'accounted'. Result set should look like…
user10
  • 187
  • 2
  • 11
-1
votes
2 answers

Show all rows with 0 if no records found on second table

I need to see all the records from table A, if no matching record found on table B then show 0 with corresponding value of A. SELECT r.date , l.total_user FROM daterange r LEFT JOIN logs l ON r.date = l.created_date WHERE r.date…
-1
votes
1 answer

SQLite: Order by number of NULL values

In SQLite, is there any way of ordering by the number of NULL values in each row, without having stored this number explicitly in a separate column? Something like SELECT rowid FROM (SELECT rowid, COUNT_NULLS(column_1,column_2,...,column_n) AS…
Radio Controlled
  • 825
  • 8
  • 23
-1
votes
1 answer

Query not returning expected data

I'm looking for an explanation for why 1 of the following 3 queries aren't returning what I am expecting. -- Query 1 SELECT ANNo, ANCpr FROM Anmodning WHERE LEFT(ANCpr,6) + '-' + RIGHT(ANCpr,4) NOT IN (SELECT PSCpr FROM Person) -- Query 2 SELECT…
Kenneth Lauritsen
  • 335
  • 1
  • 3
  • 11
-1
votes
1 answer

How to deal with Dynamic Queries

Simple Question I have a table that looks like this I am looking to do is do a select so I can get a result as Is this a PIVOTING issue? looking for suggestions to see how I can form a 'Select' query for this?
fireholster
  • 622
  • 1
  • 9
  • 22
-1
votes
2 answers

Can't join two temp tables

I've been trying to join two temp tables but I keep getting an error message saying "Ambiguous column name" even though I used column alias. FYI, this is SQL Server programming. select ID, count(PageNum) as Frequency into temp_list from…
user14193972
-1
votes
1 answer

Query two tables where one of them has been transposed

I have the following tables, USER and RECORDS. The USER table simply has the name of the user and the group they have access to. If a user has null as their group (such as BOB does), that means they are a super user and have access to all the…
user2924127
  • 6,034
  • 16
  • 78
  • 136
-1
votes
2 answers

Allays return value from left table in join

I have 2 tables A and B A has cols(AKey, val1, val2) B has Cols(BKey,Akey, ValX, valY) i have the following query select a.Val1,a.Val2,b.ValX from A Left Join B on a.AKey = b.Akey where a.Akey ={someValue} and ((b.valY…
Pythonuser
  • 203
  • 1
  • 11
-1
votes
2 answers

MySQL. How to combine or match specifics rows from two tables

First, the title is not very clear. With this example I would like to know if there is a solution with SQL code or if it has to be worked in the other side with C#, Java, PHP, etc. The principle is this: There is a table of Inputs, like this: …
EveRegalado
  • 105
  • 1
  • 8