> For the complete documentation index, see [llms.txt](https://divyanshu-vibhu.gitbook.io/system-design/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://divyanshu-vibhu.gitbook.io/system-design/storage/sql-vs-nosql.md).

# SQL vs NoSQL

So after reading about SQL and NoSQL, the common questions that comes in mind are&#x20;

> What are the differences between the two ?
>
> When do we choose SQL over NoSQL or vice-versa ?
>
> What are the properties needed to decide the type of datastore to use in your application ?

## Differences

* SQL databases are not suitable for hierarchical data storage while NoSQL databases are.
* SQL supports ACID, while NoSQL doesn't.
* SQL databases support Joins, while NoSQL joins can only be achieved at application layer.
* SQL Databases are strongly consistent, while NoSQL databases are highly available.
* SQL databases are preferred when the requirement is of correct and consistent data, while NoSQL databases rely on fast data and can compromise on accuracy
* SQL databases are vertically scalable while NoSQL are horizontally scalable.

| **Criteria**                                   | **SQL/NoSQL** |
| ---------------------------------------------- | :-----------: |
| High number of reads/writes (High QPS)         |     NoSQL     |
| High Concurrency                               |     NoSQL     |
| Low Reads/Writes (Few thousands per second)    |      SQL      |
| Joins Needed                                   |      SQL      |
| Strong Consistency                             |      SQL      |
| High Availability                              |     NoSQL     |
| Transaction Properties                         |      SQL      |
| High Commodity Hardware (Vertically Scalable)  |      SQL      |
| Low Commodity Machines (Horizontally Scalable) |     NoSQL     |
| Sharding                                       |     NoSQL     |
| Partitioning                                   |      SQL      |

{% hint style="info" %}
**Partitioning:** Partitioning is dividing large data into multiple tables in a single database. Vertical partitioning is dividing data by columns into multiple tables (normalisation), while horizontal partitioning is dividing data by rows into multiple tables.

**Sharding:** Sharding is dividing large data into smaller chunks across multiple machines. There are two types of sharding: Vertical sharding where the data is divided by columns across machines where horizontal sharding is dividing data by rows across machines.
{% endhint %}

## For More Reading

* [SQL vs NoSQL](https://www.xplenty.com/blog/the-sql-vs-nosql-difference/#:~:text=SQL%20databases%20are%20relational%2C%20NoSQL%20are%20non%2Drelational.\&text=NoSQL%20databases%20have%20dynamic%20schemas,graph%20or%20wide%2Dcolumn%20stores.)
