NoSQL – a Dev Practical Guide with Code Examples

Here's a practical, publish-ready article you can post on your blog, tech platform, or developer community. It's written in a clean, structured format with code, explanation, and examples — perfect for beginners and intermediate devs.


🆚 SQL vs NoSQL – Practical Guide with Code Examples

As developers, choosing between SQL and NoSQL depends on how your data behaves and how you want to store, retrieve, and scale it. In this guide, we'll explore the real-world differences, how activity is handled, and include practical code examples in MySQL, PostgreSQL, and MongoDB.


🗃️ SQL (Relational Databases)

✅ Structure

  • Data is stored in tables (rows and columns).

  • Enforces a strict schema.

  • Relationships between tables use foreign keys.

✅ Examples

  • MySQL

  • PostgreSQL

  • Microsoft SQL Server

  • Oracle

✅ Best Use Cases

  • Banking and finance

  • Inventory systems

  • CRM software

  • Systems needing ACID compliance (Atomicity, Consistency, Isolation, Durability)

✅ Key Features

  • Strong consistency

  • Powerful queries using SQL

  • Mature tools and standards

⚠️ Limitations

  • Less flexible with changing data structures

  • Harder to scale horizontally (across multiple servers)


📦 NoSQL (Non-Relational Databases)

✅ Structure

  • Stores data in flexible formats: documents, key‑value pairs, graphs, or wide columns.

  • Schema-less: documents can have different fields.

✅ Examples

  • MongoDB (Document)

  • Redis (Key-Value)

  • Cassandra (Wide-Column)

  • Neo4j (Graph)

✅ Best Use Cases

  • Real-time analytics

  • Social media and IoT applications

  • Recommendation engines

  • Systems requiring horizontal scaling

✅ Key Features

  • Highly scalable and fast for massive data

  • Flexible data model

  • Schema can evolve on the fly

⚠️ Limitations

  • Often eventual consistency

  • Weaker support for complex joins and transactions


🔍 Quick Decision Guide

QuestionChoose SQLChoose NoSQL
Need complex relationships & joins?
Need flexible or rapidly changing schema?
Working with structured, relational data?
Need to scale horizontally across many nodes?

💻 PRACTICE SECTION: SQL vs NoSQL CRUD

1️⃣ Microsoft SQL Server Example (TOP)

-- Get top 5 users (SQL Server)
SELECT TOP 5 * FROM Users ORDER BY Age DESC;

2️⃣ MySQL Equivalent (LIMIT)

-- Get top 5 users (MySQL)
SELECT * FROM Users ORDER BY Age DESC LIMIT 5;

3️⃣ PostgreSQL: Creating User and Database

# Open psql terminal
psql -U postgres
-- Create user and database in PostgreSQL
CREATE USER app_user WITH PASSWORD 'strongpass123';
CREATE DATABASE app_db OWNER app_user;
ALTER USER app_user CREATEDB;

📦 MongoDB PRACTICE: CRUD Operations

🔧 Setup

First, open the shell:

mongosh

✅ Create a Database and Collection

use mydb

// Automatically creates the 'users' collection
db.users.insertOne({ name: "Alice", age: 25 });

✅ INSERT (Create)

db.users.insertMany([
  { name: "Bob", age: 30 },
  { name: "Charlie", age: 22 }
]);

✅ READ

db.users.find(); // All users

db.users.find({ age: { $gte: 25 } }); // Users age 25 and above

✅ UPDATE

db.users.updateOne(
  { name: "Alice" },
  { $set: { age: 26 } }
);

✅ DELETE

db.users.deleteOne({ name: "Bob" });

✅ TOP 5 (like SELECT TOP 5 or LIMIT 5)

db.users.find().sort({ age: -1 }).limit(5);

📊 Real-Time Activity in NoSQL (MongoDB Example)

MongoDB supports real-time changes using Change Streams.

const changeStream = db.collection('users').watch();

changeStream.on('change', (next) => {
  console.log('Change detected:', next);
});

You can use this to build:

  • Activity feeds (e.g., “User X liked your post”)

  • Notifications

  • Live dashboards


🔥 Final Thoughts

Both SQL and NoSQL are powerful—your choice should be based on your app's needs:

SQL StrengthsNoSQL Strengths
Structured, relational dataFlexible, unstructured data
Transactions and joinsSpeed and scalability
Strict schemaEvolving schema and rapid dev

📢 Author:

Ekemini “Kaemzy” Thompson
Building data-powered and scalable full-stack apps.


0
Subscribe to my newsletter

Read articles from Ekemini Thompson directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ekemini Thompson
Ekemini Thompson

Ekemini Thompson is a Machine Learning Engineer and Data Scientist, specializing in AI solutions, predictive analytics, and healthcare innovations, with a passion for leveraging technology to solve real-world problems.