Hello! Welcome to the final lesson of our module, "Databases and SQL Fundamentals."
Over the past few lessons, you've gained hands-on experience with two major database paradigms. You've worked with relational databases using SQL, SQLAlchemy, and Alembic, learning how to manage structured data with precision and integrity. Most recently, you explored the world of NoSQL by building a small application with MongoDB, seeing firsthand the flexibility of its document-based model.
Today, we bring it all together. The goal of this lesson is to answer a crucial question that every developer faces: When should I use a SQL database, and when should I use a NoSQL database?
By the end of this lesson, you will be able to analyze the requirements of an application—especially an AI-powered one—and make an informed decision about the right database technology for the job. This skill is a cornerstone of system design and will be invaluable as you start building full stack applications.
1. The Core Differences: A Quick Review
Let's start by solidifying the fundamental differences between the two database types. As we've seen, SQL databases store data in structured tables, while NoSQL databases use a variety of other models.

Beyond the data model, there are three other critical distinctions to understand: scalability, schema flexibility, and the consistency model.
To get a clear overview of these distinctions, let's watch a short video from IBM Technology that outlines the top five differences.
SQL vs. NoSQL: What's the difference?
This video, "SQL vs. NoSQL: What's the difference?", provides a concise and clear breakdown of the key differentiating factors between these two database paradigms.
Watch from 00:00:34 to 00:04:15. As you watch, pay close attention to the five points the presenter makes: Data Structure: Relational vs. Non-relational. Scalability: Vertical vs. Horizontal. Storage Model: Table-based vs. Document/Key-Value based. Querying: SQL vs. dynamic/programmatic queries. Use Case: Multi-row transactions vs. unstructured data.
Now, let's dive a bit deeper into these concepts by reading a complementary article from IBM. This will help formalize your understanding of terms like ACID, CAP, and the different scaling strategies.
SQL vs. NoSQL Databases: What's the Difference? - IBM
The article "SQL vs. NoSQL Databases: What's the Difference?" will reinforce the concepts from the video and add important details about the underlying principles governing each database type.
Please read the sections titled "How SQL works" and "How NoSQL works". Focus on understanding these key points: SQL (Relational): Scalability: Primarily vertical (increasing the power of a single server). Structure: Predefined schema (you must define tables and columns upfront). Properties: Follows ACID (Atomicity, Consistency, Isolation, Durability) for high reliability and data integrity. NoSQL (Non-Relational): Scalability: Primarily horizontal (adding more servers to distribute the load). Structure: Dynamic schema (documents in a collection can have different structures). Properties: Often follows the CAP theorem (Consistency, Availability, Partition tolerance), trading some consistency for higher availability and scale.
Summary of Differences
To consolidate everything, here is a comprehensive comparison table that you can use as a reference.

2. Making the Choice: Application Use Cases
Now for the practical part: how do you apply this knowledge? The choice rarely comes down to which technology is "better" in general, but which is "fitter" for a specific purpose.
When to Use a SQL Database
You should lean towards a SQL database when your application requirements include:
- Structured Data with Clear Relationships: Think of an e-commerce platform where you have
users,products, andorders. An order belongs to a user and contains multiple products. These relationships are naturally represented in a relational model. - Data Integrity is Critical: For financial transactions, academic records, or any system where data accuracy and consistency are non-negotiable, the ACID properties of SQL databases are essential.
- Need for Complex Queries: If you foresee needing to run queries that join data from multiple tables to generate complex reports, SQL is unparalleled in its power and optimization for such tasks.
- A Stable Schema: If the structure of your data is well-understood and not expected to change frequently, the upfront effort of designing a schema pays off in long-term data quality and predictability.
When to Use a NoSQL Database
A NoSQL database becomes the front-runner when your application involves:
- Unstructured or Semi-Structured Data: This is a major factor in AI. If you're dealing with raw text from documents, user-generated content, JSON from APIs, or logs, a document database like MongoDB allows you to store this data in its native form without forcing it into a rigid schema.
- High Scalability and Write Velocity: For applications that need to handle massive volumes of data or a very high rate of writes—like IoT sensor data, real-time analytics, or a viral social media app—the horizontal scalability of NoSQL databases is a key advantage.
- Rapid Development and a Flexible Schema: In early-stage projects or agile environments where requirements are evolving, a dynamic schema allows you to iterate quickly without performing complex database migrations every time your data model changes.
3. SQL vs. NoSQL in Machine Learning
As an aspiring AI developer, it's particularly important to understand how these choices relate to machine learning workflows. Data for AI and ML can be incredibly diverse, and different types of databases serve different stages of the ML pipeline.
Let's read an article that focuses specifically on this topic.
Is SQL or NoSQL Better For Machine Learning? - Yugabyte
The article "Is SQL or NoSQL Better For Machine Learning?" from Yugabyte provides excellent, targeted advice on database selection for AI/ML projects.
Read the sections "When To Use SQL Vs NoSQL In ML System Design" and "Is NoSQL Better For AI Workloads?". As you read, focus on how the characteristics of each database type map to specific ML tasks.
Here's a summary of the key points from the article in an ML context:
-
Use SQL for:
- Curated Feature Stores: Storing clean, structured, and versioned features ready for model training.
- Transactional Data: Managing user data, business transactions, or anything requiring strong consistency that might be used to generate features.
- Audit Trails & Governance: Logging model predictions and outcomes in a structured, reliable way for compliance and analysis.
-
Use NoSQL for:
- Raw Data Ingestion: Landing large volumes of unstructured data (text, images, audio) or semi-structured data (JSON logs, clickstreams) before it's processed.
- Flexible Feature Experimentation: Storing experimental features with evolving structures during the research and development phase.
- Serving Real-time Predictions: Caching model outputs in a fast key-value store (like Redis) for low-latency retrieval.
- Vector Databases: A specialized type of NoSQL database designed to store and query vector embeddings, which are fundamental to modern search, recommendation, and RAG systems in Generative AI.
4. The Best of Both Worlds: The Hybrid Approach
One of the most important takeaways is that you don't have to choose just one. Many modern, large-scale applications use a polyglot persistence architecture—meaning they use multiple database technologies, picking the best tool for each specific job.
The e-commerce scenario from the IBM video you watched earlier is a classic example:
- SQL Database (e.g., PostgreSQL): Manages core business data like customer accounts, product catalogs, and financial transactions (orders, payments).
- NoSQL Document Database (e.g., MongoDB): Stores user-generated product reviews, or user profiles with a wide variety of optional fields.
- NoSQL Key-Value Store (e.g., Redis): Caches user session data, shopping carts, and results of expensive queries to speed up the site.
This approach combines the reliability of SQL for transactional data with the flexibility and scalability of NoSQL for other types of data.
Conclusion
Congratulations on completing the "Databases and SQL Fundamentals" module! You've come a long way, from writing your first SELECT statement to designing data models and now making high-level architectural decisions.
Key Takeaways from this lesson:
- The choice between SQL and NoSQL is a trade-off. There is no single "best" database.
- SQL excels in scenarios requiring data integrity, structured data, and complex queries. It is governed by ACID principles and scales vertically.
- NoSQL is built for flexibility, massive scale, and unstructured data. It often prioritizes availability and scales horizontally.
- For AI/ML applications, SQL is ideal for storing structured features and transactional data, while NoSQL is perfect for ingesting raw data and handling flexible data structures like those found in NLP and computer vision.
- Real-world systems often use a hybrid approach, leveraging the strengths of both paradigms for different parts of the application.
As an aspiring AI developer, having a solid understanding of both SQL and NoSQL is not just beneficial—it's essential. As the Yugabyte article mentioned, SQL is a foundational skill, but familiarity with NoSQL is increasingly valuable for handling the diverse data challenges in AI.
This knowledge forms the bedrock for our next module, Web Development with Python. In the upcoming lessons, we will start building web applications with Flask. You'll create routes, handle user input, and eventually connect these applications to a database—a choice you are now well-equipped to make.