SQL (Structured Query Language) is a standard programming language used to manage and manipulate relational databases. It allows you to interact with databases by performing operations like retrieving data, inserting new records, updating existing ones, and deleting unnecessary data.
What Can SQL Do?
SQL can be used to:
- 📄Create and manage
database structures
(tables, views, indexes) - 📥Insert new records
into a table - 🔍Query the database for specific information
- ✏️ Update existing data
- 🗑️ Delete records
- 🔐 Set permissions on tables, procedures, and views
Basic SQL Commands (CRUD)
- CREATE – Creates a new table or database
sqlCopyEditCREATETABLE Students ( ID INT, Name VARCHAR(50), Age INT);
- INSERT – Adds new data
sqlCopyEditINSERTINTO Students (ID, Name, Age)VALUES (1, ‘Alice’, 20);
- SELECT – Retrieves data
sqlCopyEditSELECT*FROM Students;
- UPDATE – Modifies existing data
sqlCopyEditUPDATE StudentsSET Age =21WHERE ID =1;
- DELETE – Removes data
sqlCopyEditDELETEFROM StudentsWHERE ID =1;
Types of SQL Statements
- DDL (Data Definition Language)
- Deals with structure: CREATE, ALTER, DROP
- DML (Data Manipulation Language)
- Deals with data: SELECT, INSERT, UPDATE, DELETE
- DCL (Data Control Language)
- Controls access: GRANT, REVOKE
- TCL (Transaction Control Language)
- Manages transactions: COMMIT, ROLLBACK, SAVEPOINT
Where is SQL Used?
- Web applications
- Data analysis
- Backend services
- Enterprise databases (like MySQL, PostgreSQL, Oracle, SQL Server)
Let me know if you want a beginner-friendly practice project or cheat sheet!
Data Definition Language and Data Manipulation Language
Great! Let’s break down Data Definition Language (DDL) and Data Manipulation Language (DML) in a clear and simple way.
Data Definition Language (DDL)
DDL is used to define and manage the structure of database objects like tables, schemas, indexes, etc.
Common DDL Commands:
Command | Description | Example |
CREATE | Creates a new database object (table, view, etc.) | CREATE TABLE Students (ID INT, Name VARCHAR(50)); |
ALTER | Modifies an existing database object | ALTER TABLE Students ADD Age INT; |
DROP | Deletes a database object | DROP TABLE Students; |
TRUNCATE | Deletes all data in a table (but keeps the structure) | TRUNCATE TABLE Students; |
Note: DDL changes are auto-committed. That means changes are permanent once executed.
Data Manipulation Language (DML)
DML is used to manipulate data stored in database tables — i.e., to add, modify, retrieve, or delete the actual records.
Common DML Commands:
Command | Description | Example |
SELECT | Retrieves data from one or more tables | SELECT * FROM Students; |
INSERT | Adds new data into a table | INSERT INTO Students (ID, Name) VALUES (1, ‘Alice’); |
UPDATE | Modifies existing data | UPDATE Students SET Name = ‘Bob’ WHERE ID = 1; |
DELETE | Removes data from a table | DELETE FROM Students WHERE ID = 1; |
Note: DML changes can be rolled back if not committed, which is useful for error recovery.
Quick Summary:
Feature | DDL | DML |
Purpose | Defines structure | Manages data |
Auto Commit | Yes | No (can be rolled back) |
Examples | CREATE, ALTER, DROP | SELECT, INSERT, UPDATE, DELETE |
Want a small SQL exercise using both DDL and DML to try out?
- Data type (char(n), varchar(n), int, float, date), constraints (not null, unique, primary key), create database, use database, show databases, drop database, show tables, create table, describe table, alter table Awesome! You’re diving into thefundamentals of SQL, so here’s a quick and beginner-friendly
breakdown of data types, constraints, and some
important SQL commands related to databases and tables.
1. SQL Data Types
Data Type | Description | Example |
CHAR(n) | Fixed-length string, always n characters | CHAR(5) stores “Bob “ |
VARCHAR(n) | Variable-length string, up to n characters | VARCHAR(20) stores “Alice” |
INT | Integer (whole number) | INT → 10, -100 |
FLOAT | Decimal (floating point number) | FLOAT → 3.14, 99.99 |
DATE | Stores a date in YYYY-MM-DD format | DATE → ‘2025-04-21’ |
2. SQL Constraints
Constraints are rules applied to table columns to ensure data integrity.
Constraint | Description | Example Use |
NOT NULL | Value must not be NULL | Name VARCHAR(50) NOT NULL |
UNIQUE | All values in the column must be different | Email VARCHAR(100) UNIQUE |
PRIMARY KEY | Uniquely identifies each row (NOT NULL + UNIQUE) | ID INT PRIMARY KEY |
3. SQL Database & Table Commands
Database Commands
sqlCopyEditCREATE DATABASE School; — Create a new databaseUSE School; — Select the database to useSHOW DATABASES; — List all databasesDROP DATABASE School; — Delete the database
Table Commands
sqlCopyEdit– Create a tableCREATETABLE Students ( ID INTPRIMARY KEY, Name VARCHAR(50)
NOTNULL, Email VARCHAR(100)
UNIQUE, Age INT,EnrollmentDateDATE); — Show all tables in current databaseSHOW TABLES; — Describe table structureDESCRIBE Students; — Add a new column to a tableALTERTABLE Students ADD GPA FLOAT; — Modify column data typeALTERTABLE Students MODIFY Name CHAR(50); — Drop (delete) a columnALTERTABLE Students DROPCOLUMN GPA;
Create Database
sqlCopyEditCREATE DATABASE school;
Use Database
sqlCopyEditUSE school;
Show Databases
sqlCopyEditSHOW DATABASES;
Drop Database
sqlCopyEditDROP DATABASE school;
Show Tables
sqlCopyEditSHOW TABLES;
Create Table
sqlCopyEdit
CREATETABLE students ( student_idINT,nameVARCHAR(50),ageINT,gradeVARCHAR(10));
sqlCopyEditDESCRIBE students;
Alter Table Add a column:
sqlCopyEditALTERTABLE students ADD email VARCHAR(100);
Remove a column:
sqlCopyEditALTERTABLE students DROPCOLUMN email;
Add a primary key:
sqlCopyEditALTERTABLE students ADDPRIMARY KEY (student_id);
Drop the primary key:
sqlCopyEditALTERTABLE students DROPPRIMARY KEY;