Lesson 3

SQL vs NoSQL: Key Differences, When to Use Each

Understand the difference between SQL and NoSQL databases so you can explain when each one makes sense.

Your ecommerce store tracks orders, customers, products, and payments. All of that data has a clear structure and clear relationships between tables. That is exactly what SQL databases are built for. Knowing why helps you understand when a different tool would be the wrong choice.

How SQL Databases Store Data

A SQL database organizes data into tables. Each table has a fixed set of columns. Every row follows that same structure.

In your ecommerce database, every row in orders has the same columns: order_id, customer_id, status, total_amount, and so on. You can always count on those columns being there.

SELECT order_id, customer_id, status, total_amount
FROM orders
WHERE status = 'pending';

That consistency is what makes SQL databases reliable for transactional data. You know the shape of your data before you query it.

How NoSQL Databases Store Data

NoSQL databases do not use tables. The most common type, a document database like MongoDB, stores each record as a JSON-like document. One document might have ten fields. The next might have three completely different ones.

There is no SQL equivalent to show here because NoSQL databases use their own query syntax. The point is that your customers table in SQL has the same columns for every row. A NoSQL customers collection might have some records with a loyalty_tier field and others without it.

That flexibility is useful when data changes shape often. It becomes a problem when you need consistent structure to do reliable analysis.

-- In SQL, every customer row has the same columns. This always works.
SELECT customer_id, first_name, loyalty_tier
FROM customers
WHERE loyalty_tier = 'Gold';
Key idea: SQL enforces structure. Every row in a table has the same columns. That makes querying predictable and joins possible. NoSQL trades that consistency for flexibility.

Where Relationships Live

SQL databases are called relational because tables relate to each other through keys. The orders table has a customer_id column that points to a row in the customers table. You can join them to get the full picture.

SELECT c.first_name, c.last_name, o.order_id, o.total_amount
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
WHERE o.total_amount > 500;

NoSQL databases handle relationships differently. Document databases often embed related data inside a single document rather than joining across collections. That works at scale but makes ad-hoc analysis harder.

For reporting and analytics, SQL joins give you more control with less effort.

When Each One Makes Sense

SQL is the right choice when your data has a consistent structure, you need to join multiple tables, and data integrity matters. Order records, payment records, and customer records fit this perfectly.

NoSQL makes sense when your data structure varies a lot per record, you are storing content like blog posts or user activity logs, or you need to scale writes across many servers without coordination overhead.

Most companies use both. A product catalog in MongoDB for flexible attributes, and a SQL database for orders and payments where every field matters.

-- You can still explore structure-related questions in your SQL database.
-- How many distinct loyalty tiers exist across all customers?
SELECT DISTINCT loyalty_tier
FROM customers
ORDER BY loyalty_tier;

Common Mistakes

-- wrong
SELECT *
FROM orders
JOIN customers ON order_id = customer_id;

-- correct
SELECT *
FROM orders o
JOIN customers c ON o.customer_id = c.customer_id;

Joining on the wrong columns returns nonsense or an empty result. Always match the foreign key in one table to the primary key in the other.

-- wrong
SELECT customer_id, loyalty_tier
FROM customers
WHERE loyalty_tier = Gold;

-- correct
SELECT customer_id, loyalty_tier
FROM customers
WHERE loyalty_tier = 'Gold';

Text values in WHERE need single quotes. Without them the database reads Gold as a column name and throws an error.

-- wrong
SELECT order_id, total_amount
FROM orders
WHERE total_amount = '500';

-- correct
SELECT order_id, total_amount
FROM orders
WHERE total_amount = 500;

Numeric columns should be compared to numbers, not strings. Wrapping a number in quotes can cause type mismatches and unexpected results in some databases.

Quick Recap

  • SQL databases store data in structured tables with fixed columns. NoSQL databases use flexible formats like documents or key-value pairs.
  • SQL enforces consistent structure, which makes joins and analysis reliable and predictable.
  • Use SQL when your data has clear relationships and consistent shape. NoSQL fits unstructured or highly variable data better.
  • Most real-world systems use both, each for what it does best.
Exercise 1 Easy
Write a query that returns the customer_id, first_name, last_name, and loyalty_tier from the customers table. Filter to only customers where country is 'US'.
Hint: Use SELECT with four columns, FROM customers, and a WHERE clause. Text values need single quotes.
Solution
SELECT customer_id, first_name, last_name, loyalty_tier
FROM customers
WHERE country = 'US';
Exercise 2 Medium
Return the order_id, status, and total_amount from the orders table for all orders where status is 'delivered' and total_amount is greater than 100. Sort the results by total_amount in descending order.
Hint: Use WHERE with two conditions joined by AND. Add ORDER BY total_amount DESC at the end.
Solution
SELECT order_id, status, total_amount
FROM orders
WHERE status = 'delivered'
  AND total_amount > 100
ORDER BY total_amount DESC;
Exercise 3 Hard
Write a query that returns each customer's first_name, last_name, and the total number of orders they have placed, labelled as order_count. Join the customers table with the orders table on customer_id. Only include customers who have placed at least 3 orders. Sort by order_count descending.
Hint: Use COUNT(o.order_id) and GROUP BY the customer columns. Filter grouped results with HAVING COUNT(o.order_id) >= 3. Add ORDER BY order_count DESC.
Solution
SELECT c.first_name, c.last_name, COUNT(o.order_id) AS order_count
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id, c.first_name, c.last_name
HAVING COUNT(o.order_id) >= 3
ORDER BY order_count DESC;
Frequently Asked Questions

SQL databases store data in structured tables with fixed columns. NoSQL databases store data in flexible formats like documents, key-value pairs, or graphs.

Neither is universally better. SQL works well for structured, relational data. NoSQL works better for unstructured or rapidly changing data at massive scale.

Use NoSQL when your data structure changes frequently, when you need to store documents or nested data, or when you are scaling to billions of rows across many servers.

Most NoSQL databases do not use SQL. They have their own query languages or APIs, though some like Couchbase have added SQL-like query support.