Languages
[Edit]
EN

Node.js - PostgreSQL - find duplicated values in multiple columns

0 points
Created by:
Brian-Tompset
491

In this article, we would like to show you how to find duplicated valuesΒ in multiple columns in the PostgreSQL database usingΒ Node.js.

Node.js - PostgreSQL - find duplicated values in multiple columns
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 fetchDuplicateCollumns = async () => {
    const query = `
		SELECT 
			"name", COUNT("name") AS "name_count",
			"country", COUNT("country") AS "country_count"
		FROM "users"
		GROUP BY "name", "country"
		HAVING (COUNT("name") > 1) AND (COUNT("country") > 1);`;
    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
    }
};

fetchDuplicateCollumns(); 

Result:Β 

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ (index) β”‚  name   β”‚ name_count β”‚  country  β”‚ country_count β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚    0    β”‚  'Kim'  β”‚    '2'     β”‚ 'Vietnam' β”‚      '2'      β”‚
β”‚    1    β”‚ 'Chris' β”‚    '2'     β”‚  'Spain'  β”‚      '2'      β”‚
β”‚    2    β”‚  'Tom'  β”‚    '3'     β”‚ 'Poland'  β”‚      '3'      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Note:

Notice that ChrisΒ user bothΒ name_countΒ and country_countΒ equals 2 because one user is from a different country.

Database preparation

create_tables.sqlΒ file:

CREATE TABLE "users" (
	"id" SERIAL,
	"name" VARCHAR(100) NOT NULL,
	"email" VARCHAR(100) NOT NULL,
	"country" VARCHAR(15) NOT NULL,
	PRIMARY KEY ("id")
);

insert_data.sqlΒ file:

INSERT INTO "users"
	("name", "email", "country")
VALUES
	('Tom', 'tom1@email.com', 'Poland'),
	('Tom', 'tom2@email.com', 'Poland'),
    ('Tom', 'tom3@email.com', 'Poland'),
    ('Kim', 'kim1@email.com', 'Vietnam'),
    ('Kim', 'kim2@email.com', 'Vietnam'),
    ('Chris', 'chris1@email.com', 'Spain'),
	('Chris', 'chris2@email.com', 'Spain'),
	('Chris', 'chris3@email.com', 'USA');

Native SQL query (used in the above example):

SELECT "name", COUNT("name") AS "name_count"
FROM "users"
GROUP BY "name"
HAVING COUNT("name") > 1

Alternative titles

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