SQL & Database Fundamentals


1. What is a Database?

A Database is a structured collection of data that is organized, stored, and managed so it can be easily accessed, updated, and managed.

Key Points:

  • Stores information in a structured way (tables, rows, columns).
  • Enables efficient data retrieval and manipulation.
  • Used in almost every software application: websites, mobile apps, ERP systems, etc.

2. Types of Databases

TypeDescriptionExamples
Relational Database (RDBMS)Data is stored in tables with rows and columns, relationships between tables are maintainedMySQL, PostgreSQL, Oracle, SQL Server
NoSQL DatabaseNon-tabular, flexible schema, good for unstructured dataMongoDB, Cassandra, Redis
In-Memory DatabaseStores data in memory for faster accessRedis, Memcached
Cloud DatabaseHosted in the cloud, scalableAWS RDS, Google Cloud SQL, Azure SQL Database

3. What is SQL?

SQL (Structured Query Language) is a standard language used to communicate with relational databases.

Purpose:

  • Retrieve data (SELECT)
  • Insert data (INSERT)
  • Update data (UPDATE)
  • Delete data (DELETE)
  • Create and modify database objects (CREATE, ALTER, DROP)

4. Basic SQL Commands

A. Data Definition Language (DDL)

Used to create and modify database structure.

CommandDescriptionExample
CREATE DATABASECreate a new databaseCREATE DATABASE mydb;
CREATE TABLECreate a new tableCREATE TABLE users(id INT, name VARCHAR(50));
ALTER TABLEModify table structureALTER TABLE users ADD email VARCHAR(50);
DROP TABLEDelete a tableDROP TABLE users;

B. Data Manipulation Language (DML)

Used to manipulate data in tables.

CommandDescriptionExample
INSERT INTOInsert new recordsINSERT INTO users(id, name) VALUES(1, 'John');
SELECTRetrieve recordsSELECT * FROM users;
UPDATEUpdate existing recordsUPDATE users SET name='Mike' WHERE id=1;
DELETEDelete recordsDELETE FROM users WHERE id=1;

C. Data Control Language (DCL)

Used to control access to the database.

CommandDescriptionExample
GRANTGive permissionsGRANT SELECT ON users TO 'testuser';
REVOKERemove permissionsREVOKE SELECT ON users FROM 'testuser';

D. Transaction Control Language (TCL)

Used to manage transactions.

CommandDescription
COMMITSave all changes
ROLLBACKUndo changes
SAVEPOINTSet a point to rollback to

5. Database Concepts

  1. Table – Collection of related data in rows and columns.
  2. Row / Record – Single entry in a table.
  3. Column / Field – Attribute of a record.
  4. Primary Key (PK) – Unique identifier for each record.
  5. Foreign Key (FK) – Field linking one table to another.
  6. Index – Improves search performance.
  7. Normalization – Organizing data to reduce redundancy.
  8. Relationships:
    • One-to-One – One record in table A corresponds to one record in table B.
    • One-to-Many – One record in table A corresponds to multiple records in table B.
    • Many-to-Many – Many records in table A correspond to many in table B (via junction table).

6. SQL Joins

Used to combine data from multiple tables.

Join TypeDescription
INNER JOINReturns records with matching values in both tables
LEFT JOIN (or LEFT OUTER)Returns all records from left table, matched records from right table
RIGHT JOIN (or RIGHT OUTER)Returns all records from right table, matched records from left table
FULL OUTER JOINReturns all records when there is a match in one of the tables
CROSS JOINReturns Cartesian product (all combinations)

7. Best Practices for SQL & Databases

  1. Always use primary keys for tables.
  2. Avoid redundant data; normalize tables.
  3. Use indexes to improve performance.
  4. Use transactions for critical operations.
  5. Write clear and optimized queries to reduce load.
  6. Backup databases regularly.
  7. Use descriptive names for tables and columns.

8. Summary

  • Database stores structured information efficiently.
  • SQL is used to create, read, update, and delete data in relational databases.
  • Basic SQL commands include DDL, DML, DCL, and TCL.
  • Key concepts include tables, keys, relationships, joins, and normalization.
  • Good practices improve performance, scalability, and reliability.