Intro of SQL



SQL

SQL (Structured Query Language) Introduction

SQL is a standard programming language used to manage and manipulate relational databases. It is essential for querying, updating, and managing data within a database system.

SQL is used to interact with databases such as MySQL, PostgreSQL, SQL Server, SQLite, and others.

Key Concepts of SQL:

  1. Database: A collection of related data organized in tables.
  2. Table: A structure within a database that stores data in rows and columns.
  3. Row (Record): A single, data entry in a table (i.e., a record).
  4. Column (Field): A single data attribute that describes the data type and content of the table (e.g., Name, Date of Birth, etc.).

Common SQL Commands

  1. SELECT - Retrieves data from the database.

    • Example:
      SELECT * FROM employees;
      This query selects all rows and columns from the employees table.
  2. INSERT INTO - Adds new data to a table.

    • Example:
      INSERT INTO employees (id, name, position) VALUES (1, 'John Doe', 'Manager');
      This query adds a new employee record to the employees table.
  3. UPDATE - Modifies existing data in a table.

    • Example:

      UPDATE employees SET position = 'Senior Manager' WHERE id = 1;
      This query updates the position of the employee with id 1.
  4. DELETE - Removes data from a table.

    • Example:
      DELETE FROM employees WHERE id = 1;
      This query deletes the employee with id 1 from the employees table.
  5. CREATE TABLE - Defines a new table and its columns.

    • Example:
      CREATE TABLE employees ( id INT PRIMARY KEY, name VARCHAR(100), position VARCHAR(50), hire_date DATE );
      This query creates a new table called employees with columns for id, name, position, and hire_date.
  6. ALTER TABLE - Modifies the structure of an existing table (e.g., adding/removing columns).

    • Example:

      ALTER TABLE employees ADD salary DECIMAL(10, 2);
      This query adds a new column salary to the employees table.
  7. DROP TABLE - Deletes an entire table from the database.

    • Example:
      DROP TABLE employees;
      This query deletes the employees table from the database.

  8. SQL Clauses

    • WHERE: Filters records based on specified conditions.

      • Example:
        SELECT * FROM employees WHERE position = 'Manager';
    • ORDER BY: Sorts the result set by one or more columns.

      • Example:

        SELECT * FROM employees ORDER BY hire_date DESC;
    • AND / OR: Combines multiple conditions in the WHERE clause.

      • Example:
        SELECT * FROM employees WHERE position = 'Manager' AND hire_date > '2020-01-01';
    • GROUP BY: Groups rows that have the same values into summary rows.

      • Example:
        SELECT position, COUNT(*) FROM employees GROUP BY position;
    • HAVING: Filters the results of GROUP BY (similar to WHERE, but for grouped data).

      • Example:
        SELECT position, COUNT(*) FROM employees GROUP BY position HAVING COUNT(*) > 5;

    Basic Data Types in SQL

    • INT: Integer numbers.
    • VARCHAR: Variable-length character string (text).
    • DATE: Date in 'YYYY-MM-DD' format.
    • DECIMAL: Fixed-point numbers (useful for currency or precise calculations).

    SQL Joins

    SQL joins allow you to combine data from two or more tables based on related columns.

    1. INNER JOIN: Returns records that have matching values in both tables.

      • Example:
        SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.id;
    2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table, and the matched records from the right table (returns NULL for non-matching rows in the right table).

      • Example:
        SELECT employees.name, departments.department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.id;
    3. RIGHT JOIN (or RIGHT OUTER JOIN): Similar to LEFT JOIN but returns all records from the right table.

    4. FULL OUTER JOIN: Returns all records when there is a match in either left or right table.


    Conclusion

    SQL is a powerful language used for managing and manipulating relational databases. It’s essential for tasks such as retrieving, inserting, updating, and deleting data. By mastering basic SQL commands and concepts like joins, aggregations, and filtering, you'll be well-equipped to work with databases effectively.

    Let me know if you need more detailed examples or have specific questions about SQL!



Post a Comment

0 Comments