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.