Author: saqibkhan

  • Secondary Storage Devices

    Databases handle huge volumes of data that must be stored efficiently and accessed reliably. Database management systems use the primary memory (RAM) for generating speed while handling active operations, however RAMs are volatile and costly for large datasets. This is where the secondary storage devices come into the picture.

    Secondary storage devices are non-volatile, provide higher capacities, and are quite cost-effective. These devices are essential for long-term data storage in DBMS. Read this chapter to get a good understanding of the types of secondary storage devices used in DBMS.

    Secondary Storage Devices for Databases

    For databases, we need stable storages. Secondary storage devices store data persistently. They are slower than primary memory but offer significantly larger storage capacities and are more affordable per byte. Data stored in secondary storage remains intact even when the system is turned off, ensuring data permanence.

    Common secondary storage devices are mainly of two types â€“ magnetic disks and magnetic tapes. Both these types of secondary storage devices have different use cases based on their performance and accessibility features.

    Magnetic Disks

    Magnetic Disks or Hard Disk Drives (HDD) are one of the most commonly used storage devices in DBMS. These devices are used for both personal and enterprise systems as they provide a perfect balance of performance, durability, and cost.

    How Do Magnetic Disks Work?

    Magnetic disks are circular plates coated with a magnetic material. Data is stored on these plates by magnetizing the areas on the disk that represent binary values of “0” or “1”. Modern magnetic disks typically come in either single-sided or double-sided formats. Single-sided disks store data on one surface, while double-sided disks utilize both the surfaces for higher storage capacity.

    Data Organization on Disks

    The structure of magnetic disks is specially designed to maximize storage and facilitate fast access. Disks have three major parts that we need to consider −

    • Tracks − Concentric circles on the surface of the disk where data is stored.
    • Sectors − Divisions of a track that hold a fixed amount of data, usually 512 to 8192 bytes.
    • Cylinders − Groups of tracks with the same diameter across multiple platters. Cylinders allow for faster data retrieval as the read/write head doesn’t need to move between tracks.

    A modern magnetic disk might implement Zone Bit Recording (ZBR). Here the tracks in different zones have varying numbers of sectors. This optimization techniques allows for higher storage density in the outer tracks without compromising performance.

    Data Organization on Disks

    Advantages of Magnetic Disks

    Following are the advantages of using magnetic disks −

    • Random Access − Magnetic disks, unlike sequential storage, allow direct access to specific data blocks, which makes them highly efficient for databases.
    • High Capacity − Disks can store terabytes of data. They are suitable for modern applications with large datasets.
    • Durability − Magnetic disks are designed to withstand repeated read and write operations.

    Performance Example: Seagate Cheetah Disk

    The Seagate Cheetah 15K.6 is a high-performance magnetic disk that illustrates the capabilities of modern storage. With a formatted capacity of 450 GB and rotational speeds of 15,000 rpm, it achieves an internal transfer rate of up to 2225 Mb/sec. Its average seek time is 3.4 ms for read operations, which makes it ideal for enterprise-level DBMS where speed is critical.

    Magnetic Tape Storage: A Reliable Backup Solution

    Magnetic tapes are another category of secondary storage devices. These devices are not so popular now, however they still remain indispensable for archival and backup purposes.

    How Do Magnetic Tapes Work?

    Magnetic tapes are sequential storage devices. Here the data is stored on long strips of magnetic material wound onto reels or cartridges. Accessing the data stored on magnetic tapes means scanning through previous blocks to reach the desired one. It is this property of sequential access that makes the tapes slower than disks for random data retrieval.

    Characteristics of Magnetic Tapes

    Given below are some of the important characteristics of magnetic tapes −

    • High Storage Capacity − Modern magnetic tapes can store a very large volume of data, like hundreds of gigabytes per cartridge. It makes them suitable for large-scale backups.
    • Cost-Effectiveness − Tapes are cheaper than disks, both in terms of initial cost and long-term storage.
    • Sequential Access − Magnetic tapes are slower for specific data retrieval. They take longer time in reading or writing large continuous datasets.

    Real-World Example: Sun Storage SL8500

    The Sun Storage SL8500 is an example of such a magnetic tape. With a storage capacity of up to 70 petabytes and throughput rates reaching 193.2 TB/hour, this system is ideal for enterprises handling massive backups. Robotic arms and automatic labeling are used with them.

    Applications of Magnetic Tapes

    Given below are some of the important applications of magnetic tapes −

    • Backup Storage − Tapes are essential tools for creating periodic backups of databases. They protect against disk failures and data corruption.
    • Archiving − Magnetic tapes are ideal for storing and archiving historical or seldom-used data for future reference.
    • Disaster Recovery − Tapes offer a reliable solution for recovering data in catastrophic scenarios.

    Buffering in Secondary Storage

    No doubt secondary storages have larger capacity, but their speed often lags behind that of primary memory devices. To bridge this gap, DBMS uses buffering techniques to optimize data transfer between disks and main memory.

    • Double Buffering − Double buffering is a widely used technique where two buffers are alternated during data transfer. While one buffer is being filled with data from the disk, the other is processed by the CPU. This overlap ensures continuous data flow, reducing delays caused by waiting for disk operations.
    • Impact on Performance − Double buffering improves the efficiency significantly when transferring multiple blocks of data. For example, when processing a database query that spans several disk blocks, the system can simultaneously read and process data, minimizing the idle time.

    Access Times in Magnetic Disks

    Consider the following three key steps while accessing data that is stored on magnetic disks −

    • Seek Time − It’s the time taken to position the read/write head over the correct track.
    • Rotational Delay − The wait for the desired sector to rotate under the read/write head.
    • Block Transfer Time − The time required to move the data block from disk to memory.

    Example − A disk rotating at 15,000 rpm has an average rotational delay of 2 milliseconds. Now consider with seek time and block transfer time, the total delay can range from 9 to 60 milliseconds. While this is relatively fast, it will still be slower than the speeds achievable by the primary memory.

    Applications of Secondary Storage in DBMS

    Secondary storage devices are crucial for several DBMS operations −

    • Online Storage − Magnetic disks handle active databases that require frequent read/write operations.
    • Backup and Recovery − The data stored on magnetic tapes can be restored in case of failure or corruption.
    • Archival Systems − Outdated but legally required records are stored on tapes to save on costs while meeting compliance requirements.
    • Scalable Storage Solutions − Large-scale systems, like storage area networks (SANs), use secondary storage for managing enterprise-level databases.

    Limitations of Secondary Storage

    Despite their benefits, secondary storage devices face certain limitations −

    • Latency − Access times are significantly slower compared to primary memory.
    • Maintenance − Tapes require regular maintenance to ensure data integrity over time.
    • Costs for High Performance − While basic storage is cheap, high-performance disks like the Seagate Cheetah can be costly.

    Conclusion

    In this chapter, we presented in detail the role of secondary storage devices in DBMS. Starting with an overview of the importance and features of secondary storage devices, we focussed on the organization of magnetic disks, including real-world examples like the Seagate Cheetah 15K.6.

    In addition, we covered the use of magnetic tapes for backup and archival purpose, highlighting systems like the Sun Storage SL8500. Thereafter, we explored the buffering techniques and how they improve the performance of secondary storage devices. We finished the chapter by highlighting the applications and limitations of secondary storage devices in DBMS.

  • File Structure

    Relative data and information is stored collectively in file formats. A file is a sequence of records stored in binary format. A disk drive is formatted into several blocks that can store records. File records are mapped onto those disk blocks.

    File Organization

    File Organization defines how file records are mapped onto disk blocks. We have four types of File Organization to organize file records −

    File Organization

    Heap File Organization

    When a file is created using Heap File Organization, the Operating System allocates memory area to that file without any further accounting details. File records can be placed anywhere in that memory area. It is the responsibility of the software to manage the records. Heap File does not support any ordering, sequencing, or indexing on its own.

    Sequential File Organization

    Every file record contains a data field (attribute) to uniquely identify that record. In sequential file organization, records are placed in the file in some sequential order based on the unique key field or search key. Practically, it is not possible to store all the records sequentially in physical form.

    Hash File Organization

    Hash File Organization uses Hash function computation on some fields of the records. The output of the hash function determines the location of disk block where the records are to be placed.

    Clustered File Organization

    Clustered file organization is not considered good for large databases. In this mechanism, related records from one or more relations are kept in the same disk block, that is, the ordering of records is not based on primary key or search key.

    File Operations

    Operations on database files can be broadly classified into two categories −

    • Update Operations
    • Retrieval Operations

    Update operations change the data values by insertion, deletion, or update. Retrieval operations, on the other hand, do not alter the data but retrieve them after optional conditional filtering. In both types of operations, selection plays a significant role. Other than creation and deletion of a file, there could be several operations, which can be done on files.

    • Open − A file can be opened in one of the two modes, read mode or write mode. In read mode, the operating system does not allow anyone to alter data. In other words, data is read only. Files opened in read mode can be shared among several entities. Write mode allows data modification. Files opened in write mode can be read but cannot be shared.
    • Locate − Every file has a file pointer, which tells the current position where the data is to be read or written. This pointer can be adjusted accordingly. Using find (seek) operation, it can be moved forward or backward.
    • Read − By default, when files are opened in read mode, the file pointer points to the beginning of the file. There are options where the user can tell the operating system where to locate the file pointer at the time of opening a file. The very next data to the file pointer is read.
    • Write − User can select to open a file in write mode, which enables them to edit its contents. It can be deletion, insertion, or modification. The file pointer can be located at the time of opening or can be dynamically changed if the operating system allows to do so.
    • Close − This is the most important operation from the operating systems point of view. When a request to close a file is generated, the operating system
      • removes all the locks (if in shared mode),
      • saves the data (if altered) to the secondary storage media, and
      • releases all the buffers and file handlers associated with the file.

    The organization of data inside a file plays a major role here. The process to locate the file pointer to a desired record inside a file various based on whether the records are arranged sequentially or clustered.

  • Storage System

    Databases are stored in file formats, which contain records. At physical level, the actual data is stored in electromagnetic format on some device. These storage devices can be broadly categorized into three types −

    Memory Types
    • Primary Storage − The memory storage that is directly accessible to the CPU comes under this category. CPU’s internal memory (registers), fast memory (cache), and main memory (RAM) are directly accessible to the CPU, as they are all placed on the motherboard or CPU chipset. This storage is typically very small, ultra-fast, and volatile. Primary storage requires continuous power supply in order to maintain its state. In case of a power failure, all its data is lost.
    • Secondary Storage − Secondary storage devices are used to store data for future use or as backup. Secondary storage includes memory devices that are not a part of the CPU chipset or motherboard, for example, magnetic disks, optical disks (DVD, CD, etc.), hard disks, flash drives, and magnetic tapes.
    • Tertiary Storage − Tertiary storage is used to store huge volumes of data. Since such storage devices are external to the computer system, they are the slowest in speed. These storage devices are mostly used to take the back up of an entire system. Optical disks and magnetic tapes are widely used as tertiary storage.

    Memory Hierarchy

    A computer system has a well-defined hierarchy of memory. A CPU has direct access to it main memory as well as its inbuilt registers. The access time of the main memory is obviously less than the CPU speed. To minimize this speed mismatch, cache memory is introduced. Cache memory provides the fastest access time and it contains data that is most frequently accessed by the CPU.

    The memory with the fastest access is the costliest one. Larger storage devices offer slow speed and they are less expensive, however they can store huge volumes of data as compared to CPU registers or cache memory.

    Magnetic Disks

    Hard disk drives are the most common secondary storage devices in present computer systems. These are called magnetic disks because they use the concept of magnetization to store information. Hard disks consist of metal disks coated with magnetizable material. These disks are placed vertically on a spindle. A read/write head moves in between the disks and is used to magnetize or de-magnetize the spot under it. A magnetized spot can be recognized as 0 (zero) or 1 (one).

    Hard disks are formatted in a well-defined order to store data efficiently. A hard disk plate has many concentric circles on it, called tracks. Every track is further divided into sectors. A sector on a hard disk typically stores 512 bytes of data.

    Redundant Array of Independent Disks

    RAID or Redundant Array of Independent Disks, is a technology to connect multiple secondary storage devices and use them as a single storage media.

    RAID consists of an array of disks in which multiple disks are connected together to achieve different goals. RAID levels define the use of disk arrays.

    RAID 0

    In this level, a striped array of disks is implemented. The data is broken down into blocks and the blocks are distributed among disks. Each disk receives a block of data to write/read in parallel. It enhances the speed and performance of the storage device. There is no parity and backup in Level 0.

    RAID 0

    RAID 1

    RAID 1 uses mirroring techniques. When data is sent to a RAID controller, it sends a copy of data to all the disks in the array. RAID level 1 is also called mirroring and provides 100% redundancy in case of a failure.

    RAID 1

    RAID 2

    RAID 2 records Error Correction Code using Hamming distance for its data, striped on different disks. Like level 0, each data bit in a word is recorded on a separate disk and ECC codes of the data words are stored on a different set disks. Due to its complex structure and high cost, RAID 2 is not commercially available.

    RAID 2

    RAID 3

    RAID 3 stripes the data onto multiple disks. The parity bit generated for data word is stored on a different disk. This technique makes it to overcome single disk failures.

    RAID 3

    RAID 4

    In this level, an entire block of data is written onto data disks and then the parity is generated and stored on a different disk. Note that level 3 uses byte-level striping, whereas level 4 uses block-level striping. Both level 3 and level 4 require at least three disks to implement RAID.

    RAID 4

    RAID 5

    RAID 5 writes whole data blocks onto different disks, but the parity bits generated for data block stripe are distributed among all the data disks rather than storing them on a different dedicated disk.

    RAID 5

    RAID 6

    RAID 6 is an extension of level 5. In this level, two independent parities are generated and stored in distributed fashion among multiple disks. Two parities provide additional fault tolerance. This level requires at least four disk drives to implement RAID.

    RAID 6
  • Trigger and Schema Modification

    Triggers and schema modifications are quite useful in managing and automating database operations. Triggers are special programs that automatically run when certain events take place in a database, like inserting, updating, or deleting data. Schema modification, on the other hand, focuses on changing the structure of the database itself. Schema modifications are used for adding or removing tables, columns, or constraints. Read this chapter to learn how to use triggers for automation and how to modify database schemas.

    Sample Tables and Data

    In the examples of this chapter, we will use an e-commerce database. The associated tables and the data are given below −

    Given below is the Customers table −

    customer_idfirst_namelast_nameemailphone
    1AnuJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012

    Here is the Products table −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005

    The Orders table is as follows −

    order_idcustomer_idproduct_idquantityorder_date
    1110112023-11-01 10:00:00
    2210322023-11-02 12:30:00
    3310212023-11-03 15:45:00

    Triggers in SQL

    Triggers are like “event listeners” in a database. They run automatically when specific actions take place in a table. The operations could be anything like INSERT, UPDATE, or DELETE.

    Creating a Trigger

    First of all, one must understand how to create a trigger. In the following example, when a new order is placed, the stock quantity for the ordered product should automatically decrease.

    CREATETRIGGER UpdateStock
    AFTERINSERTON Orders
    FOR EACH ROWBEGINUPDATE Products
       SET stock_quantity = stock_quantity - NEW.quantity
       WHERE product_id = NEW.product_id;END;

    This trigger automatically updates the stock quantity after processing new orders. It does the following −

    • The AFTER INSERT trigger runs whenever a new row is added inside the Orders table.
    • The NEW keyword refers to the newly inserted row.
    • Finally, the trigger updates the Products table by reducing the stock for the ordered product.

    Adding an Order

    When an order is placed for a product, the Orders table gets updated −

    INSERTINTO Orders (order_id, customer_id, product_id, quantity, order_date)VALUES(4,1,102,3,'2023-11-05 10:00:00');

    The trigger updates the Products table and now it would be as follows −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0017
    103Office ChairFurniture150.005

    Here, we can see the stock_quantity for Smartphone decreases from 20 to 17 after the order is placed.

    Deleting a Trigger

    If we no longer need a trigger, we can simply remove it. We can drop an existing trigger using the DROP TRIGGER statement.

    DROPTRIGGER UpdateStock;

    It removes the UpdateStock trigger from the database.

    Schema Modification in SQL

    Schema modification refers to altering the structure of the database, which can include adding or removing tables, columns, or constraints.

    Adding a New Table

    Let’s create an Employees table to track the employees in the system −

    CREATETABLE Employees (
       employee_id INTPRIMARYKEYAUTO_INCREMENT,
       first_name VARCHAR(50),
       last_name VARCHAR(50),
       position VARCHAR(50),
       salary DECIMAL(10,2),
       hire_date DATEDEFAULTCURRENT_DATE);

    The Employees Table Schema looks as follows −

    employee_idfirst_namelast_namepositionsalaryhire_date
    (empty)

    The new table is now ready for storing employee data.

    Modifying an Existing Table

    Sometimes we may want to update a table by adding or removing its columns. The following example demonstrates how you can add a column for loyalty points.

    Example: Add a Column for Loyalty Points

    To track customer loyalty points, let us see how to add a column to the Customers table −

    ALTERTABLE Customers
    ADD loyalty_points INTDEFAULT0;

    The updated Customers table would be as follows −

    customer_idfirst_namelast_nameemailphoneloyalty_points
    1AnuJoshi[email protected]123-456-78900
    2BimalSaha[email protected]234-567-89010
    3ChandanTudu[email protected]345-678-90120

    Removing a Column

    If a column is no longer needed, we can simply drop it. Use the following query to remove the loyalty_points column from the Customers table −

    ALTERTABLE Customers
    DROPCOLUMN loyalty_points;

    The updated Customers table would be as follows −

    customer_idfirst_namelast_nameemailphone
    1AnuJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012

    Renaming a Table

    To rename a table, we can use the RENAME TABLE statement. Use the following query to rename the Orders table to CustomerOrders −

    RENAMETABLE Orders TO CustomerOrders;

    The table Orders is now called CustomerOrders.

    Dropping a Table

    If a table is no longer needed, we can remove it entirely. You can use the following query to drop the Employees table −

    DROPTABLE Employees;

    It deletes the Employees table along with all its data.

    When to Use Triggers and Schema Modifications?

    We generally use triggers for automation, like updating related data or maintaining logs. One must avoid overusing triggers because they can make debugging harder.

    Schema Modifications are used while adapting to new business requirements. One must be cautious when dropping columns or tables. As it may lead to data loss.

  • Views in SQL

    Views in SQL are like virtual tables that do not store data themselves but act as saved SQL queries that we can reuse. We can think of views as a snapshot of a query result. Views give us a simplified way to look at or interact with the data.

    Views are quite handy in improving query readability. They can also be used for restricting access to sensitive data, or combining data from multiple tables into a single view. In this chapter, we will use a set of examples to demonstrate what views are, their types, and how to use them effectively in practical aspects.

    What are SQL Views?

    view is a predefined SQL query stored in the database. It acts like a virtual table that updates automatically whenever the underlying data changes. We can use views to −

    • Simplify complex queries
    • Create customized perspectives for different users
    • Restrict access to specific columns or rows

    Sample Tables and Data

    We will use the following set of tables in the examples of this chapter to demonstrate the practical aspects of using views in SQL −

    Here is the Customers table −

    customer_idfirst_namelast_nameemailphone
    1AntaraJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012

    This is the Products table −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005

    And, the Orders table is as follows −

    order_idcustomer_idproduct_idquantityorder_date
    1110112023-11-01 10:00:00
    2210322023-11-02 12:30:00
    3310212023-11-03 15:45:00
    4110312023-11-04 14:00:00

    Creating Views in SQL

    To work with views we must see how we can make a view from the existing table. We can create a view using the CREATE VIEW statement. When a view is created, we can query it like a table.

    Example: View for Customer Orders

    Let us create a view to display customer names along with the products they ordered.

    CREATEVIEW CustomerOrders ASSELECT c.first_name, c.last_name, p.product_name, o.quantity, o.order_date
    FROM Orders o
    JOIN Customers c ON o.customer_id = c.customer_id
    JOIN Products p ON o.product_id = p.product_id;

    Use the following query to fetch all the records from the CustomerOrders view −

    SELECT*FROM CustomerOrders;

    It will fetch all the records from the view −

    first_namelast_nameproduct_namequantityorder_date
    AntaraJoshiLaptop12023-11-01 10:00:00
    BimalSahaOffice Chair22023-11-02 12:30:00
    ChandanTuduSmartphone12023-11-03 15:45:00
    AntaraJoshiOffice Chair12023-11-04 14:00:00

    This view combines data from three tables, which makes it easier to fetch customer orders.

    Updating the Data Using Views

    Some views allow updates, but it depends on the complexity of the view and database constraints.

    Example: Update through a Simple View

    Let us make a view to display product stock levels.

    CREATEVIEW ProductStock ASSELECT product_id, product_name, stock_quantity
    FROM Products;

    Use the following query to fetch the details from the ProductStock view −

    SELECT*FROM ProductStock;

    It will fetch the records from the ProductStock view −

    product_idproduct_namestock_quantity
    101Laptop10
    102Smartphone20
    103Office Chair5

    Updating the Stock Quantity through the View

    Let us now use the following query to update the ProductStock view −

    UPDATE ProductStock
    SET stock_quantity = stock_quantity -1WHERE product_id =101;

    Now, query the ProductStock view again −

    SELECT*FROM ProductStock;

    It will fetch the updated data from the ProductStock view −

    product_idproduct_namestock_quantity
    101Laptop9
    102Smartphone20
    103Office Chair5

    Here, we can see the update reflects in the underlying Products table as well.

    Types of Views in SQL

    SQL views can be categorized into two main types −

    • Simple Views
    • Complex Views

    Let’s start with a simple example of Simple Views.

    Simple Views

    Simple views are based on a single table. Simple views allow data updates if no calculations or aggregate functions are used.

    Example: Simple View for Customers

    In this query, we are creating a simple view from the Customers table −

    CREATEVIEW CustomerDetails ASSELECT first_name, last_name, email
    FROM Customers;

    Complex Views

    Complex Views combine data from multiple tables or include calculations. They can also be aggregate functions or GROUP BY clauses. Complex Views generally do not allow updates.

    Example: Complex View for Total Revenue

    Let us make a view to calculate the total revenue for each product −

    CREATEVIEW ProductRevenue ASSELECT p.product_name,SUM(o.quantity * p.price)AS total_revenue
    FROM Orders o
    JOIN Products p ON o.product_id = p.product_id
    GROUPBY p.product_name;

    Use the following query to fetch the data from the ProductRevenue view −

    SELECT*FROM ProductRevenue;

    This complex view fetches the revenue generated by each product −

    product_nametotal_revenue
    Laptop1000.00
    Smartphone800.00
    Office Chair450.00

    Dropping the Views

    Like we delete tables, we can delete views as well. If we no longer need a view, we can delete it using the DROP VIEW statement.

    Example: Drop a View

    Use the following query to drop the ProductStock view −

    DROPVIEW ProductStock;

    It removes the ProductStock view but does not affect the underlying Products table.

    Advantages and Disadvantages of Using Views

    The following table highlights the advantages and disadvantages of using views in SQL queries −

    AdvantagesDisadvantages
    Simplifies Complex Queries − We can save a complicated query as a view and reuse it easily.Restricts Data Access − Views can limit the columns or rows that certain users can see.Encourages Reusability − Once a view is created, it can be used in multiple queries.Improves Readability − Queries referencing views are generally easier to read and understand to work with.Performance Overhead − Complex views can slow down performance, because the query runs every time we access the view.Limited Updates − Not all views allow data updates.Dependency Issues − Dropping or modifying the underlying tables can break views.
  • Join and Subquery in SQL

    In SQL querying, we sometimes need to fetch data from more than one table, for which we need to join the tables together or apply subqueries. Subqueries and the Join operation help us retrieve related data by linking the tables or running nested queries within a larger query.

    In this chapter, we will have an elaborate discussion on joins and subqueries with the help of clear examples and their resulting tables.

    Sample Tables and Data

    For the examples in this chapter, we will use the following three tables: Customers, Orders, and Products. We will use these three tables to demonstrate joins and subqueries.

    Here is the Customers table −

    customer_idfirst_namelast_nameemailphone
    1AmritaJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012

    This is the Products table −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005

    And, the Orders table is as follows −

    order_idcustomer_idproduct_idquantityorder_date
    1110112023-11-01 10:00:00
    2210322023-11-02 12:30:00
    3310212023-11-03 15:45:00
    4110312023-11-04 14:00:00

    Joins in SQL

    Joins combine rows from two or more tables based on a related column. In SQL we have option for several types of joins. We will see the most commonly used techniques.

    The Inner Join

    The Inner Join retrieves rows that have matching values in both tables. If some matching is not there the entire row will be neglected.

    Example: Fetch Customer Orders

    Use the following query to fetch all the customer orders −

    SELECT c.first_name, c.last_name, p.product_name, o.quantity, o.order_date
    FROM Orders o
    JOIN Customers c ON o.customer_id = c.customer_id
    JOIN Products p ON o.product_id = p.product_id;

    This query combines the Orders, Customers, and Products tables to display complete order details −

    first_namelast_nameproduct_namequantityorder_date
    AmritaJoshiLaptop12023-11-01 10:00:00
    BimalSahaOffice Chair22023-11-02 12:30:00
    ChandanTuduSmartphone12023-11-03 15:45:00
    AmritaJoshiOffice Chair12023-11-04 14:00:00

    The Left Join

    The Left Join retrieves all the rows from the left table and the matching rows from the right table. If no match exists, NULL values are returned for the right table’s columns.

    Example: Customers with or without Orders

    Use the following query to fetch the customer details with or without orders −

    SELECT c.first_name, c.last_name, o.order_id
    FROM Customers c
    LEFTJOIN Orders o ON c.customer_id = o.customer_id;

    It fetches all customers, including those with no orders (if there were any).

    first_namelast_nameorder_id
    AmritaJoshi1
    AmritaJoshi4
    BimalSaha2
    ChandanTudu3

    The Right Join

    The Right Join retrieves all the rows from the right table and the matching rows from the left table.

    Example: Products with or without Orders

    Use the following query to fetch the products with or without orders −

    SELECT p.product_name, o.order_id
    FROM Products p
    RIGHTJOIN Orders o ON p.product_id = o.product_id;

    The query fetches all the rows from the Orders table. When it is a product that does not match, it will be NULL. All products are matched in this case.

    product_nameorder_id
    Laptop1
    Office Chair2
    Smartphone3
    Office Chair4

    The Full Outer Join

    Combining both the Left and Right join makes the new join a Full Outer Join. It will fetch all the rows from both the tables.

    Subqueries in SQL

    Subqueries are nested queries inside another query. They can return single values or entire tables. Subqueries are useful for solving complex problems step-by-step.

    Subqueries in WHERE Clause

    Subqueries are mostly used in the WHERE clause to filter data based on the result of another query.

    Example: Find Customers Who Ordered the Most Expensive Product

    Here, we have three queries all together. The innermost query finds the product_id having the maximum number of products. Then, this product_id is matched with the Orders table in the second inner query, from where it gets the customer_id. Based on the customer_id, it fetches the name of the customer.

    SELECT first_name, last_name
    FROM Customers
    WHERE customer_id IN(SELECT customer_id
       FROM Orders
       WHERE product_id =(SELECT product_id
    
      FROM Products
      WHERE price =(SELECTMAX(price)FROM Products)));</pre>

    This query identifies the customers who ordered the most expensive product (Laptop).

    first_namelast_name
    AmritaJoshi

    Subqueries in SELECT Clause

    Subqueries can also be used in the SELECT clause to include calculated data in the output.

    Example: Total Quantity Ordered by Each Product

    Use the following query to get the details of the total quantity ordered by each product −

    SELECT product_name,(SELECTSUM(quantity)FROM Orders
       WHERE Orders.product_id = Products.product_id)AS total_quantity_ordered
    FROM Products;

    This query calculates the total quantity ordered for each product using a subquery. It is an advanced version of GROUP BY querying.

    product_nametotal_quantity_ordered
    Laptop1
    Smartphone1
    Office Chair3

    Subqueries in FROM Clause

    Subqueries in the FROM clause are called derived tables and are used to create temporary tables.

    Example: Find Revenue per Product

    Use the following query to get the revenue details per product −

    SELECT product_name, revenue
    FROM(SELECT p.product_name,SUM(o.quantity * p.price)AS revenue
       FROM Orders o
       JOIN Products p ON o.product_id = p.product_id
       GROUPBY p.product_name
    )AS ProductRevenue;

    The subquery calculates revenue per product, and the main query fetches it for display.

    product_namerevenu
    Laptop1000.00
    Smartphone800.00
    Office Chair450.00

    When to Use Joins vs. Subqueries

    Both the approaches, joins and subqueries, are equally useful and often interchangeable. However, the joining operation is avoided sometimes due to its resulting large intermediate table size. In such cases, using an inner query is much more efficient. But, choosing the right option depends on the situation.

    • Use Joins when you need to combine rows from multiple tables directly.
    • Use Subqueries when you need to break down a problem into steps or filter the data based on calculated values.
  • Aggregate Functions in SQL

    In SQL querying sometimes we use special functions called aggregate functions that let us perform calculations on our data to find summaries, totals, averages, and other meaningful insights. If we have ever wanted to know the total sales for a month, the average rating of a product, or the number of customers who placed an order, then we would have to use the aggregate functions. Read this chapter to learn how to use aggregate functions in SQL.

    Sample Tables and Data

    We will take two sample tables and a set of examples to show how aggregate functions work. Here we are using the following schema and data from an e-commerce setup. The two tables we are going to use are Orders and Products.

    Here is the Products table −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005
    104DeskFurniture250.008

    And, the Orders table is as follows −

    order_idproduct_idquantityorder_date
    110122023-11-01 10:00:00
    210312023-11-02 12:30:00
    310232023-11-03 15:45:00
    410322023-11-04 17:20:00
    510112023-11-05 14:00:00

    Aggregate Functions in SQL

    Aggregate functions in SQL include the following functions −

    • The COUNT function
    • The SUM function
    • The AVG function
    • The MIN and MAX Functions

    In the following sections of this chapter, we will have examples to demonstrate how you can apply these aggregate functions in SQL.

    The COUNT Function: Counting the Rows

    The COUNT function is used to find the number of rows in a table or the number of records that match a condition. It is like counting the items in a shopping cart.

    Example: Total Number of Orders

    In this query, we have use the aggregate function COUNT to get the total number of orders from the Orders table −

    SELECTCOUNT(*)AS total_orders
    FROM Orders;

    It will fetch the following output −

    total_orders
    5

    Here, we can see it is simply counting all the rows in the Orders table. The result tells us there are five orders in the Orders table.

    Example: Count the Products in Each Category

    Use the following query to count the products in each category −

    SELECT category,COUNT(*)AS product_count
    FROM Products
    GROUPBY category;

    It will fetch the following data from the Products table −

    categoryproduct_count
    Electronics2
    Furniture2

    Here, this query is grouping the products by category and counting how many products are there in each category.

    The SUM Function: Adding the Values

    The SUM function calculates the total of a numeric column. It is quite useful in finding totals, like sales or stock quantities.

    Example: Total Quantity of Products Ordered

    Use the following query to get the total quantity of products ordered −

    SELECTSUM(quantity)AS total_quantity_ordered
    FROM Orders;

    It will fetch the following data from the Orders table −

    total_quantity_ordered
    9

    The SUM function adds up the quantity column in the Orders table. It gives us the total number of products ordered across all orders.

    Example: Total Revenue Generated

    To calculate the total revenue generated, we need to join the Orders and Products tables and multiply the quantity by price for each product.

    SELECTSUM(o.quantity * p.price)AS total_revenue
    FROM Orders o
    JOIN Products p ON o.product_id = p.product_id;

    Here, we will get the following output −

    total_revenue
    4900.00

    The above query simply multiplies the quantity of each order by the price of the corresponding product. Then, it adds up the results to calculate the total revenue.

    The AVG Function: Calculating Averages

    The AVG function calculates the average value of a numeric column. It’s useful for finding the trends or understanding typical values.

    Example: Average Price of Products

    You can use the following query to get the average price of products in the Products table −

    SELECTAVG(price)AS average_price
    FROM Products;

    It will fetch the following data −

    average_price
    550.00

    The AVG function gives the average price of all the products in the Products table.

    Example: Average Quantity Ordered per Order

    Use the AVG function as shown in the following query to get the average quantity ordered per order −

    SELECTAVG(quantity)AS average_quantity_per_order
    FROM Orders;

    It will produce the following output −

    average_quantity_per_order
    1.80

    In this query, we used the AVG function to calculate the average number of items ordered in each order.

    The MIN and MAX Functions: Finding the Minimum and Maximum Values

    The MIN and MAX functions are used to find the smallest and largest values in a column.

    Example: Cheapest and Most Expensive Products

    Use the following query to find the cheapest and the most expensive products in the Products table −

    SELECTMIN(price)AS cheapest_product,MAX(price)AS most_expensive_product
    FROM Products;

    It will fetch the following data −

    cheapest_productmost_expensive_product
    150.001000.00

    Here, we can see that the MIN function finds the lowest price and the MAX function finds the highest price.

    Example: Earliest and Latest Order Dates

    Here is another example of how you can use the MIN and MAX functions to get the earliest and latest order dates −

    SELECTMIN(order_date)AS first_order_date,MAX(order_date)AS last_order_date
    FROM Orders;

    It will fetch the following data from the Orders table −

    first_order_datelast_order_date
    2023-11-01 10:00:002023-11-05 14:00:00

    Here, this query shows the earliest and most recent dates when orders were placed.

    The GROUP BY Clause: Aggregating Data by Groups

    The GROUP BY clause works with the aggregate functions to organize data into groups. Think of it as sorting the data into buckets before performing calculations.

    Example: Total Quantity Ordered per Product

    You can use the following query to find the total quantity ordered per product −

    SELECT p.product_name,SUM(o.quantity)AS total_quantity
    FROM Orders o
    JOIN Products p ON o.product_id = p.product_id
    GROUPBY p.product_name;

    It will produce the following output −

    product_nametotal_quantity
    Laptop3
    Smartphone3
    Office Chair3

    This query groups the orders by product and then calculates the total quantity ordered for each.

    Example: Revenue per Category

    Use the following query to calculate the revenue per each product category −

    SELECT p.category,SUM(o.quantity * p.price)AS total_revenue
    FROM Orders o
    JOIN Products p ON o.product_id = p.product_id
    GROUPBY p.category;

    It will give you the following data as the output −

    categorytotal_revenue
    Electronics4400.00
    Furniture500.00

    Here, we are grouping the products by category and then calculating the total revenue for each.

    Importance of Aggregate Functions

    Aggregate functions help us perform data analysis. Instead of going through each of the records manually, we can use aggregate functions to quickly find totals, averages, and other key metrics with just a few lines of SQL. Aggregate functions are quite useful in creating reports or dashboards that summarize large datasets.

  • CRUD Operations in SQL

    To work with a relational database, one must understand how to create a database with SQL and then learn how to insert, read, update, and delete the data, because these are the basic operations for transactions. CRUD stands for Create, Read, Update, and Delete. The four fundamental operations for managing data in a database. If databases were like a library, the CRUD operations would be the ways we can add new books, search for a specific title, edit details of an existing book, or remove one from the shelf.

    Rea this chapter to get a good understanding of all the four CRUD operations. We will take a sample table and a set of examples to explain the CRUD operations in detail.

    Sample Tables and Data

    To understand the CRUD operations, we must take examples. We will use the following sample table throughout this chapter −

    Here is the Customers table −

    customer_idfirst_namelast_nameemailphone
    1AmritaJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012

    And, this is the Products table −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005

    With these tables in place, let us move ahead and explore the CRUD operations!

    The Create Operation (Inserting Data into Tables)

    Creating data that involves adding new rows to a table using the INSERT statement. This is like putting new files into a cabinet or adding new contacts to our phone contacts.

    Example: Adding a New Customer

    Use the following SQL command to insert a new customer row in the existing Customers table −

    INSERTINTO Customers (customer_id, first_name, last_name, email, phone)VALUES(4,'Deb','Mallick','[email protected]','456-789-0123');

    The Updated Customers Table would be as follows −

    customer_idfirst_namelast_nameemailphone
    1AmritaJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012
    4DebMallick[email protected]456-789-0123

    Example: Adding a New Product

    Use the following command to insert a new product row into the Products table −

    INSERTINTO Products (product_id, product_name, category, price, stock_quantity)VALUES(104,'Desk','Furniture',250.00,8);

    The Updated Products Table would be as follows −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005
    104DeskFurniture250.008

    The Read Operation (Fetching Data from Tables)

    After creating we must read the data from the database. For that the ‘R’ in CRUD comes. This is done using the SELECT statement. It is like opening a book and searching through it to find the information we need.

    Example: Fetch All Customers

    Fetch all the data from the Customers table −

    SELECT*FROM Customers;

    It will get you the following output −

    customer_idfirst_namelast_nameemailphone
    1AmritaJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012
    4DebMallick[email protected]456-789-0123

    Since we inserted a new record (Deb) in the Customers table, it is present there in the current table.

    Example: Fetch Products with Stock Below 10

    Now, let’s use the WHERE clause to filter the data and fetch something specific −

    SELECT product_name, stock_quantity
    FROM Products
    WHERE stock_quantity <10;

    This query will fetch all the products with their stock quantity less than 10 −

    product_namestock_quantity
    Office Chair5

    The Update Operation (Modifying the Existing Data)

    Updating the data means changing the values in existing rows, for which we use the UPDATE command. Think of it as editing a contact’s phone number on our phone.

    Example: Updating a Customer’s Email

    You can use the following command to update a customer’s email in the existing records −

    UPDATE Customers
    SET email ='[email protected]'WHERE customer_id =1;

    After executing the above query, the Updated Customers Table would look as follows −

    customer_idfirst_namelast_nameemailphone
    1AmritaJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012
    4DebMallick[email protected]456-789-0123

    Example: Restocking a Product

    Use the following query to update the Products table after restocking a product −

    UPDATE Products
    SET stock_quantity = stock_quantity +5WHERE product_id =103;

    Now, the Updated Products Table would reflect the updated stock quantities −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.0010
    104DeskFurniture250.008

    The stock of “Office Chair” has now been updated from 5 to 10.

    The Delete Operation (Removing Data from Tables)

    Deleting the data means removing existing rows from a table, which is done using the DELETE statement. Consider erasing a contact from our phone. We are deleting the record but not the whole table.

    Example: Deleting a Customer

    The following query shows how you can use the DELETE command to remove a customer’s record from the Customers table −

    DELETEFROM Customers
    WHERE customer_id =4;

    With the above query, the Updated Customers Table would look as follows −

    customer_idfirst_namelast_nameemailphone
    1AmritaJoshi[email protected]123-456-7890
    2BimalSaha[email protected]234-567-8901
    3ChandanTudu[email protected]345-678-9012

    Notice that Deb’s record has been removed.

    Example: Removing a Product from Inventory

    Use the following query to remove a product from the inventory −

    DELETEFROM Products
    WHERE product_id =104;

    The Updated Products Table will now look as follows −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.0010

    The “Desk” product is no longer in the inventory.

    Why CRUD Operations Are Important

    CRUD operations are the most basic operations for any database operator; they allow us to:

    • Add new information (like customers or products)
    • Retrieve specific details when needed
    • Keep the data up to date
    • Remove outdated or incorrect data

    Without CRUD operations, we cannot update a database, in which case the data will be historical and of no use in real-time applications.

  • Querying in SQL

    We use SQL for querying a relational database. SQL is a command-based language to operate on tables and databases, which is why querying is at the heart of using SQL. SQL is the medium by which we ask a database questions and get the answers we need. Whether you are retrieving customer orders, checking stock levels, or just counting the number of employees, querying makes it all happen.

    Read this chapter to learn the basics of querying in SQL. We will start with some sample tables with data and then understand how to write queries with their results.

    Sample Tables and Data

    To make things clear, we will use three tables: Customers, Products, and Orders. Given below are the tables with some initial data −

    Here’s the Customers Table −

    customer_idfirst_namelast_nameemailphone
    1AliceJohnson[email protected]123-456-7890
    2BobSmith[email protected]234-567-8901
    3CarolTaylor[email protected]345-678-9012

    This is the Products Table −

    product_idproduct_namecategorypricestock_quantity
    101LaptopElectronics1000.0010
    102SmartphoneElectronics800.0020
    103Office ChairFurniture150.005

    And, the third one is the Orders Table –

    order_idcustomer_idproduct_idquantityorder_date
    1110112023-11-01 10:00:00
    2210322023-11-02 12:30:00
    3310212023-11-03 15:45:00

    With this setup, let us start writing some SQL queries. After each query, we will see the results and explain what is happening.

    Retrieving the Data with SELECT Command

    The SELECT statement is used for querying. It is used to fetch data from one or more tables.

    Example: Get All Customer Data

    Use the following command −

    SELECT * FROM Customers;

    It will fetch the following rows −

    customer_idfirst_namelast_nameemailphone
    1AliceJohnson[email protected]123-456-7890
    2BobSmith[email protected]234-567-8901
    3CarolTaylor[email protected]345-678-9012

    It fetches everything in the Customers table. The “*” symbol means “all columns”.

    Example: Fetch Specific Columns

    To fetch a specific set of columns, use the following command −

    SELECT first_name, email FROM Customers;

    It will fetch the following rows –

    first_nameemail
    Alice[email protected]
    Bob[email protected]
    Carol[email protected]

    Here, we are only fetching the first_name and the email. This command is useful when we do not need all the data.

    Filtering the Data with WHERE Clause

    The WHERE clause lets us filter rows based on conditions.

    Example: Find Customers with Last Name ‘Smith’

    Here, we have used the WHERE clause to filter the data −

    SELECT * FROM Customers
    WHERE last_name ='Smith';

    It will fetch the following rows −

    customer_idfirst_namelast_nameemailphone
    2BobSmith[email protected]234-567-8901

    The WHERE clause filters only those rows where the last_name is ‘Smith.’

    Example: Products below a Certain Price

    Here is another example of how the WHERE clause is used −

    SELECT product_name, price
    FROM Products
    WHERE price <900;

    It will fetch the following rows −

    product_nameprice
    Office Chair150.00
    Smartphone800.00

    Here, we only get a list of those products that are priced below 900.

    Sorting the Data with ORDER BY Clause

    The ORDER BY clause helps us sort the results.

    Example: Sort the Products by Price (Low to High)

    Use the following query to sort the products by their price –

    SELECT product_name, price
    FROM Products
    ORDER BY price ASC;

    It will fetch the following rows –

    product_nameprice
    Office Chair150.00
    Smartphone800.00
    Laptop1000.00

    The ASC keyword sorts in ascending order. To reverse, use DESC.

    Aggregating the Data with Functions

    SQL has built-in functions like COUNT, SUM, and AVG for analyzing data.

    Example: Count the Total Products

    Use the following command to get a count of the total number of products −

    SELECT COUNT(*) AS total_products
    FROM Products;

    It will fetch the following rows −

    total_products
    3

    This query counts the total number of rows in the Products table.

    Example: Find the Average Product Price

    Use the following query to get the average product price −

    SELECT AVG(price) AS average_price
    FROM Products;

    It will fetch the following rows −

    average_price
    650.00

    The AVG function calculates the average price of all products.

    Joining the Tables

    Joins are used to combine data from two or more tables based on a related column.

    Example: Find Customer Orders

    SELECT Customers.first_name, Customers.last_name, Products.product_name, Orders.quantity
    FROM Orders
    JOIN Customers ON Orders.customer_id = Customers.customer_id
    JOIN Products ON Orders.product_id = Products.product_id;

    It will fetch the following rows −

    first_namelast_nameproduct_namequantity
    AliceJohnsonLaptop1
    BobSmithOffice Chair2
    CarolTaylorSmartphone1

    This query combines the OrdersCustomers, and Products tables. It shows which customer ordered what.

    Using Aliases for Simplicity

    Aliases let us give the temporary names to tables or columns for easier reading.

    Example: Simplify a Join

    SELECT c.first_name, c.last_name, p.product_name, o.quantity
    FROM Orders o
    JOIN Customers c ON o.customer_id = c.customer_id
    JOIN Products p ON o.product_id = p.product_id;

    It gives the same output as Example 8 but it uses short aliases (o, c, p) for tables.

    Nested Queries

    Subqueries, or queries within queries, are handy for more complex tasks. Let’s understand how with the help of the following example −

    Example: Find Customers Who Ordered the Most Expensive Product

    Here’s an example of a set of nested queries to find the customers who ordered the most expensive product −

    SELECT first_name, last_name
    FROM Customers
    WHERE customer_id =(
    	SELECT customer_id
    	FROM Orders
    	WHERE product_id =(
    		SELECT product_id
    		FROM Products
    		WHERE price =(SELECT MAX(price) FROM Products)));

    It will fetch the following rows −

    first_namelast_name
    AliceJohnson

    Here, we are finding the customer who ordered the product with the highest price.

    Grouping the Data with GROUP BY Clause

    The GROUP BY clause organizes the rows into groups based on one or more columns.

    Example: Total Quantity Ordered Per Product

    Here is an example that demonstrates how you can use the GROUP BY clause −

    SELECT product_id, SUM(quantity) AS total_quantity
    FROM Orders
    GROUP BY product_id;

    It will fetch the following rows –

    product_idtotal_quantity
    1011
    1021
    1032

    It groups the orders by product and calculates the total quantity for each.

  • Types of SQL Commands

    Developers and analysts use the structured query language (SQL) to interact with relational databases. SQL is made up of different types of sub-languages that let us perform different tasks. Think of it like a toolkit; each tool is specially used for a particular job. Read this chapter to get a clear understanding of the types of SQL languages and how they are used.

    The Four Types of SQL Languages

    SQL can be divided into four main types −

    • Data Definition Language (DDL)
    • Data Manipulation Language (DML)
    • Data Control Language (DCL)
    • Transaction Control Language (TCL)

    These are used for distinct purpose. Let us see them one by one.

    Data Definition Language (DDL)

    DDL commands are used for defining and modifying the structure of a database. They help in setting up the foundation of the database. DDL commands are is used for creating tables, adding or deleing columns, and changing table properties.

    Common Commands in DDL

    Here’s a list of the common commands used in DDL −

    • CREATE − To make new tables, databases, or other structures
    • ALTER − To modify an existing structure, like adding a column
    • DROP − To delete tables or databases
    • TRUNCATE − To quickly empty a table without removing its structure

    Example: Setting Up a Table

    Let’s understand the steps by setting up a table −

    CREATE TABLE Customers (
       customer_id INT PRIMARY KEY,
       first_name VARCHAR(50),
       last_name VARCHAR(50),
       email VARCHAR(100) UNIQUE
    );

    Here, we are creating a table called Customers. It has columns for customer ID, first name, last name, and email. Observe closely how we define the data types: INT for numbers and VARCHAR for text.

    Modifying an Existing Table

    Sometimes we need to change an existing table. For example, if you need to add a phone number column in the Customers table, then you can use the ALTER command −

    ALTER TABLE Customers
    ADD phone VARCHAR(15);

    Data Manipulation Language (DML)

    DML commands are used for querying the tables and fetching relevant data. When the tables are set up, we actually work with the data inside them. It is all about adding, updating, deleting, and fetching the stored data.

    Following are the common commands in DML −

    • INSERT − To add new data
    • UPDATE − To change existing data
    • DELETE − To remove data
    • SELECT − To retrieve data

    Example: Adding Data

    Let’s add a new row in our existing Customers table −

    INSERT INTO Customers (customer_id, first_name, last_name, email, phone)
    VALUES (1, 'Alice', 'Johnson', '[email protected]', '123-456-7890');

    It adds a new row for Alice.

    Fetching Data

    To know what is inside the Customers table, we can use the SELECT command −

    SELECT first_name, last_name, email
    FROM Customers;

    It fetches the first name, last name, and email of all customers.

    Updating the Records

    If Alice changes her phone number, we can update it in the table −

    UPDATE Customers
    SET phone ='987-654-3210'
    WHERE customer_id =1;

    Deleting the Records

    Let us say Alice asks to remove her data entirely, then you would use the following command −

    DELETE FROM Customers
    WHERE customer_id =1;

    Data Control Language (DCL)

    DCL commands are all about managing the access. It is like setting up locks and keys to make sure only authorized people can access or change the precious data.

    Following are the common commands in DCL −

    • GRANT − To give permissions
    • REVOKE − To take permissions away

    Example: Granting Access

    Suppose you want to give a new employee permission to view the Customers data, then you would use the following command −

    GRANT SELECT ON Customers TO new_employee;

    Revoking Access

    If an employee leaves, then you can take back their access using the following command −

    REVOKE SELECT ON Customers FROM new_employee;

    DCL ensures the database is secure and only the authorized people have the right access.

    Transaction Control Language (TCL)

    Transactions are like bundles of database actions that should be treated as a single unit. TCL commands ensure that these actions are completed fully or not at all. TCL commands help keep the database consistent.

    Following are the common commands in TCL –

    • COMMIT − To save the changes permanently
    • ROLLBACK − To undo the changes
    • SAVEPOINT − To set a checkpoint in a transaction

    Example: A Transaction in Action

    Imagine we are processing an order where stock quantities need to be updated. Now, an order record needs to be created. These actions must be carried out together −

    BEGIN TRANSACTION;
    
    UPDATE Products
    SET stock_quantity = stock_quantity - 1
    WHERE product_id =101;
    
    INSERT INTO Orders (order_id, customer_id, order_date, status, total_amount)
    VALUES (1001, 1, NOW(), 'Pending', 99.99);
    
    COMMIT;

    If something goes wrong during the transaction, we can roll it back −

    ROLLBACK;

    How Do Different Types of SQL Commands Work Together?

    All the four types SQL sub-languages (DDL, DML, DCL, and TCL) work in unison to provide seamless support and service to the end-users.

    • We can use DDL to set up the database structure.
    • DML helps us to populate and manage the data.
    • DCL secures the data by controlling who can access it.
    • TCL ensures the database stays consistent during critical operations.

    Let us see at a practical scenario using an e-commerce example −

    • We create a Products table using DDL.
    • We insert product details using DML.
    • We grant the team permission to view and update the Products table using DCL.
    • During a big sale, we process orders and during such events, TCL ensures data consistency.

    You can mix and match these different types of SQL commands to handle a variety of database tasks. Whether you are building a system from scratch, have some troubleshooting issues, or need to optimize the performance of an existing database, you will invariably need to use these SQL commands. For instance, without DDL, we cannot even have a database structure. Without DML, the tables would just sit there. DCL ensures the data is not tampered with, while TCL protects it from corruption during complex operations.