

Structured Query Language, commonly known as SQL provides a wide range of commands and capabilities for working with relational databases, making it a powerful and versatile tool for data manipulation and retrieval in various applications, from web development to data analysis.
Table of Contents
What is SQL?
Structured Query Language (SQL), is a domain-specific programming language used for managing and manipulating data in relational database management systems (RDBMS). SQL is a powerful tool for querying databases efficiently.
SQL is the standard language for interacting with relational databases, and it allows you to perform various operations, including data retrieval, data insertion, data updating, and data deletion.
Type of Database Schema
Logical Schema:
A logical schema defines the logical structure and organization of data within the database. It focuses on the relationships between data elements and how data is conceptually organized, without specifying physical storage details.
Logical schemas are used in the early stages of database design to model the data’s structure and relationships before translating them into a physical schema.
Physical Schema:
A physical schema defines how data is physically stored and organized on the storage devices. It specifies details such as file locations, data storage formats, and access paths.
Physical schemas are used to optimize database performance by determining how data is stored and accessed efficiently. They are crucial for database administrators and system architects.
Relational Schema:
A relational schema defines the structure of tables, their columns, data types, and relationships (foreign keys) between tables in a relational database. It represents the core elements of a database system.
Relational schemas are used in relational database management systems (RDBMS) like MySQL, PostgreSQL, and Oracle. They are the foundation for creating tables, ensuring data integrity, and managing relationships.
Star Schema:
A star schema is a type of data warehousing schema. It consists of a central fact table containing key performance indicators (KPIs) and dimension tables that provide context to the KPIs. The central fact table is linked to dimension tables through foreign keys.
Star schemas are designed for data warehouses and data analysis applications where quick retrieval of summarized data is essential. They simplify complex queries and reporting.
Snowflake Schema:
A snowflake schema is an extension of the star schema. In a snowflake schema, dimension tables are further normalized, breaking them into smaller, related tables. This can reduce data redundancy but may complicate queries.
Snowflake schemas are used when data integrity is critical, and storage space is limited. They are common in data warehousing and business intelligence environments.
Hierarchical Schema:
A hierarchical schema organizes data in a tree-like structure with parent-child relationships. Each node can have multiple children, and it is used for representing hierarchical data like organizational structures or file systems.
Hierarchical schemas are employed when modeling hierarchical data, and they are supported by hierarchical database systems like XML databases.
NoSQL Schema (Schema-less):
In NoSQL databases, there is no fixed schema. Data is typically stored in documents, key-value pairs, or other formats without a predefined structure.
NoSQL databases like MongoDB or Cassandra use schema-less designs to allow flexibility and scalability. Data can vary between records, making it suitable for unstructured or semi-structured data.
Graph Schema:
A graph schema is used in graph databases to define nodes and relationships between them. It specifies the structure of the graph and the types of nodes and relationships that can exist.
Graph schemas are used in graph databases like Neo4j, which are designed to handle highly connected data. They are ideal for applications requiring complex network or social graph analysis.
Database Schema
A database schema consists of tables, columns, relationships, constraints, and other elements that collectively define the structure of a database. It provides a logical separation of data and ensures that objects with the same name in different schemas are distinct. A well-designed schema is essential for efficient data storage and retrieval. In DBMS the schema acts as a container for organizing and managing these database objects.
Key components related to a database schema:
- Tables: Tables are the primary objects in a database schema. They represent entities or data categories, such as “customers,” “orders,” or “products.” Each table consists of rows (records) and columns (fields) that store specific pieces of information.
- Columns: Columns define the attributes or properties of the data within a table. Each column is associated with a data type that specifies the kind of data it can hold, such as integers, text, dates, or binary data.
- Primary Key: A primary key is a column or a combination of columns that uniquely identifies each row in a table. It enforces data integrity and ensures that each record is unique.
- Foreign Key: A foreign key is a column or set of columns that establishes a relationship between two tables. It is used to ensure referential integrity, meaning that data in one table corresponds to data in another.
- Indexes: Indexes are data structures that improve the retrieval speed of data from a table. They are created on one or more columns to facilitate faster data access. Common types of indexes include primary keys and non-unique indexes.
- Constraints: Constraints are rules or conditions applied to columns or tables to maintain data integrity. Common constraints include NOT NULL (requiring a column to have a value), UNIQUE (ensuring unique values), and CHECK (specifying conditions that data must meet).
- Views: Views are virtual tables created by defining a SELECT statement. They allow users to access a specific subset of data from one or more tables without altering the underlying data.
- Stored Procedures: Stored procedures are precompiled SQL scripts stored in the database. They allow you to execute a sequence of SQL statements as a single unit, improving code reusability and security.
- Triggers: Triggers are database objects that are automatically executed when specific events occur, such as data changes in a table. They are often used for enforcing business rules or auditing data.
- Roles and Permissions: A schema can also include user roles and permissions that define who can access and modify the data in the database. These access controls help maintain data security.
SQL Statements or Commands
Data Control Language (DCL)
DCL commands are used to control access to data within a database, including permissions, access control, and user management.

GRANT: Grants specific privileges or permissions to a user or role.
GRANT SELECT, INSERT ON table_name TO user_name;
REVOKE: Revokes previously granted privileges or permissions from a user or role.
REVOKE SELECT ON table_name FROM user_name;
Data Definition Language (DDL)
DDL commands are used to define, modify, and manage the structure of the database, including tables, indexes, and constraints.

CREATE: Creates a new table with defined columns and constraints.
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... );
ALTER: Modifies an existing table, such as adding or dropping columns.
ALTER TABLE table_name ADD column_name datatype;
DROP: Deletes an entire table and its data.
DROP TABLE table_name;
Data Manipulation Language (DML)
DML commands are used to manipulate data stored in the database. These commands include operations like inserting, updating, and deleting data.

INSERT: Adds new records (rows) to a table.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
UPDATE: Modifies existing records in a table.
UPDATE table_name SET column1 = new_value WHERE condition;
DELETE: Removes records from a table.
DELETE FROM table_name WHERE condition;
Data Query Language (DQL)
DQL commands are used to retrieve data from the database.

SELECT: Retrieves data from one or more tables based on specified criteria.
SELECT column1, column2 FROM table_name WHERE condition;
Transaction Control Language (TCL)
TCL commands are used to manage transactions within the database, ensuring data integrity and consistency.

COMMIT: Saves all the changes made during the current transaction and ends the transaction.
COMMIT;
ROLLBACK: Discards all the changes made during the current transaction and undoes them.
ROLLBACK;
SAVEPOINT: Sets a point within a transaction to which you can later roll back.
SAVEPOINT point_name;
Whether you’re a database administrator managing large datasets, data analyst seeking insights, or a developer working on database-driven applications mastering SQL is an essential skill that can significantly boost your productivity and reduce performance issues in your database applications.