"SQL Fundamentals: A Beginner's Guide to Managing Databases"

"SQL Fundamentals: A Beginner's Guide to Managing Databases"

1. Introduction

Structured Query Language (SQL) is the standard language used to manage and interact with databases. SQL is indispensable for handling structured data, enabling you to store, modify, and query information effectively. This guide introduces the SQL basics, from creating a database to executing fundamental queries.


2. Prerequisites

To follow this documentation, you will need:

  • A basic understanding of SQL syntax.

  • A SQL database management system (DBMS), such as MySQL or PostgreSQL, installed. For a graphical interface, you can use MySQL Workbench or phpMyAdmin.


3. Step 1: Create a Database

To begin, open your SQL client and create a new database using the command:

CREATE DATABASE company_db;

Verifying Database Creation

To confirm that the database was created, list all databases:

SHOW DATABASES;

Switch to the newly created database:

USE company_db;

4. Step 2: Create a Table

Next, create a table named employees to store employee records. The table will include the following fields: employee ID, name, position, department, and salary.

CREATE TABLE employees (
    employee_id INT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    position VARCHAR(100),
    department VARCHAR(100),
    salary DECIMAL(10, 2)
);

Explanation of Fields

  • employee_id: Unique identifier for each employee, set to auto-increment.

  • name: Stores the employee's name.

  • position: Stores the employee's job title.

  • department: Indicates the department the employee belongs to.

  • salary: Stores the employee's salary as a decimal value.

To verify the creation of the table:

SHOW TABLES;

5. Step 3: Insert Data into the Table

With the table ready, add some records:

INSERT INTO employees (name, position, department, salary)
VALUES
('Alice Johnson', 'Software Engineer', 'Development', 85000.00),
('Bob Smith', 'Product Manager', 'Management', 95000.00),
('Charlie Evans', 'HR Manager', 'Human Resources', 65000.00),
('Diana Roberts', 'Data Scientist', 'Analytics', 90000.00),
('Ethan White', 'QA Engineer', 'Testing', 72000.00);

To view the inserted data:

SELECT * FROM employees;

6. Step 4: Perform Basic SQL Queries

Now that data is in place, we can perform queries to retrieve and manipulate it.

Retrieve Data with SELECT

  1. View All Employees
    Display all records in the employees table:

     SELECT * FROM employees;
    
  2. View Specific Columns
    Show only the name and position columns:

     SELECT name, position FROM employees;
    
  3. Filter Records with WHERE Clause
    Retrieve employees in the Development department:

     SELECT * FROM employees WHERE department = 'Development';
    
  4. Sort Data
    List employees by salary in descending order:

     SELECT * FROM employees ORDER BY salary DESC;
    

Update Data with UPDATE

To change an employee's salary, use the following:

UPDATE employees
SET salary = 88000.00
WHERE name = 'Alice Johnson';

Verify the update:

SELECT * FROM employees WHERE name = 'Alice Johnson';

Delete Data with DELETE

To delete a specific record (e.g., "Charlie Evans"):

DELETE FROM employees WHERE name = 'Charlie Evans';

Confirm the deletion:

SELECT * FROM employees;

7. Step 5: Advanced SQL Queries (Bonus)

Aggregate Functions

Use AVG to calculate the average salary:

SELECT AVG(salary) AS average_salary FROM employees;

Group By Department

Count employees in each department:

SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department;

8. Conclusion

This documentation introduced the basics of SQL, including creating databases, adding tables, inserting data, and performing basic queries. With these foundational skills, users can effectively manage databases, paving the way for more advanced SQL operations in web development, data analytics, and software engineering.