What is the difference between SQL and NoSQL databases and when to use each with Node.js?
Why Interviewers Ask This
Candidates at the intermediate level are expected to not only know this concept but explain the trade-offs involved. Interviewers use this question to see if you can reason about design decisions, not just recall facts.
Answer
SQL databases (PostgreSQL, MySQL, SQLite) use structured tables with a fixed schema, enforce ACID transactions, and use SQL for querying. They excel at complex queries with joins, strict data integrity, and well-defined relational data. Use SQL for: financial data, e-commerce (orders, inventory), applications needing complex reporting, or when data relationships are complex and well-understood. Node.js drivers: pg (PostgreSQL), mysql2, with ORMs like Prisma, Sequelize, TypeORM. NoSQL databases (MongoDB, Redis, Cassandra, DynamoDB) offer flexible schemas, horizontal scaling, and specific optimizations for particular data patterns. Use NoSQL for: document stores (MongoDB for content, user profiles, catalogs), key-value cache (Redis), real-time analytics (Cassandra for time-series), or when schema evolves rapidly. Node.js drivers: mongoose (MongoDB), ioredis (Redis). In practice, many applications use both — PostgreSQL for transactional data and Redis for caching and sessions.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Node.js codebase.