SQL or NoSQL - How to choose the suitable one for you
Tags: webdev, sql, database, productivity
Published: 2022-09-26 09:59:17

## Introduction

I'm going to start by giving examples of SQL and NoSQL databases.

SQL is short for Structured Query Language. They are used by relational databases.
Examples of relational databases are IBM, Oracle, MySQL, Microsoft SQLServer and PostgreSQL. They are the same as RDBMS (Relational Database Management System).

NoSQL is called non-relational, "no SQL", or "not only SQL".
Examples include MongoDB, CouchDB, CouchBase, Cassandra, HBase, Redis, Riak, Neo4J, DynamoDb, e.t.c


## Getting into it 🚀

### Features

Often, when you search for the differences between these two, you don't understand it 😥 because of the grammar used to explain it (i mean, we're not experts yet 🤷‍♀️). So let's simplify those grammar and choose the right database for you 😁.

**SQL FEATURES**
1. SQL uses the SQL language syntax which includes SELECT, UPDATE, INSERT, and DELETE statements.

2. These relational database examples have the same shared syntax, some of them might add additional features on top of it. Generally, they all have very common functionalities

3. The data are laid out in tables with rows/records and columns/fields.

4. SQL databases are relational for the most part. For instance, tables in a database can be related to each other through primary keys.

**NoSQL FEATURES**
1. They don't follow the standard SQL language syntax.

2. They don't worry about relational data as much. They are can be used to store unstructured data.

3. The data are stored in documents as opposed to tables.
Note: Collections in NoSQL are equivalent to a table in SQL
      Documents are equivalent to records in SQL.

4. There are no Ids or foreign keys linking tables together.

5. As mentioned earlier, they can be easily used to store unstructured data with the drawback of not being able to perform SQL operations like JOIN.

Easy so far, right?💪💪💪

{% embed https://tenor.com/bybY0.gif %}

## Okay. So, how do we choose now? 🤷‍♀️

- SQL databases can be used when you have structured data and you want to keep the data structured while NoSQL can be used to store unstructured data.

- SQL databases are great for making quicker complex queries on strictly monitored data. They can make complex queries or calls to the database faster, while NoSQL can write quicker to the database.

- Some NoSQL database queries from the database are slower. 
    NoSQL databases are geared towards writing to the database faster. This is because the database does not check the structure of the data strictly when writing to it compared to SQL databases.

- SQL rules with rows, columns and indexes make it much easier to know that the data in a database follows a specific format. It has a pre-defined/rigid schema while NoSQL is schemaless or they have a flexible schema, this means that your data do not have to follow a specific format indicating that different documents could look different depending on where you're pulling from.

> Let me give you an example of the flexible Schema concept. So you don't get confused. 
>     In a SQL database, let's say you create a database with a _lastname_ field that's not required. A new user without a _lastname_ in the database will still have the _lastname_ column in the database, but with a value of null. 
>     Whereas, NoSQL will discard the _lastname_ field from the database if you don't give it a value. As I said, It can be used to store unstructured/semi-structured data.

Phew!!!!😅 So, let's get back.

- If you've searched for this topic before, you have probably encountered this "NoSQL databases scale horizontally while SQL scales vertically".
Well, that's true.
    NoSQL scales very well when they have a lot of people connecting to them at the same time compared to SQL. SQL is not very good at handling many connections as it has to perform strict checks on data before writing them. 
Note: NoSQL performs better here when it comes to reading and writing.

##My Final thoughts and words 😇

NoSQL scales a bit better, but when you are scaling an app with strict requirements that you'll be reading from a lot, then SQL databases might be better. Even though it doesn't quite scale as nicely as NoSQL, it can still scale whilst still having the benefit of reading from it quicker and faster.

NoSQL is the option when you need to store unstructured data. For example, Settings for a user where some things are off, some things are toggled on or off. Instead of having a large number of columns, to keep track of some settings that we shouldn't, you can have a NoSQL database that handles the settings.

With all said, 😁 nothing stops you from using SQL and NoSQL for applications as long as you know what you're doing. As of the writing of this post, Netflix uses a SQL and NoSQL database. So it all depends on your needs.

Thanks for reading ❤️✌️