DEV Community

Cover image for Day 36 of #100DaysOfCode — SQL Basics
M Saad Ahmad
M Saad Ahmad

Posted on

Day 36 of #100DaysOfCode — SQL Basics

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 email

✅ Tip: Always define a primary key to ensure each row is unique.


Creating Databases and Tables

Create a Database

CREATE DATABASE mydb;
Enter fullscreen mode Exit fullscreen mode

Select (Use) the Database

USE mydb;
Enter fullscreen mode Exit fullscreen mode

Create a Table

CREATE TABLE users (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  age INT
);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Read (Select Data)

Select all columns:

SELECT * FROM users;
Enter fullscreen mode Exit fullscreen mode

Select specific columns:

SELECT name, age FROM users;
Enter fullscreen mode Exit fullscreen mode

Update Data

UPDATE users
SET age = 24
WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

Delete Data

DELETE FROM users
WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

Filtering Data

Filtering helps you fetch only the data you need.

WHERE Clause

SELECT * FROM users
WHERE age > 20;
Enter fullscreen mode Exit fullscreen mode

Multiple Conditions

SELECT * FROM users
WHERE age > 20 AND name = 'Ali';
Enter fullscreen mode Exit fullscreen mode

💡 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;
Enter fullscreen mode Exit fullscreen mode

Descending order:

SELECT * FROM users
ORDER BY age DESC;
Enter fullscreen mode Exit fullscreen mode

Limiting Results

To control the number of results returned, use LIMIT. This is especially useful for pagination:

SELECT * FROM users
LIMIT 5;
Enter fullscreen mode Exit fullscreen mode

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)