Languages
[Edit]
EN

Node.js - PostgreSQL - find row with null value in column

0 points
Created by:
Brian-Tompset
491

In this article, we would like to show you how to find the row with a null value in a column in the PostgreSQL database usingΒ Node.js.

Node.js - PostgreSQL - find row with null value in column
Data used in the example - HeidiSQL

Note:Β at the end of this article you can find database preparation SQL queries.

const { Client } = require('pg');

const client = new Client({
    host: '127.0.0.1',
    user: 'postgres',
    database: 'database_name',
    password: 'password',
    port: 5432,
});

const getRowWithoutEmail = async () => {
    const query = `SELECT * 
				   FROM "users"
				   WHERE "email" IS NULL;`;
    try {
        await client.connect();  // creates connection
        const { rows } = await client.query(query); // sends query
		console.table(rows);
    } catch (error) {
        console.error(error.stack);
    } finally {
        await client.end();      // closes connection
    }
};

getRowWithoutEmail();

Result:Β 

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚ id β”‚    name     β”‚  surname  β”‚ email β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚ 2  β”‚   'Chris'   β”‚  'Brown'  β”‚ null  β”‚
β”‚    1    β”‚ 3  β”‚   'Kate'    β”‚  'Lewis'  β”‚ null  β”‚
β”‚    2    β”‚ 5  β”‚ 'Gwendolyn' β”‚  'James'  β”‚ null  β”‚
β”‚    3    β”‚ 6  β”‚   'Simon'   β”‚ 'Collins' β”‚ null  β”‚
β”‚    4    β”‚ 7  β”‚  'Taylor'   β”‚ 'Martin'  β”‚ null  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”˜

Database preparation

create_tables.sqlΒ file:

CREATE TABLE "users" (
	"id" SERIAL,
	"name" VARCHAR(50) NOT NULL,
	"surname" VARCHAR(50) NOT NULL,
	"email" VARCHAR(50),
	PRIMARY KEY ("id")
);

insert_data.sqlΒ file:

INSERT INTO "users"
	( "name", "surname", "email")
VALUES
	('John', 'Stewart', 'john@email.com'),
	('Chris', 'Brown', NULL),
	('Kate', 'Lewis', NULL),
	('Ailisa', 'Gomez', 'ailisa@email.com'),
	('Gwendolyn', 'James', NULL),
	('Simon', 'Collins', NULL),
	('Taylor', 'Martin', NULL),
	('Andrew', 'Thompson', 'andrew@email.com');

Native SQL query (used in the above example):

SELECT * 
FROM "users"
WHERE "email" IS NULL
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Node.js - PostgreSQL - Problems

Native Advertising
πŸš€
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

β€οΈπŸ’» πŸ™‚

Join