Showing posts with label SQL vs MongoDB. Show all posts
Showing posts with label SQL vs MongoDB. Show all posts

Practice Assignment: SQL JOINs vs MongoDB Referencing

๐Ÿงช Practice Assignment: SQL JOINs vs MongoDB Referencing


Here's a clear and practical assignment to help readers compare SQL JOINs with MongoDB referencing. This exercise is designed to reinforce how both systems manage relationships through hands-on tasks.


๐ŸŽฏ Objective:

Understand how to build and query related data using:

  • SQL with JOINs

  • MongoDB with manual referencing


๐Ÿ“š Scenario:

You’re building a simple Library System with:

  • A list of Authors

  • A list of Books written by those authors

Each book belongs to one author, and each author can write multiple books (One-to-Many relationship).


๐Ÿ”น Part A: SQL – Using Foreign Keys & JOINs


Step 1: Create Tables

CREATE TABLE Authors (
  AuthorID INT PRIMARY KEY,
  Name VARCHAR(100)
);

CREATE TABLE Books (
  BookID INT PRIMARY KEY,
  Title VARCHAR(150),
  AuthorID INT,
  FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)
);

Step 2: Insert Data

INSERT INTO Authors (AuthorID, Name) VALUES
(1, 'George Orwell'),
(2, 'J.K. Rowling');

INSERT INTO Books (BookID, Title, AuthorID) VALUES
(101, '1984', 1),
(102, 'Animal Farm', 1),
(103, 'Harry Potter', 2);

Step 3: Write a JOIN Query

Write a SQL query to display all book titles along with their author names.

Example:

SELECT Books.Title, Authors.Name
FROM Books
JOIN Authors ON Books.AuthorID = Authors.AuthorID;

๐Ÿ”น Part B: MongoDB – Manual Referencing


Step 1: Insert Documents

Authors Collection:

db.authors.insertMany([
  { _id: 1, name: "George Orwell" },
  { _id: 2, name: "J.K. Rowling" }
]);

Books Collection:

db.books.insertMany([
  { title: "1984", author_id: 1 },
  { title: "Animal Farm", author_id: 1 },
  { title: "Harry Potter", author_id: 2 }
]);

Step 2: Query to Simulate a JOIN

Write MongoDB queries to show book titles and their author names:

Step 1: Get author document

const author = db.authors.findOne({ name: "George Orwell" });

Step 2: Find all books by that author

db.books.find({ author_id: author._id });

๐Ÿ”ธ Optional Challenge: Try Embedding in MongoDB

Instead of referencing, restructure books with embedded author info:

db.books.insertOne({
  title: "1984",
  author: {
    id: 1,
    name: "George Orwell"
  }
});

Then query:

db.books.find({ "author.name": "George Orwell" });

๐Ÿง  Reflection Questions

  1. Which method (JOIN or referencing) felt more intuitive to you?

  2. How does MongoDB referencing differ from SQL JOINs?

  3. What trade-offs do you notice in terms of query performance or complexity?


  • Click Next to view solution with expected outputs


Feel free to share your thoughts in the comments!


CRUD Operations in SQL vs MongoDB – Practice Task + Assignment for Beginners


๐Ÿงช Practice Task – CRUD Basics in SQL & MongoDB


Here's a Practice Task + Mini Assignment that complements Part 6: CRUD Operations in SQL vs NoSQL. It's designed to reinforce the differences in syntax and logic between SQL and MongoDB, while staying simple and highly practical for beginners.


๐ŸŽฏ Objective:

Perform basic Create, Read, Update, and Delete operations in both SQL and NoSQL (MongoDB).


๐Ÿ”น Scenario: School Students Data

You are managing a student database and need to do the following:

  1. Add a new student

  2. Fetch the student's data

  3. Update the student’s class

  4. Delete the student’s record


๐Ÿ“Œ A. SQL Practice

1. Create

INSERT INTO Students (StudentID, Name, Class)
VALUES (2, 'Ravi Kumar', '10B');

2. Read

SELECT * FROM Students WHERE StudentID = 2;

3. Update

UPDATE Students SET Class = '10C' WHERE StudentID = 2;

4. Delete

DELETE FROM Students WHERE StudentID = 2;


๐Ÿ“Œ B. MongoDB Practice

1. Create

db.students.insertOne({
  student_id: 2,
  name: "Ravi Kumar",
  class: "10B",
  marks: [{ subject: "English", score: 82 }]
});

2. Read

db.students.findOne({ student_id: 2 });

3. Update

db.students.updateOne(
  { student_id: 2 },
  { $set: { class: "10C" } }
);

4. Delete

db.students.deleteOne({ student_id: 2 });

๐Ÿ“ Mini Assignment: Build Your Own CRUD Set


๐Ÿ”น Instructions:

  1. Choose a student name and add them to both SQL and NoSQL.

  2. Add a subject and score for them (embedded in NoSQL).

  3. Read and display their data.

  4. Change their class and update their English score to 90.

  5. Finally, delete the record in both systems.


๐Ÿ’ก Challenge (Optional):

  • In SQL, insert marks into a separate Marks table and join it to fetch the student’s full profile.

  • In MongoDB, use $push to add a new subject to the marks array.


✅ Bonus: MongoDB $push Example

db.students.updateOne(
  { student_id: 2 },
  { $push: { marks: { subject: "Science", score: 88 } } }
);

Do you Know: The $push operator is used to add a new element to an array field in MongoDB.



Tell Us in Comments:

Which syntax do you find easier — SQL or MongoDB? Share your thoughts in the comments!

Also, Try the assignment above and share your solution in the comments.



To know about CRUD opreations read  Part 6: CRUD Operations in SQL vs NoSQL – A Beginner's Guide


✅ What’s Next?

Next: solution for the mini assignment in both SQL and NoSQL



Featured Post

Creating Your First MongoDB Database and Collection (Step-by-Step Tutorial)

Creating Your First MongoDB Database and Collection A Super Fun, Step-by-Step Adventure for Beginner to Expert Level What is MongoDB? ...

Popular Posts