DEV Community

Cover image for Introduction to PostgreSQL (Coding Style) Part-4
Vinay Kumar Talreja
Vinay Kumar Talreja

Posted on

Introduction to PostgreSQL (Coding Style) Part-4

This blog aims to assist you in understanding the concepts of PostgreSQL with complete coding as query language.

PostgreSQL SQL commands

Note: Make sure you have Postgres installed on your system to proceed to this tutorial.

PostgreSQL - NULL Values

Null

  • In PostgreSQL, NULL signifies a missing or undefined value within a table, often appearing as an empty field.

  • NULL is distinct from zero or empty spaces, as it represents the absence of any value in a field.

Syntax:

CREATE TABLE COMPANY(
ID INT PRIMARY KEY NOT NULL,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL
);

  • Here, NOT NULL signifies that the column should always accept an explicit value of the given data type.

  • There are two columns where we did not use NOT NULL. Hence, this means these columns could be NULL.

Example:

  • The NULL value can cause problems when selecting data because when comparing an unknown value to any other value, the result is always unknown and not included in the final results.

  • Consider the following table, COMPANY having the following records −

ID NAME AGE ADDRESS SALARY
----+-------+-----+----------------------+------------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0

Query: UPDATE COMPANY SET ADDRESS = NULL, SALARY = NULL where ID IN(6,7);

Result:

id name age address salary
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000
6 Kim 22
7 James 24

IS NOT NULL

  • let us see the usage of IS NOT NULL operator to list down all the records where SALARY is not NULL −

Query: SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM COMPANY
WHERE SALARY IS NOT NULL;

Result:

id name age address salary
1 Paul 32 California 20000
2 Allen 25 Texas 15000
3 Teddy 23 Norway 20000
4 Mark 25 Rich-Mond 65000
5 David 27 Texas 85000

IS NULL

  • let us see the usage of IS NULL operator which will list down all the records where SALARY is NULL −

Query: SELECT ID, NAME, AGE, ADDRESS, SALARY
FROM COMPANY
WHERE SALARY IS NULL;

Result:

id name age address salary
6 Kim 22
7 James 24

I hope this blog has helped you understand the concepts of PostgreSQL with complete coding as a query language.

Check out a summary of Part-5.

Top comments (0)