SQL Fundamentals

SQL Fundamentals

Great choice πŸ‘ Let’s cover the fundamentals of SQL (Structured Query Language) in a clear, practical way.


SQL Fundamentals

SQL is the standard language used to interact with relational databases. It allows you to create, read, update, and delete data (often called CRUD operations), as well as define and manage database structures.


1. Basic SQL Commands

a) SELECT – Retrieving Data

Used to fetch data from a table.

SELECT first_name, last_name 
FROM employees 
WHERE department = 'Sales';

πŸ‘‰ Returns names of employees in the Sales department.


b) INSERT – Adding Data

Used to insert new records.

INSERT INTO employees (first_name, last_name, department, salary) 
VALUES ('John', 'Doe', 'Sales', 5000);

πŸ‘‰ Adds a new employee record.


c) UPDATE – Modifying Data

Used to update existing records.

UPDATE employees 
SET salary = 6000 
WHERE first_name = 'John' AND last_name = 'Doe';

πŸ‘‰ Updates John Doe’s salary.


d) DELETE – Removing Data

Used to delete records.

DELETE FROM employees 
WHERE department = 'Interns';

πŸ‘‰ Removes all interns from the employees table.


2. Filtering and Sorting

  • WHERE β†’ filter records
  • ORDER BY β†’ sort results
  • LIMIT β†’ restrict number of rows

Example:

SELECT first_name, salary 
FROM employees 
WHERE salary > 4000 
ORDER BY salary DESC 
LIMIT 5;

πŸ‘‰ Gets top 5 highest-paid employees.


3. Using Functions

SQL includes built-in functions:

  • COUNT() β†’ counts rows
  • SUM() β†’ adds values
  • AVG() β†’ averages values
  • MIN() / MAX() β†’ lowest/highest values
SELECT department, AVG(salary) AS avg_salary
FROM employees 
GROUP BY department;

πŸ‘‰ Shows average salary per department.


4. Joins – Combining Data from Multiple Tables

Relational databases often store data in different tables. JOIN lets you connect them.

SELECT orders.order_id, customers.name 
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id;

πŸ‘‰ Retrieves orders along with customer names.

Types of joins:

  • INNER JOIN β†’ only matching rows
  • LEFT JOIN β†’ all from left table + matches
  • RIGHT JOIN β†’ all from right table + matches
  • FULL JOIN β†’ all rows from both sides

5. Creating and Modifying Tables

-- Create a table
CREATE TABLE employees (
    id INT PRIMARY KEY,
    first_name VARCHAR(50),
    last_name VARCHAR(50),
    department VARCHAR(50),
    salary DECIMAL(10,2)
);

-- Add a new column
ALTER TABLE employees ADD hire_date DATE;

6. Keys & Constraints

  • Primary Key β†’ uniquely identifies each row
  • Foreign Key β†’ enforces relationships between tables
  • Unique β†’ prevents duplicate values
  • Not Null β†’ ensures column must have a value
CREATE TABLE departments (
    dept_id INT PRIMARY KEY,
    dept_name VARCHAR(50) UNIQUE NOT NULL
);

7. Transactions

Used to group SQL operations into a single unit of work.

BEGIN TRANSACTION;

UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;

COMMIT;  -- or ROLLBACK if something goes wrong

πŸ‘‰ Ensures data consistency (important in banking, etc.).


βœ… With these fundamentals, you can query, insert, update, and manage data in most relational databases.






Leave a Reply