Introduction to SQL

Introduction to SQL

SQL, or Structured Query Language, is a standard programming language specifically designed for managing and manipulating relational databases. SQL is used to perform tasks such as querying data, inserting new records, updating existing data, deleting records, and defining database structures.


Key Features of SQL

  • Declarative Language: SQL specifies what to do rather than how to do it.
  • Data Manipulation: It allows retrieving, inserting, updating, and deleting data from tables.
  • Data Definition: SQL includes commands to create, modify, and delete database structures like tables.
  • Data Control: SQL manages permissions and access to the database through Data Control Language (DCL).

  • Common SQL Commands

    SQL commands can be categorized into five main types:

    • Data Query Language (DQL): Fetch data from the database.

      • Example: SELECT
    • Data Definition Language (DDL): Define or modify database structures.

      • Examples: CREATE, ALTER, DROP
    • Data Manipulation Language (DML): Modify data in the database.

      • Examples: INSERT, UPDATE, DELETE
    • Data Control Language (DCL): Control access to data.

      • Examples: GRANT, REVOKE
    • Transaction Control Language (TCL): Manage database transactions.

      • Examples: COMMIT, ROLLBACK, SAVEPOINT

    • Basic SQL Syntax

      Here is an overview of commonly used SQL commands with examples:


      1. SELECT Statement

      Used to retrieve data from a table.

        SELECT column1, column2
        FROM table_name
        WHERE condition;
      

      Example: Retrieve all employees from the employees table who earn more than $50,000.

        SELECT first_name, last_name, salary
        FROM employees
        WHERE salary > 50000;

      2. INSERT INTO Statement

      Used to insert new rows into a table.

        INSERT INTO table_name (column1, column2, column3)
        VALUES (value1, value2, value3);
      

      Example: Add a new employee to the employees table.

        INSERT INTO employees (first_name, last_name, job_title, salary)
        VALUES ('Jane', 'Doe', 'Software Engineer', 75000);            
      

      3. UPDATE Statement

      Used to update existing records.

        UPDATE table_name
        SET column1 = value1, column2 = value2
        WHERE condition;
      

      Example: Increase the salary of all employees in the IT department by 10%.

        UPDATE employees
        SET salary = salary * 1.10
        WHERE department = 'IT';
      

      4. DELETE Statement

      Used to delete records from a table.

        DELETE FROM table_name
        WHERE condition;
      

      Example: Remove employees who are no longer active.

        DELETE FROM employees
        WHERE status = 'Inactive';
       

      5. CREATE TABLE Statement

      Used to create a new table.

        CREATE TABLE table_name (
            column1 datatype constraints,
            column2 datatype constraints,
            ...
        );         
      

      Example: Create a customers table.

        CREATE TABLE customers (
            customer_id INT PRIMARY KEY,
            first_name VARCHAR(50),
            last_name VARCHAR(50),
            email VARCHAR(100),
            phone_number VARCHAR(15)
        );       
      

      6. ALTER TABLE Statement

      Used to modify the structure of an existing table.

        ALTER TABLE table_name
        ADD column_name datatype;
      

      Example: Add a new column date_of_birth to the customers table.

        ALTER TABLE customers
        ADD date_of_birth DATE;
      

      7. DROP TABLE Statement

      Used to delete an entire table.

        DROP TABLE table_name;
      

      Example: Remove the old_data table.

        DROP TABLE old_data;

      Conclusion

      SQL is a versatile and essential tool for database management. It is widely used across industries for tasks ranging from simple data retrieval to complex database operations. Mastering SQL involves understanding its syntax, practicing with examples, and applying it to real-world scenarios.

Next