Topics SQL Database Fundamentals IQs
Back Sign up to track progress
SQL

Database Fundamentals IQs

Sign up free to track your views & progress

What is a Database Management System (DBMS), and Can You Name Some Examples?

A Database Management System (DBMS) is software used to store, manage, retrieve, and organize data efficiently. It acts as an interface between applications and databases, allowing users to perform operations such as inserting, updating, deleting, and querying data securely and consistently.

A DBMS helps organizations manage large amounts of structured or unstructured data while ensuring:

  • data integrity
  • security
  • concurrency control
  • backup and recovery
  • efficient data access

Without a DBMS, managing data manually would become extremely difficult, especially in enterprise applications handling millions of records.

Core Functions of DBMS

Data Storage

Stores information systematically in tables, collections, or files.

Data Retrieval

Allows users to fetch data using queries.

Data Security

Controls user access and permissions.

Data Integrity

Maintains consistency and accuracy of stored data.

Concurrency Control

Allows multiple users to access data simultaneously without conflicts.

Backup and Recovery

Protects data during failures or disasters.

Types of DBMS

Relational DBMS (RDBMS)

Stores data in tables with rows and columns.

Examples:

  • MySQL
  • PostgreSQL
  • Oracle Database
  • Microsoft SQL Server

NoSQL Databases

Designed for scalability and flexible data models.

Examples:

  • MongoDB
  • Cassandra
  • Redis
  • Couchbase

Distributed Databases

Used in large-scale cloud systems.

Examples:

  • Google Spanner
  • Amazon DynamoDB

Real-World Example

In an e-commerce application:

  • customer details
  • product information
  • orders
  • payments

are stored and managed using a DBMS such as MySQL or PostgreSQL.

Benefits of Using DBMS

  • centralized data management
  • improved security
  • reduced redundancy
  • better scalability
  • efficient querying
  • reliable transactions

DBMS systems are foundational components of modern software applications because nearly every enterprise system depends on efficient and secure data management.


Explain the ACID Properties in the Context of Databases

ACID properties are a set of principles that ensure reliable and consistent database transactions. These properties are especially important in systems handling critical business operations such as banking, e-commerce, and financial applications where data consistency and integrity are essential.

ACID stands for:

  • Atomicity
  • Consistency
  • Isolation
  • Durability

These properties guarantee that database transactions behave correctly even during failures, crashes, or concurrent operations.

Atomicity

Atomicity means a transaction is treated as a single indivisible unit. Either all operations inside the transaction succeed, or none of them are applied.

Example:

  • transferring money between bank accounts

If money is deducted from one account but not credited to another, the entire transaction must rollback.

Consistency

Consistency ensures that a transaction moves the database from one valid state to another valid state while maintaining all integrity rules and constraints.

Example:

  • foreign key constraints
  • unique constraints
  • data validation rules

Invalid data should never corrupt the database state.

Isolation

Isolation ensures that concurrent transactions do not interfere with each other.

Even if multiple users access the database simultaneously, transactions should behave as if they execute independently.

Isolation prevents issues such as:

  • dirty reads
  • non-repeatable reads
  • phantom reads

Durability

Durability guarantees that once a transaction is committed, the data remains permanently stored even if:

  • system crashes
  • power failures occur
  • servers restart

Databases achieve durability using:

  • transaction logs
  • disk persistence
  • replication mechanisms

Real-World Example

In an online payment system:

  1. User places an order
  2. Payment is processed
  3. Inventory updates
  4. Transaction commits successfully

If failure occurs during processing:

  • rollback restores consistent state

Importance of ACID Properties

  • ensures data reliability
  • prevents corruption
  • supports concurrent users
  • guarantees transaction safety
  • improves business trust

Relational databases such as MySQL, PostgreSQL, Oracle, and SQL Server strongly support ACID transactions because enterprise applications require high data consistency and reliability.


What are the Differences Between SQL and NoSQL Databases?

SQL and NoSQL databases are two major categories of database systems used in modern applications. They differ in terms of data structure, scalability, schema design, consistency models, and use cases.

SQL databases are relational databases that store data in structured tables with predefined schemas. NoSQL databases are non-relational databases designed for scalability, flexibility, and handling large volumes of distributed data.

SQL Databases

SQL databases use:

  • tables
  • rows
  • columns
  • relationships

They rely on Structured Query Language (SQL) for querying data.

Examples:

  • MySQL
  • PostgreSQL
  • Oracle
  • SQL Server

Characteristics of SQL Databases

Structured Schema

Tables follow predefined schemas.

ACID Compliance

Strong transactional consistency is supported.

Relational Data Model

Relationships are managed using:

  • primary keys
  • foreign keys
  • joins

Vertical Scaling

Traditionally scale by increasing server capacity.

Complex Queries

Support advanced joins and transactional operations.

NoSQL Databases

NoSQL databases store data in flexible formats such as:

  • documents
  • key-value pairs
  • graphs
  • wide-column structures

Examples:

  • MongoDB
  • Cassandra
  • Redis
  • DynamoDB

Characteristics of NoSQL Databases

Flexible Schema

No fixed table structure is required.

Horizontal Scaling

Designed for distributed cloud environments.

High Availability

Optimized for scalability and performance.

Eventual Consistency

Some NoSQL systems prioritize availability over strict consistency.

Better for Unstructured Data

Suitable for rapidly changing or large-scale datasets.

Key Differences

Data Structure

SQL:

  • relational tables

NoSQL:

  • flexible document or key-value structures

Schema

SQL:

  • fixed schema

NoSQL:

  • dynamic schema

Scalability

SQL:

  • vertical scaling

NoSQL:

  • horizontal scaling

Transactions

SQL:

  • strong ACID support

NoSQL:

  • eventual consistency in many systems

Use Cases

SQL:

  • banking
  • ERP systems
  • transactional applications

NoSQL:

  • social media
  • real-time analytics
  • IoT
  • large-scale distributed systems

Real-World Example

An e-commerce platform may use:

  • MySQL for orders and payments
  • Redis for caching
  • MongoDB for product catalog storage

Modern architectures often combine both SQL and NoSQL databases depending on business requirements and scalability needs.


Describe a Relational Database Schema

A relational database schema is the logical structure that defines how data is organized in a relational database. It specifies:

  • tables
  • columns
  • data types
  • relationships
  • constraints
  • keys

The schema acts as a blueprint for storing and managing data consistently.

In relational databases, data is stored in tables where:

  • rows represent records
  • columns represent attributes

Components of a Relational Schema

Tables

Tables store related data.

Example:

  • Users
  • Orders
  • Products

Columns

Columns define data fields and their data types.

Example:

user_id INT
name VARCHAR(100)
email VARCHAR(255)

Primary Key

Uniquely identifies each row in a table.

Foreign Key

Creates relationships between tables.

Constraints

Enforce data integrity rules such as:

  • NOT NULL
  • UNIQUE
  • CHECK

Relationships

Tables can have:

  • one-to-one
  • one-to-many
  • many-to-many relationships

Real-World Example

In an online shopping system:

Users Table:

user_id
name
email

Orders Table:

order_id
user_id
amount

The user_id in Orders references Users table using a foreign key relationship.

Benefits of Relational Schema

  • organized data structure
  • reduced redundancy
  • improved consistency
  • easier querying
  • strong integrity enforcement

A well-designed relational schema is extremely important because poor schema design may lead to:

  • duplicated data
  • inconsistent records
  • slow performance
  • difficult maintenance

Database schema design is therefore a critical part of enterprise application architecture.


What is a Primary Key, and Why is it Important?

A primary key is a column or combination of columns that uniquely identifies each record in a database table. No two rows in a table can have the same primary key value, and primary key columns cannot contain NULL values.

Primary keys are fundamental to relational database design because they ensure that each record can be uniquely identified and accessed efficiently.

Characteristics of a Primary Key

Unique

Each value must be distinct.

Not Null

Primary key fields cannot contain NULL values.

Stable

Values should not change frequently.

Indexed Automatically

Most databases automatically create indexes for primary keys.

Example

Users Table:

user_id | name | email

Here:

  • user_id acts as the primary key

Importance of Primary Keys

Unique Identification

Ensures each record is distinguishable.

Relationship Management

Foreign keys reference primary keys to establish relationships.

Faster Query Performance

Indexed primary keys improve lookup speed.

Data Integrity

Prevents duplicate records.

Supports Normalization

Helps organize relational data efficiently.

Real-World Example

In an employee management system:

  • employee_id uniquely identifies employees

Even if two employees share the same name:

  • employee_id remains unique

Types of Primary Keys

Simple Primary Key

Single column key.

Example:

student_id

Composite Primary Key

Combination of multiple columns.

Example:

student_id + course_id

Primary keys are critical because relational databases rely heavily on unique record identification for maintaining relationships and ensuring reliable data management.


Can You Explain What a Foreign Key is and its Role in the Database?

A foreign key is a column or set of columns in one table that references the primary key of another table. Foreign keys establish relationships between tables and help maintain referential integrity in relational databases.

The main purpose of a foreign key is to ensure that related data remains consistent across tables.

Example

Users Table:

user_id | name

Orders Table:

order_id | user_id | amount

Here:

  • user_id in Orders table is a foreign key referencing Users table.

Role of Foreign Keys

Establish Relationships

Foreign keys connect related tables.

Maintain Referential Integrity

Prevents invalid references between tables.

Prevent Orphan Records

Ensures referenced records exist.

Enable Joins

Allows related data retrieval across multiple tables.

Relationship Types

One-to-Many

One customer → many orders

One-to-One

One employee → one ID card

Many-to-Many

Implemented using junction tables.

Foreign Key Constraints

Databases enforce rules such as:

  • INSERT validation
  • DELETE restrictions
  • UPDATE consistency

Common behaviors:

  • CASCADE
  • SET NULL
  • RESTRICT

Real-World Example

In a banking application:

  • customer_id in Accounts table references Customers table

This ensures accounts cannot exist without valid customers.

Benefits of Foreign Keys

  • improved data consistency
  • relationship management
  • prevention of invalid data
  • easier querying

Foreign keys are essential in relational database design because they help maintain structured and reliable relationships between business entities.

What is Database Normalization, and Why Do We Use It?

Database normalization is the process of organizing data in a relational database to reduce redundancy and improve data integrity. It involves dividing large tables into smaller related tables and defining relationships between them using keys.

The main goal of normalization is to eliminate duplicate data and ensure that the database structure is efficient, consistent, and maintainable.

Without normalization, databases may suffer from:

  • duplicated records
  • inconsistent data
  • update anomalies
  • insertion issues
  • deletion problems

Normalization is achieved through a series of normal forms such as:

  • First Normal Form (1NF)
  • Second Normal Form (2NF)
  • Third Normal Form (3NF)
  • Boyce-Codd Normal Form (BCNF)

First Normal Form (1NF)

Ensures:

  • each column contains atomic values
  • no repeating groups exist

Example:
Instead of storing:

phone_numbers = "12345,67890"

Store phone numbers separately.

Second Normal Form (2NF)

Ensures:

  • table is already in 1NF
  • non-key columns depend fully on the primary key

Third Normal Form (3NF)

Ensures:

  • table is already in 2NF
  • non-key columns depend only on the primary key

Benefits of Normalization

Reduces Data Redundancy

Duplicate data storage is minimized.

Improves Data Consistency

Updates occur in one place only.

Prevents Data Anomalies

Avoids:

  • update anomalies
  • insertion anomalies
  • deletion anomalies

Improves Data Integrity

Relationships remain consistent through keys and constraints.

Better Database Design

Creates cleaner and maintainable schemas.

Real-World Example

Without normalization:

student_name | course_name | instructor_name

Repeated data appears many times.

After normalization:

  • Students table
  • Courses table
  • Instructors table

Relationships are maintained using keys.

Challenges of Over-Normalization

Highly normalized databases may require:

  • multiple joins
  • complex queries

which can sometimes reduce performance.

Normalization is extremely important in transactional systems such as:

  • banking
  • ERP
  • inventory management
  • healthcare systems

because data consistency and integrity are business critical.


What is Denormalization and When Would You Consider It?

Denormalization is the process of intentionally adding redundant data into a database to improve read performance and reduce complex joins. While normalization focuses on eliminating redundancy, denormalization introduces controlled redundancy for performance optimization.

In large-scale applications, highly normalized databases may require multiple joins to retrieve data, which can increase query execution time. Denormalization helps improve performance in read-heavy systems.

Denormalization is commonly used in:

  • analytics systems
  • reporting platforms
  • data warehouses
  • high-traffic applications

Why Denormalization is Used

The primary goal is improving:

  • query performance
  • response time
  • scalability

especially when read operations occur far more frequently than write operations.

Example

Normalized Structure:

Orders Table
Customers Table
Products Table

Retrieving complete order details requires multiple joins.

Denormalized Structure:

Orders Table contains customer_name and product_name directly

This reduces joins and improves query speed.

Benefits of Denormalization

Faster Read Performance

Fewer joins improve query execution time.

Simplified Queries

Queries become easier and faster.

Better Reporting Performance

Useful for dashboards and analytics systems.

Improved Scalability

High-volume read systems perform better.

Disadvantages of Denormalization

Data Redundancy

Duplicate data increases storage usage.

Update Complexity

Changes must update multiple locations.

Risk of Inconsistency

Duplicate data may become inconsistent.

More Maintenance

Managing redundant data requires careful design.

When to Use Denormalization

Read-Heavy Applications

Systems with significantly more reads than writes.

Reporting Systems

Analytics queries often require denormalized structures.

Distributed Systems

Microservices may use denormalized read models for performance.

Caching and Search Optimization

Denormalized data improves fast retrieval.

Real-World Example

In an e-commerce application:

  • product information may be copied into order records

This allows faster order history retrieval without expensive joins.

Modern architectures often combine:

  • normalized transactional databases
  • denormalized reporting systems

to balance consistency and performance effectively.


Compare and Contrast the DROP, DELETE, and TRUNCATE Commands

DROP, DELETE, and TRUNCATE are SQL commands used to remove data, but they differ significantly in behavior, performance, rollback capability, and impact on database objects.

DELETE Command

DELETE removes specific rows from a table.

Example:

DELETE FROM employees WHERE department='HR';

Characteristics:

  • removes selected rows
  • supports WHERE condition
  • logged row by row
  • can usually be rolled back
  • table structure remains intact

Advantages:

  • selective deletion
  • transactional control

Disadvantages:

  • slower for large datasets

TRUNCATE Command

TRUNCATE removes all rows from a table quickly.

Example:

TRUNCATE TABLE employees;

Characteristics:

  • removes all rows
  • cannot use WHERE clause
  • minimally logged
  • faster than DELETE
  • resets identity counters in many databases
  • table structure remains intact

Advantages:

  • high performance
  • efficient large data cleanup

Disadvantages:

  • cannot delete selective rows
  • rollback support depends on database system

DROP Command

DROP completely removes the database object itself.

Example:

DROP TABLE employees;

Characteristics:

  • removes table structure and data
  • deletes indexes and constraints
  • frees storage space completely
  • object no longer exists

Advantages:

  • complete object removal

Disadvantages:

  • data and structure are lost entirely

Key Differences

DELETE

  • removes rows selectively
  • supports WHERE clause
  • slower
  • table remains

TRUNCATE

  • removes all rows
  • faster
  • no WHERE clause
  • table remains

DROP

  • removes entire table object
  • deletes structure and data

Real-World Example

DELETE:

  • remove inactive users only

TRUNCATE:

  • clear temporary log table daily

DROP:

  • remove obsolete backup table permanently

Choosing the correct command is important because accidental misuse may lead to:

  • performance issues
  • unintended data loss
  • recovery complications

What is the Difference Between a Full Join and an Inner Join?

INNER JOIN and FULL JOIN are SQL join operations used to combine data from multiple tables, but they differ in how unmatched rows are handled.

INNER JOIN

An INNER JOIN returns only the rows that have matching values in both tables.

Example:

SELECT *
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id;

Behavior:

  • only matching records are returned
  • unmatched rows are excluded

Example:
If:

  • Employee belongs to existing department → included
  • Employee has invalid department → excluded

FULL JOIN

A FULL JOIN (FULL OUTER JOIN) returns:

  • matching rows from both tables
  • unmatched rows from left table
  • unmatched rows from right table

Example:

SELECT *
FROM employees e
FULL OUTER JOIN departments d
ON e.department_id = d.department_id;

Behavior:

  • matching rows appear normally
  • unmatched rows contain NULL values

Key Differences

INNER JOIN

Returns:

  • only matching rows

FULL JOIN

Returns:

  • matching rows
  • unmatched rows from both tables

NULL Handling

INNER JOIN:

  • no unmatched rows

FULL JOIN:

  • unmatched columns contain NULL

Real-World Example

Suppose:

Employees Table:

employee_id | department_id

Departments Table:

department_id | department_name

INNER JOIN:

  • shows employees assigned to valid departments only

FULL JOIN:

  • also shows:
    • employees without departments
    • departments without employees

When to Use INNER JOIN

  • retrieving related business data
  • transactional applications
  • filtering matching records

When to Use FULL JOIN

  • data comparison
  • audit reporting
  • identifying missing relationships

Understanding joins is extremely important because relational databases rely heavily on table relationships and query optimization.

Done reading this topic? Sign up free to track your progress.
Sign Up to Track