-- Create the friendship table
CREATE TABLE friendship (
    person1 VARCHAR(50),
    person2 VARCHAR(50),
    PRIMARY KEY (person1, person2)
);

-- Insert sample data
INSERT INTO friendship (person1, person2) VALUES
('Alice', 'Bob'),
('Alice', 'Charlie'),
('Bob', 'David'),
('Charlie', 'David'),
('David', 'Eve'),
('Eve', 'Frank'),
('Frank', 'George'),
('George', 'Alice'),
('Henry', 'Alice'),
('Henry', 'Bob'),
('Henry', 'Charlie'),
('Ivy', 'Alice'),
('Ivy', 'Bob'),
('Ivy', 'Charlie'),
('Ivy', 'David');

-- Create indexes for performance
CREATE INDEX idx_friendship_person1 ON friendship(person1);
CREATE INDEX idx_friendship_person2 ON friendship(person2);

-- The optimized query
WITH RECURSIVE
-- Base friend relation
friends AS (
    SELECT DISTINCT person1, person2
    FROM friendship
    UNION
    SELECT person2, person1
    FROM friendship
),

-- Friend of friend relation
friend_of_friend AS (
    SELECT f1.person1 AS person, f2.person2 AS friend_of_friend
    FROM friends f1
    JOIN friends f2 ON f1.person2 = f2.person1
    WHERE f1.person1 <> f2.person2
),

-- Potential friend recommendations
potential_recommendations AS (
    SELECT fof.person, fof.friend_of_friend, 
           COUNT(*) AS mutual_friend_count
    FROM friend_of_friend fof
    LEFT JOIN friends f ON fof.person = f.person1 AND fof.friend_of_friend = f.person2
    WHERE f.person1 IS NULL  -- Ensure they're not already friends
    GROUP BY fof.person, fof.friend_of_friend
    HAVING COUNT(*) >= 2  -- Lowered threshold for this small dataset
),

-- Rank recommendations
ranked_recommendations AS (
    SELECT person, friend_of_friend, mutual_friend_count,
           RANK() OVER (PARTITION BY person ORDER BY mutual_friend_count DESC) as rank
    FROM potential_recommendations
)

-- Get top recommendations
SELECT person, friend_of_friend, mutual_friend_count
FROM ranked_recommendations
WHERE rank = 1
ORDER BY person; 

PostgreSQL online editor

Write, Run & Share PostgreSQL queries online using OneCompiler's PostgreSQL online editor and compiler for free. It's one of the robust, feature-rich online editor and compiler for PostgreSQL. Getting started with the OneCompiler's PostgreSQL editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose database as 'PostgreSQL' and start writing queries to learn and test online without worrying about tedious process of installation.

About PostgreSQL

PostgreSQL is a open source relational database system and is also knows as Postgres.

Key Features:

  • Postgres is not only free and open-source but also it is highly extensible.
  • Custom Data types and funtions from various programming languaues can be introduced and the good part is compiling entire database is not required.
  • ACID(Atomicity, Consistency, Isolation, Durability) compliant.
  • First DBMS which implemented Multi-version concurrency control (MVCC) feature.
  • It's the default database server for MacOS.
  • It supports all major operating systems like Linux, Windows, OpenBSD,FreeBSD etc.

Syntax help

1. CREATE

CREATE command is used to create a table, schema or an index.

Syntax:

         CREATE TABLE table_name (
                column1 datatype,
                column2 datatype,
                ....);

2. ALTER

ALTER command is used to add, modify or delete columns or constraints from the database table.

Syntax

ALTER TABLE Table_name ADD column_name datatype;

3. TRUNCATE:

TRUNCATE command is used to delete the data present in the table but this will not delete the table.

Syntax

TRUNCATE table table_name;

4. DROP

DROP command is used to delete the table along with its data.

Syntax

DROP TABLE table_name;

5. RENAME

RENAME command is used to rename the table name.

Syntax

ALTER TABLE table_name1 RENAME to new_table_name1; 

6. INSERT

INSERT Statement is used to insert new records into the database table.

Syntax

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

7. SELECT

Select statement is used to select data from database tables.

Syntax:

SELECT column1, column2, ...
FROM table_name; 

8. UPDATE

UPDATE statement is used to modify the existing values of records present in the database table.

Syntax

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition; 

9. DELETE

DELETE statement is used to delete the existing records present in the database table.

Syntax

DELETE FROM table_name where condition;