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
3
votes
3 answers

Get the percentage of row with TEXT type

I am trying to calculate the percentage of the row events when the value is null. I have a table with 2 rows (zip_code, events). TABLE weather events TEXT zip_code INTEGER This is the query I wrote. I am just getting the count of all the events…
serafm
  • 59
  • 7
3
votes
1 answer

How do I add conditional check after AND statement in SQL?

I want to check if certain column exists after AND statement and if it does, add another AND statement. What would be the best way to achieve this? For example: SELECT * FROM db.table AS t WHERE t.value < 100 AND t.another = 'hi' …
passionateLearner
  • 722
  • 1
  • 7
  • 19
3
votes
1 answer

Add NOT NULL column without DEFAULT but WITH VALUES

I'm using SQL Server 2017 and I want to add a NOT NULL column without DEFAULT but supply a values for current record e.g. using WITH VALUES in a single query. Let me explain. I understand the fact that I cannot create a NOT NULL column without…
zolv
  • 1,720
  • 2
  • 19
  • 36
3
votes
1 answer

SQL Hive - Replace null values with 0 (Hadoop Hive)

After conducting a left join, I was left with many null (empty) values. How may I replace those null values with a 0 in only certain columns within the same query? select m1.*, t2.Apple, t3.Berry, t4.Cherry from table1 as t1 left join…
Logan
  • 293
  • 3
  • 11
3
votes
2 answers

SELECT MAX(...) incorrectly returns NULL in stored procedure

I have a table, ga_sum_1, with a column, created_timestamp in it. When I execute the following query from the mysql command line: mysql> select max(created_timestamp) from ga_sum_1; +------------------------+ | max(created_timestamp)…
j4nd3r53n
  • 680
  • 2
  • 11
  • 26
3
votes
1 answer

Grafana dashboard variable - handling NULL values in Query-based variable

I've created a Grafana dashboard variable (multi-select enabled) based on a MySQL query: SELECT DISTINCT(my_field) from my_table This field has about 12 options, one of them being a NULL value. When editing my dashboard widgets, I include a SQL…
radicand
  • 6,068
  • 3
  • 27
  • 22
3
votes
2 answers

Difference between (NULL) and empty in mysql column values

I am working in a company where we use Spring -Hibernate and mysql database at backend. There is a table tc_wallet In this table i have an column tips_type which has values Collection,Payable '' (empty) NULL --> No value has…
Ajinkya Karode
  • 177
  • 1
  • 4
  • 18
3
votes
2 answers

SQL Count NULL values is 0 even when there are NULL values

In a particular case I have a database table populated with 1070 items, and at some point in the process I add a column called 'Current_Status' to it. As a result, all items have a new field that is initially NULL. This table is used as a queue, and…
Pedro Zampella
  • 81
  • 1
  • 11
3
votes
3 answers

How null is treated using union and union all in mysql?

based on below statement select null union select null the output of the above statement is: null While this statement : select null union all select null Outputs: null null As null <> null then how the value null is treated here and in which…
ashish
  • 239
  • 2
  • 6
  • 13
3
votes
2 answers

How to alter new not null column in Postgresql?

I have a table that I want to add new not null varchar(255) column My query is: alter table poll_management.DASHLETS add column name varchar(255) not null; update poll_management.DASHLETS as dashlet set name = report.name from…
Mr White
  • 97
  • 1
  • 9
2
votes
2 answers

Need a SQL select statement to return rows that have the same id in one column and distinct value in another column

I have a table that contains a group number column and a data column: GROUP DataColumn 1 NULL 1 NULL 1 "hello" 1 NULL 2 "bye" 2 "sorry" 3 NULL 3 NULL 3 NULL I want to return the string in the DataColunm as long as all…
2
votes
0 answers

Passing NULL value into the SQL server with Android Studio

How do I accept null values into the SQL Server database with Kotlin? I am trying to make that if the user does not input anything into the column, it will return null instead in the SQL Server. But I failed to do so and a bunch of numbers is saved…
2
votes
2 answers

In SQLite, how do I query the changed values in two tables, and NULL out the matching values

If I have two rows in two different tables such as: Table Name - Old id title views wx How to clean a drill 30 np Craziest Fails 400 zo Eating Challenge 8 lf JavaScript Tutorial 0 Table Name - New id title views wx How to…
waivek
  • 193
  • 1
  • 5
2
votes
1 answer

MYSQL, how can I get rows in conditions

I have a structure role_approvers: id, role_id, level, user_id, approval_date Rows: 4116, 1300, 1, 1158, 2021-05-28 14:24:34 4117, 1300, 1, 1186, NULL 4596, 1300, 2, 21, NULL I need to get MIN level where role_id = 1300 and ALL approval_date on…
2
votes
1 answer

TSQL with NULL NOT IN clause, will it ever fail or no?

While submitting code for my graded project I was pointed that this logic with NOT IN could fail if any Flag has NULL value. I've did my homework and testing and could not confirm this, my query below always works fine even dealing with NULL. Is…
Mich28
  • 519
  • 3
  • 14
1
2
3
16 17