Project: Social App — Followers & Feed
Schema
sa_users(username UNIQUE)follows(follower_id, followee_id) – self-referencing many-to-many, PK on both,CHECK (follower_id <> followee_id)posts(author, body, created_at)likes(user, post) – PK on both (one like per user per post)
Queries that power the app
- Home feed: posts from people I follow, newest first, with like counts and "did I like it?" — with keyset pagination
- Follower and following counts per user
- Mutual follows (friends): A follows B and B follows A
- "People you may know": followed by the people I follow, but not by me yet (friends-of-friends)
- Trending posts: most likes in the last 24 hours
- Has a user liked a post? (
EXISTS)
Design and performance points
- Index
follows (followee_id)as well as the PK(follower_id, followee_id)— you query both directions - Index
posts (author_id, created_at DESC)for feeds - Counters (likes, followers) can be denormalized with triggers at scale
- RLS in Supabase: anyone can read public posts; only the author can edit/delete; users can only like as themselves
Stretch goals
Comments with replies (recursive CTE), hashtags (array + GIN index), notifications table filled by triggers, blocking users, Realtime subscriptions for new posts
Example
CREATE TABLE sa_users (id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, username TEXT UNIQUE NOT NULL);
CREATE TABLE sa_follows (
follower_id INT REFERENCES sa_users(id) ON DELETE CASCADE,
followee_id INT REFERENCES sa_users(id) ON DELETE CASCADE,
PRIMARY KEY (follower_id, followee_id),
CHECK (follower_id <> followee_id) -- can't follow yourself
);
CREATE INDEX ON sa_follows (followee_id);
CREATE TABLE sa_posts (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
author_id INT NOT NULL REFERENCES sa_users(id) ON DELETE CASCADE,
body TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX ON sa_posts (author_id, created_at DESC);
CREATE TABLE sa_likes (
user_id INT REFERENCES sa_users(id) ON DELETE CASCADE,
post_id INT REFERENCES sa_posts(id) ON DELETE CASCADE,
PRIMARY KEY (user_id, post_id)
);
INSERT INTO sa_users (username) VALUES ('asha'), ('ravi'), ('sita'), ('bikash'), ('maya');
INSERT INTO sa_follows VALUES (1,2), (1,3), (2,1), (2,4), (3,1), (3,4), (3,5), (4,2);
INSERT INTO sa_posts (author_id, body, created_at) VALUES
(2, 'Paragliding in Pokhara! 🪂', '2026-11-23 09:00+05:45'),
(3, 'Saved 20% of my salary this month', '2026-11-23 18:30+05:45'),
(4, 'Best momo place in Lalitpur?', '2026-11-24 08:15+05:45'),
(3, 'Budget tip: track every rupee', '2026-11-24 10:00+05:45'),
(5, 'Hello world', '2026-11-24 11:00+05:45');
INSERT INTO sa_likes VALUES (1,1), (3,1), (4,1), (1,2), (2,2), (1,4), (5,4), (2,3);
-- 1. Asha's home feed: people she follows, newest first, likes + "liked by me"
SELECT u.username, p.body, p.created_at,
(SELECT count(*) FROM sa_likes l WHERE l.post_id = p.id) AS likes,
EXISTS (SELECT 1 FROM sa_likes l WHERE l.post_id = p.id AND l.user_id = 1) AS liked_by_me
FROM sa_posts p
JOIN sa_follows f ON f.followee_id = p.author_id AND f.follower_id = 1
JOIN sa_users u ON u.id = p.author_id
ORDER BY p.created_at DESC, p.id DESC
LIMIT 10;
-- sita: Budget tip … | sita: Saved 20% … | ravi: Paragliding …
-- 2. Follower / following counts
SELECT u.username,
(SELECT count(*) FROM sa_follows f WHERE f.followee_id = u.id) AS followers,
(SELECT count(*) FROM sa_follows f WHERE f.follower_id = u.id) AS following
FROM sa_users u ORDER BY followers DESC, u.username;
-- 3. Mutual follows (friends), each pair once
SELECT a.username AS user_1, b.username AS user_2
FROM sa_follows f1
JOIN sa_follows f2 ON f2.follower_id = f1.followee_id AND f2.followee_id = f1.follower_id
JOIN sa_users a ON a.id = f1.follower_id
JOIN sa_users b ON b.id = f1.followee_id
WHERE f1.follower_id < f1.followee_id;
-- asha ↔ ravi | asha ↔ sita | ravi ↔ bikash
-- 4. People Asha may know: followed by people she follows, not followed by her yet
SELECT u.username, count(*) AS mutual_connections
FROM sa_follows mine
JOIN sa_follows theirs ON theirs.follower_id = mine.followee_id
JOIN sa_users u ON u.id = theirs.followee_id
WHERE mine.follower_id = 1
AND theirs.followee_id <> 1
AND NOT EXISTS (SELECT 1 FROM sa_follows x WHERE x.follower_id = 1 AND x.followee_id = theirs.followee_id)
GROUP BY u.username
ORDER BY mutual_connections DESC;
-- bikash 2 (via ravi and sita) | maya 1
-- 5. Trending: most-liked posts
SELECT p.body, count(l.user_id) AS likes
FROM sa_posts p LEFT JOIN sa_likes l ON l.post_id = p.id
GROUP BY p.id ORDER BY likes DESC, p.created_at DESC LIMIT 3;