SQL is a standard language used to communicate with relational databases. It allows you to perform various operations like querying data, inserting records, updating records, and deleting records.
A relational database organizes data into tables, where each table consists of rows (records) and columns (attributes). Tables can be related to each other using keys, enabling complex data structures.
To practice SQL, you'll need a DBMS. Two popular choices are:
A SQL client like DBeaver or HeidiSQL provides a graphical interface to interact with your database.
Tables are the core structure in a relational database. Each table consists of columns with specific data types, such as INT
, VARCHAR
, and DATE
.
Creating a table is the first step in building a database. Here's how you can create an employees
table:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(50),
position VARCHAR(50),
salary DECIMAL(10, 2),
hire_date DATE
);
This table has columns for employee ID, name, position, salary, and hire date.
Once the table is created, you can insert records into it:
INSERT INTO employees (id, name, position, salary, hire_date)
VALUES (1, 'John Doe', 'Software Engineer', 60000.00, '2023-01-15');
This command inserts a new employee record into the employees
table.
The SELECT
statement is used to retrieve data from one or more tables. For example:
SELECT * FROM employees;
This command retrieves all records from the employees
table.
You can filter records using the WHERE
clause:
SELECT * FROM employees WHERE salary > 50000;
This retrieves records where the salary is greater than 50,000.
To sort records, use the ORDER BY
clause:
SELECT * FROM employees ORDER BY hire_date DESC;
This command retrieves records ordered by the hire date in descending order.
Tables can be joined to combine data from multiple tables. Here's an example of an inner join between employees
and departments
tables:
SELECT employees.name, departments.name AS department
FROM employees
JOIN departments ON employees.department_id = departments.id;
This query retrieves employee names along with their department names.
Aggregation functions like COUNT
, SUM
, and AVG
allow you to perform calculations on your data:
SELECT position, COUNT(*) AS count
FROM employees
GROUP BY position;
This query groups employees by position and counts the number of employees in each position.
Subqueries are queries within queries and are used for more complex operations:
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
This query retrieves names of employees whose salary is above the average salary.
Normalization is the process of organizing data to reduce redundancy. Here are the first three normal forms (NF):
Tables in a relational database can have different types of relationships:
Let's build a simple employee management system that tracks employee details, departments, and salary history.
employees: Stores employee details.
departments: Stores department information.
salaries: Stores salary history.
CREATE TABLE departments (
id INT PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE salaries (
employee_id INT,
amount DECIMAL(10, 2),
start_date DATE,
end_date DATE,
FOREIGN KEY (employee_id) REFERENCES employees(id)
);
Inserting Data
INSERT INTO departments (id, name) VALUES (1, 'HR'), (2, 'Engineering');
INSERT INTO salaries (employee_id, amount, start_date, end_date)
VALUES (1, 60000.00, '2023-01-15', NULL);
Querying Data
SELECT employees.name, departments.name AS department, salaries.amount
FROM employees
JOIN departments ON employees.department_id = departments.id
JOIN salaries ON employees.id = salaries.employee_id;
This query retrieves employee names, their department, and their current salary.
SQL is an essential skill for anyone working with data. By mastering SQL, you'll be able to manage and query databases efficiently, whether for small projects or large applications. Remember, practice is key to becoming proficient in SQL. Keep experimenting with different queries, and explore more advanced topics as you progress.