Basics of Structured Query Language (SQL), SQL vs NoSQL
Basics of Structured Query Language (SQL), SQL vs NoSQL

Basics of Structured Query Language (SQL), SQL vs NoSQL

Date
Feb 1, 2025
Tags
backend
Structured Query Language, the backbone of pretty much every app you use.
SQL is for structured data and strong consistency.
NoSQL is for unstructured data and scalability.

SQL Basics: Decoded

Imagine a massive Excel spreadsheet.
That's kinda like a database table. It has rows (records) and columns (fields). SQL lets you interact with this spreadsheet on steroids.
1. SELECT: This is how you grab data.
SELECT username, followers FROM instagram_users WHERE verified = TRUE;
Translation: "Gimme the usernames and follower counts from the instagram_users table, but ONLY for verified accounts."
 
2. FROM: Tells SQL where to get the data. Like, "Hey, look in this specific spreadsheet."
 
3. WHERE: Filters your data. It's like saying, "Only give me the good stuff." You can use all sorts of conditions here:
WHERE age > 18 AND location = 'Los Angeles';
WHERE product_price < 100 OR discount_code = 'SUMMER20';
 
4. INSERT: Adds new data. Think of it as creating a new row in your spreadsheet.
INSERT INTO products (product_name, product_price) VALUES ('Cool Socks', 15);
 
5. UPDATE: Changes existing data. Like, "Oops, the price changed!"
UPDATE products SET product_price = 20 WHERE product_name = 'Cool Socks';
 
6. DELETE: Removes data. Use with caution! This is like deleting a row, gone forever once deleted
DELETE FROM products WHERE product_name = 'Lame Socks';
 
7. JOIN: This is where things get interesting. It lets you combine data from multiple tables. Imagine you have one table for users and another for their orders. JOIN lets you see which user made which order.
SELECT users.username, orders.order_date
FROM users
JOIN orders ON users.user_id = orders.user_id;
This joins the users and orders tables based on the matching user_id.
 
8. GROUP BY: Groups data based on a specific column. Super useful for aggregations.
SELECT country, COUNT(*) AS total_users FROM users GROUP BY country;
This gives you the number of users from each country.
 
9. ORDER BY: Sorts your data. Like, "Show me the users with the most followers."
SELECT username, followers FROM instagram_users ORDER BY followers DESC; -- DESC for descending order
 
10. LIMIT: Limits the number of results. Useful for pagination or just getting a quick sample.
SELECT username, followers FROM instagram_users LIMIT 10; -- Show only the top 10

from noob to (kinda) pro

To go from mid-level to (almost) senior-level, you need to go deeper:
  • Indexes: Learn how to speed up your queries. Think of it as creating an index in a book.
  • Transactions: Understand how to ensure data consistency, especially when multiple users are making changes.
  • Stored Procedures: Write reusable chunks of SQL code. This is like creating your own custom SQL functions.
  • Database Design: Learn how to design efficient and scalable databases. This is HUGE.
  • Performance Tuning: Optimize your queries to run faster. Because nobody likes slow loading times.
  • NoSQL: While we're focusing on SQL, understanding NoSQL databases (like MongoDB or Firestore) is also valuable. They're a different beast, but often used alongside SQL databases.
 
Example time:
Let's say you're building a database for your sneaker collection. You have tables for sneakers, brands, and colors. You want to find all the red Nike sneakers released after 2020. Here’s how you’d write the query:
SELECT s.sneaker_name FROM sneakers s JOIN brands b ON s.brand_id = b.brand_id JOIN colors c ON s.color_id = c.color_id WHERE b.brand_name = 'Nike' AND c.color_name = 'Red' AND s.release_year > 2020;

SQL vs. NoSQL

SQL (Relational DBs)

Data is stored in tables with rows and columns, and relationships between tables are clearly defined.
Advantages:
  • ACID Properties: Atomicity, Consistency, Isolation, Durability. Basically, this means your data is safe and reliable. Think bank transactions – you need ACID properties.
  • Structured Data: Perfect for data that fits neatly into tables, like customer information, product catalogs, etc.
  • Mature Technology: Lots of resources, tools, and experienced developers.
  • Standardized Language: SQL is a well-established language, making it easier to learn and use across different databases.
Disadvantages:
  • Scalability: Scaling SQL databases can be complex and expensive. Vertical scaling (increasing server resources) has limits.
  • Flexibility: Not ideal for unstructured or rapidly changing data. Schema changes can be a pain.
  • Less Suitable for Huge Data Volumes: While SQL databases can handle large datasets, NoSQL databases often handle massive datasets more efficiently.

NoSQL (Non-relational DBs)

NoSQL databases are non-relational. Examples: document stores (MongoDB), key-value stores (Redis), and graph databases (Neo4j). They're all about flexibility and scalability.
Advantages:
  • Scalability: Horizontal scaling (adding more servers) is easier and cheaper. Perfect for handling massive amounts of data and traffic.
  • Flexibility: Handles unstructured and semi-structured data like JSON or XML. Great for social media feeds, sensor data, etc.
  • Faster Development: More flexible schema means faster development cycles.
  • High Performance for Specific Use Cases: NoSQL databases are often optimized for specific tasks, like caching or real-time data processing.
Disadvantages:
  • Lack of Standardization: No single language like SQL. Each NoSQL database has its own query language.
  • Data Consistency: ACID properties are often relaxed in favor of performance and scalability. This can lead to data inconsistencies if not handled carefully.
  • Less Mature Technology: The NoSQL landscape is still evolving, and finding experienced developers can be challenging.

When to Choose What:

  • SQL: When you need strong data consistency (ACID), structured data, complex queries, and a mature ecosystem. Think financial applications, e-commerce platforms, or any application where data integrity is paramount.
  • NoSQL: When you need to handle massive amounts of data, unstructured or rapidly changing data, high performance for specific use cases, and rapid development. Think social media platforms, real-time analytics, or IoT applications.
Often, the best approach is to use both. Use SQL for your core transactional data and NoSQL for specific use cases like analytics or caching.