Full Stack Development

SQL vs NoSQL Databases

by Tomas Trescak t.trescak@westernsydney.edu.au

SQL vs NoSQL

/

Misconceptions

  • 🐲 Myth: SQL is old, NoSQL is new
  • 🐲 Myth: SQL is too rigid
  • 🐲 Myth: NoSQL is too uncontrollable (unsafe)

SQL Databases

/

Structured Power

  • Tables and schemas
  • Relationships
  • Blog Example

SQL Databases

/

Structured Power

    import { Pool } from 'pg';

const pool = new Pool({ 
  user: 'blog_user', host: 'localhost', database: 'blog_db', password: 'pass' });

interface Post { id: number; title: string; content: string; date: string }

async function getPostsByTag(tag: string): Promise<Post[]> {
  const result = await pool.query(
    `SELECT p.id, p.title, p.content, p.date 
     FROM posts p 
     JOIN post_tags pt ON p.id = pt.post_id JOIN tags t ON t.id = pt.tag_id 
     WHERE t.name = $1`,
    [tag]
  );
  return result.rows;
}
  
  • Tables and schemas
  • Relationships
  • Example

NoSQL Databases

/

Flexible Freedom

    import { MongoClient } from 'mongodb';

const uri = 'mongodb://localhost:27017';
const client = new MongoClient(uri);

interface Post { 
  _id: string; 
  title: string; content: string; date: string; tags: string[] }

async function getPostsByTag(tag: string): Promise<Post[]> {
  await client.connect();
  const db = client.db('blog_db');
  const posts = await db.collection('posts').find({ tags: tag }).toArray();
  return posts as Post[];
}
  
  • Documents, not tables
  • Scales like a dream
  • Example
    {
    "title": "My Post",
    "content": "Post content",
    "tags": ["react", "sql"]
}
  

SQL vs NoSQL

https://blog.algomaster.io/p/sql-vs-nosql-7-key-differences

SQL vs NoSQL

/

When to Use

  • SQL
    • Structured Data
    • Joins and queries
  • NoSQL: Embedded data
    • Unstructured Data
    • Fast-pacing changes
  • Mix them!

SQL vs NoSQL

/

Case Study

Our blog starts small—10 posts, 5 tags. SQL works great for filtering. Then it explodes—10,000 posts, new features like comments, images, likes, replies ... NoSQL takes over for speed and flexibility.

Decision? Start with SQL, and switch if chaos hits!

    SQL vs NoSQL

    /

    Conclusions

    • Key takeaway : Fit the tool to the job
    • Best practice : Plan ahead
    • Pitfall : Overcomplicating