A relational database is a way to store data in an organized structure of tables with rows and columns, making it easy to manage, query, and maintain relationships between different datasets. To interact with these databases, we use SQL (Structured Query Language) — the standard language for creating, reading, updating, and deleting data.
Today, for Day 36, the goal was to understand table creation, CRUD operations, filtering, sorting, and common data types.
What is SQL?
SQL stands for Structured Query Language. It is the standard language used to interact with relational databases.
Relational databases are systems where data is stored in tables with rows and columns. Popular relational databases include:
- MySQL
- PostgreSQL
- SQLite
- Microsoft SQL Server
Example Table:
| id | name | age |
|---|---|---|
| 1 | Ali | 23 |
| 2 | Sara | 25 |
Database Structure Concepts
Understanding SQL requires knowing some key database terms:
| Concept | Meaning |
|---|---|
| Database | Collection of tables |
| Table | Structured dataset |
| Row | Single record in a table |
| Column | Attribute of data |
| Primary Key | Unique identifier for a row |
| Foreign Key | Reference to another table |
Example: Users Table
| id | name |
|---|
✅ Tip: Always define a primary key to ensure each row is unique.
Creating Databases and Tables
Create a Database
CREATE DATABASE mydb;
Select (Use) the Database
USE mydb;
Create a Table
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
Concepts to Learn:
- Data types (
INT,VARCHAR,TEXT, etc.) - Primary keys
- Table structure
CRUD Operations in SQL
CRUD stands for Create, Read, Update, Delete — the four essential operations in SQL.
Create (Insert Data)
INSERT INTO users (id, name, age)
VALUES (1, 'Saad', 23);
Read (Select Data)
Select all columns:
SELECT * FROM users;
Select specific columns:
SELECT name, age FROM users;
Update Data
UPDATE users
SET age = 24
WHERE id = 1;
Delete Data
DELETE FROM users
WHERE id = 1;
Filtering Data
Filtering helps you fetch only the data you need.
WHERE Clause
SELECT * FROM users
WHERE age > 20;
Multiple Conditions
SELECT * FROM users
WHERE age > 20 AND name = 'Ali';
💡 Tip: Use OR and AND to combine conditions.
Sorting Results
SQL allows you to sort query results using ORDER BY.
SELECT * FROM users
ORDER BY age ASC;
Descending order:
SELECT * FROM users
ORDER BY age DESC;
Limiting Results
To control the number of results returned, use LIMIT. This is especially useful for pagination:
SELECT * FROM users
LIMIT 5;
SQL Data Types (Basic)
Common SQL data types you’ll encounter:
| Type | Use |
|---|---|
| INT | Numbers |
| VARCHAR | Short text |
| TEXT | Long text |
| BOOLEAN | True/False |
| DATE | Date values |
✅ Pro Tip: Choosing the correct data type is important for performance and storage efficiency.
🏁 Conclusion
SQL is an essential skill for any developer, data analyst, or anyone working with databases. By mastering tables, CRUD operations, filtering, sorting, and data types, you can interact with relational databases efficiently.
Learning SQL and understanding relational databases is incredibly valuable because nearly every application, website, or service relies on data stored in structured tables. Whether you want to build applications, analyze data, or manage systems, SQL empowers you to efficiently retrieve, manipulate, and maintain data.
Thank you for reading! Feel free to share your thoughts!
Top comments (0)