{"id":20796472,"url":"https://github.com/pawandai/instagram-db-clone","last_synced_at":"2026-02-14T01:05:06.859Z","repository":{"id":255619399,"uuid":"852354889","full_name":"pawandai/Instagram-DB-Clone","owner":"pawandai","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-10T13:58:48.000Z","size":600,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-09-03T22:40:55.324Z","etag":null,"topics":["dbms","sql"],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pawandai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-09-04T17:00:48.000Z","updated_at":"2024-09-10T14:01:24.000Z","dependencies_parsed_at":"2024-09-10T15:24:31.101Z","dependency_job_id":null,"html_url":"https://github.com/pawandai/Instagram-DB-Clone","commit_stats":null,"previous_names":["pawandai/instagram-db-clone"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/pawandai/Instagram-DB-Clone","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pawandai%2FInstagram-DB-Clone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pawandai%2FInstagram-DB-Clone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pawandai%2FInstagram-DB-Clone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pawandai%2FInstagram-DB-Clone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pawandai","download_url":"https://codeload.github.com/pawandai/Instagram-DB-Clone/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pawandai%2FInstagram-DB-Clone/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29427698,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-13T22:20:51.549Z","status":"ssl_error","status_checked_at":"2026-02-13T22:20:49.838Z","response_time":78,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dbms","sql"],"created_at":"2024-11-17T16:27:22.790Z","updated_at":"2026-02-14T01:05:06.843Z","avatar_url":"https://github.com/pawandai.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# Basic Instagram Database Clone\n*(source: google)*\n\nThis repository contains the schema for a simplified clone of Instagram's database that I tried to create based on everything I've learned so far. The schema is designed to store users, photos, comments, likes, follows, and tags, with appropriate normalization and relationships between entities.\n\n## Entity-Relationship (ER) Diagram\n\n![ER Diagram](ER_Diagram.svg)\n\n## Database Structure\n\n### 1. Users Table\nThis table stores the details of users in the application.\n\n- **id**: The primary key for the user.\n- **username**: A unique username for each user.\n- **createdAt**: The timestamp when the user was created.\n\n```sql\nCREATE TABLE users (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    username VARCHAR(255) UNIQUE NOT NULL,\n    createdAt TIMESTAMP DEFAULT NOW()\n);\n```\n\n### 2. Photos Table\nThis table stores photos uploaded by users.\n\n- **id**: The primary key for the photo.\n- **imageUrl**: The URL of the photo.\n- **userId**: A foreign key that references the `users` table.\n- **createdAt**: The timestamp when the photo was uploaded.\n\n```sql\nCREATE TABLE photos (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    imageUrl VARCHAR(255) NOT NULL,\n    userId INT NOT NULL,\n    FOREIGN KEY(userId) REFERENCES users(id),\n    createdAt TIMESTAMP DEFAULT NOW()\n);\n```\n\n### 3. Comments Table\nThis table stores comments made by users on photos.\n\n- **id**: The primary key for the comment.\n- **title**: The content of the comment.\n- **userId**: A foreign key that references the `users` table.\n- **photoId**: A foreign key that references the `photos` table.\n- **createdAt**: The timestamp when the comment was made.\n\n```sql\nCREATE TABLE comments (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    title VARCHAR(255) NOT NULL,\n    userId INT NOT NULL,\n    FOREIGN KEY(userId) REFERENCES users(id),\n    photoId INT NOT NULL,\n    FOREIGN KEY(photoId) REFERENCES photos(id),\n    createdAt TIMESTAMP DEFAULT NOW()\n);\n```\n\n### 4. Likes Table\nThis table stores likes made by users on photos.\n\n- **userId**: A foreign key that references the `users` table.\n- **photoId**: A foreign key that references the `photos` table.\n- **createdAt**: The timestamp when the like was made.\n- The combination of `userId` and `photoId` forms the primary key, also known as **composite key**.\n\n```sql\nCREATE TABLE likes (\n    userId INT NOT NULL,\n    FOREIGN KEY(userId) REFERENCES users(id),\n    photoId INT NOT NULL,\n    FOREIGN KEY(photoId) REFERENCES photos(id),\n    PRIMARY KEY(userId, photoId),\n    createdAt TIMESTAMP DEFAULT NOW()\n);\n```\n\n### 5. Follows Table\nThis table stores follow relationships between users.\n\n- **followerId**: A foreign key that references the `users` table (the follower, ie. a person who is follows other).\n- **followeeId**: A foreign key that references the `users` table (the followee, ie. a person who is followed by others).\n- **createdAt**: The timestamp when the follow relationship was created.\n- The combination of `followerId` and `followeeId` forms the primary key.\n\n```sql\nCREATE TABLE follows (\n    followerId INT NOT NULL,\n    FOREIGN KEY(followerId) REFERENCES users(id),\n    followeeId INT NOT NULL,\n    FOREIGN KEY(followeeId) REFERENCES users(id),\n    PRIMARY KEY(followerId, followeeId),\n    createdAt TIMESTAMP DEFAULT NOW()\n);\n```\n\n### 6. Tags Table\nThis table stores unique tags that can be associated with photos.\n\n- **id**: The primary key for the tag.\n- **tagName**: A unique name for each tag.\n- **createdAt**: The timestamp when the tag was created.\n\n```sql\nCREATE TABLE tags (\n    id INT AUTO_INCREMENT PRIMARY KEY,\n    tagName VARCHAR(20) UNIQUE,\n    createdAt TIMESTAMP DEFAULT NOW()\n);\n```\n\n### 7. Photo_Tags Table\nThis table stores the many-to-many relationship between photos and tags.\n\n- **photoId**: A foreign key that references the `photos` table.\n- **tagId**: A foreign key that references the `tags` table.\n- The combination of `photoId` and `tagId` forms the primary key.\n\n```sql\nCREATE TABLE photo_tags (\n    photoId INT NOT NULL,\n    FOREIGN KEY(photoId) REFERENCES photos(id),\n    tagId INT NOT NULL,\n    FOREIGN KEY(tagId) REFERENCES tags(id),\n    PRIMARY KEY(photoId, tagId)\n);\n```\n\n## Database Normalization\n\nNormalization is a database design technique that organizes tables to minimize redundancy and dependency. In this schema:\n\n- The `tags` table has been normalized to eliminate redundancy. Each tag is stored only once, and the relationship between tags and photos is managed by the `photo_tags` table.\n- The `photo_tags` table handles the many-to-many relationship between photos and tags.\n\n### Example of Normalization\n\nConsider a photo that has multiple tags:\n\n- Without normalization, we might have a table(tags) where the same photo is stored multiple times, each with a different tagName. This would lead to redundancy.\n- With normalization, we separate the tags into a different table and use a junction table (`photo_tags`) to link photos with their tags. This eliminates redundancy and ensures data consistency.\n\n## Some Logical Queries\nI spent most of my time learning sql queries during the time of my Database Management System Exam. There were so many options on how to write queries, I've sorted few them from basic to advanced :\n### Basic Queries\n1. **Query 1:** What day of the week do most users register on?\n   ```sql\n   select\n    count(*) as total,\n    dayname(createdAt) as day\n    from\n        users\n    group by\n        day\n    order by\n        total desc\n    limit\n        1;\n    ```\n2. **Query 2:** Select the top 5 users that have been loyal for more than eight years and maybe reward them.\n   ```sql\n   select\n    *\n    from\n        users\n    where\n        datediff(now(), createdAt) / 356 \u003e 8\n    order by\n        createdAt desc\n    limit\n        5;\n    ```\n### Intermediate Queries\n3. **Query 3:** Find the users who have never posted a photo and send them email to post some photos.\n   ```sql\n    select\n        username\n    from\n        users\n        left join photos on users.id = photos.userId\n    where\n        photos.id is null;\n    ```\n4. **Query 4:** Pick a most liked photo and send the user who posted that photo a reward.\n   ```sql\n    select\n        username,\n        photos.id,\n        photos.imageUrl,\n        count(*) as total\n    from\n        photos\n        inner join likes on likes.photoId = photos.id\n        inner join users on photos.userId = users.id\n    group by\n        photos.id\n    order by\n        total desc\n    limit\n        1;\n    ```\n5. **Query 5:** How many times does an average user posts?\n     ```sql\n    select\n        ceil(\n            (\n                select\n                    count(*)\n                from\n                    photos\n            ) / (\n                select\n                    count(*)\n                from\n                    users\n            )\n        ) as average;\n    ```\n6. **Query 6:** What are the top 5 most commonly userd hashtags?\n   ```sql\n   select\n        count(*) as total,\n        tags.tagName\n    from\n        photo_tags\n        join tags on photo_tags.tagId = tags.id\n    group by\n        tags.id\n    order by\n        total desc\n    limit\n        5;\n    ```\n7. **Query 7:** Find users who have likes every single photo on the app (*detect bots*)\n   ```sql\n    select\n        username,\n        likes.userId,\n        count(*) as totalLikes\n    from\n        users\n        inner join likes on users.id = likes.userId\n    group by\n        likes.userId\n    having\n        totalLikes = (\n            select\n                count(*)\n            from\n                photos\n        );\n    ```\n8. **Query 8:** Retrieve all followers of a specific user\n   ```sql\n    SELECT\n        u.username\n    FROM\n        follows f\n        JOIN users u ON f.followerId = u.id\n    WHERE\n        f.followeeId = 1;\n    ```\n9.  **Query 9:** Show all the users that have liked a specific photo with id = 1\n    ```sql\n    SELECT\n        users.username\n    FROM\n        likes\n        JOIN users ON likes.userId = users.id\n    WHERE\n        likes.photoId = 1;\n    ```\n10. **Query 10:** Find users who have never uploaded a photo.\n    ```sql\n    SELECT\n        username\n    FROM\n        users\n    WHERE\n        id NOT IN (\n            SELECT\n                userId\n            FROM\n                photos\n        );\n    ```\n11. **Query 11:** Get a list of users who follow a specific user and have commented on one of their photos.\n    ```sql\n    SELECT\n    DISTINCT u.username\n    FROM\n        follows f\n        JOIN users u ON f.followerId = u.id\n        JOIN comments c ON c.userId = u.id\n    WHERE\n        f.followeeId = 1\n        AND c.photoId IN (\n            SELECT\n                id\n            FROM\n                photos\n            WHERE\n                userId = 1\n        );\n    ```\n\n### Advanced Queries\n12. **Query 12:** Find the average number of comments per photo.\n    ```sql\n    SELECT\n        AVG(comment_count)\n    FROM\n        (\n            SELECT\n                photoId,\n                COUNT(*) AS comment_count\n            FROM\n                comments\n            GROUP BY\n                photoId\n        ) AS avg_comments;\n\n    ```\n\n13. **Query 13:** Find top 3 most liked photos with the total number of comments on each photo.\n    ```sql\n    SELECT\n        p.id,\n        COUNT(l.userId) AS like_count,\n        COUNT(c.id) AS comment_count\n    FROM\n        photos p\n        LEFT JOIN likes l ON p.id = l.photoId\n        LEFT JOIN comments c ON p.id = c.photoId\n    GROUP BY\n        p.id\n    ORDER BY\n        like_count DESC\n    LIMIT\n        3;\n    ```\n\n14. **Query 14:** Find the number of mutual followers between two users (userA and userB).\n    ```sql\n    SELECT\n        COUNT(*)\n    FROM\n        follows f1\n        JOIN follows f2 ON f1.followerId = f2.followerId\n    WHERE\n        f1.followeeId = userA\n        AND f2.followeeId = userB;\n    ```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpawandai%2Finstagram-db-clone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpawandai%2Finstagram-db-clone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpawandai%2Finstagram-db-clone/lists"}