Introduction to Databases (RDBMS) #
What is a Database? #
A database is a structured collection of data stored electronically.
Example:
- Customer records
- Sales data
- Employee data
What is RDBMS? #
RDBMS (Relational Database Management System) stores data in tables (rows & columns) and allows relationships between them.
Key Concepts #
| Concept | Meaning |
|---|---|
| Table | Collection of data |
| Row | Single record |
| Column | Attribute/field |
| Primary Key | Unique identifier |
| Foreign Key | Link between tables |
Example Table #
| ID | Name | Age | City |
|---|---|---|---|
| 1 | Alex | 25 | London |
| 2 | John | 30 | New York |
What is SQL? #
SQL (Structured Query Language) is used to:
- Retrieve data
- Filter data
- Sort data
- Manage databases
SELECT Statement #
What is SELECT? #
Used to retrieve data from a table
Syntax #
SELECT column_name FROM table_name;
Examples #
Select all columns: #
SELECT * FROM Students;
Select specific columns:
SELECT Name, Age FROM Students;
FROM Clause #
What is FROM? #
Specifies which table to retrieve data from
Example #
SELECT Name FROM Students;
Students is the table
WHERE Clause #
What is WHERE? #
Used to filter data based on conditions
Syntax #
SELECT column FROM table WHERE condition;
Examples #
Filter by age: #
SELECT * FROM Students WHERE Age > 25;
Filter by text:
SELECT * FROM Students WHERE City = 'London';
Operators #
| Operator | Meaning |
|---|---|
| = | Equal |
| > | Greater than |
| < | Less than |
| >= | Greater or equal |
| <= | Less or equal |
| != | Not equal |
DISTINCT #
What is DISTINCT? #
Removes duplicate values
Syntax #
SELECT DISTINCT column FROM table;
Example
SELECT DISTINCT City FROM Students;
Shows unique cities only
ORDER BY #
What is ORDER BY? #
Used to sort data
Syntax #
SELECT column FROM table ORDER BY column ASC|DESC;
Examples #
Ascending (default): #
SELECT * FROM Students ORDER BY Age ASC;
Descending:
SELECT * FROM Students ORDER BY Age DESC;
LIMIT #
What is LIMIT? #
Used to restrict number of rows returned
Syntax #
SELECT column FROM table LIMIT number;
8.3 Example
SELECT * FROM Students LIMIT 3;
Returns first 3 rows
Combined Query Example #
SELECT DISTINCT City FROM Students WHERE Age > 25 ORDER BY City ASC LIMIT 5;
This query:
- Removes duplicates
- Filters age > 25
- Sorts cities
- Returns top 5 results
Execution Order (Important) #
SQL runs in this order:
- FROM
- WHERE
- SELECT
- DISTINCT
- ORDER BY
- LIMIT
Table #
| Clause | Purpose |
|---|---|
| SELECT | Choose columns |
| FROM | Specify table |
| WHERE | Filter data |
| DISTINCT | Remove duplicates |
| ORDER BY | Sort data |
| LIMIT | Restrict rows |
RDBMS → Stores data in tables SQL → Language to query data Core clauses:
- SELECT → retrieve
- FROM → source
- WHERE → filter
- DISTINCT → unique values
- ORDER BY → sort
- LIMIT → restrict results

