A Look Inside SQL Commands ๐ : How We Create, Change & Query Data ๐ ๏ธ
In the previous article, we looked at how databases manage users, roles, and the security rules that control who can do what. ๐ Access & Permissions
This article explores how a database is spoken to through SQL. How to make requests, give instructions, ask questions, change information, or even create and remove entire structures. SQL is the language that serves as the bridge between you and the data stored in the database.
SQL is surprisingly simple once you get the hang of its rhythm. It reads almost like English, and every statement follows a predictable structure. In this article, weโll slowly walk through the building blocks of SQL syntax, and then explore the major types of SQL commands โ the ones you use to create things, change things, retrieve things, protect things, and save things.
We wonโt jump into everything at once. Each SQL command category will be broken down into simple, beginner-friendly sections below so you can learn them one at a time without feeling overwhelmed.
SQL Syntax Basics โ The Language Rules Databases Understand ๐ฏ๏ธ
Before diving into the different categories of SQL commands, itโs essential to understand how SQL is written. Think of this section as learning the alphabet and grammar before writing full sentences.
SQL is designed to be readable and predictable, even for beginners. Here are the key basics:
SQL Statements Read Like Simple Instructions ๐ค
SQL uses short, clear keywords such as:
- SELECT
- INSERT
- UPDATE
- DELETE
- CREATE
These keywords tell the database exactly what action to perform.
Example:
SELECT name FROM Employees;
This simply means: โGet the name column from the Employees table.โ
SQL Statements End With a Semicolon โ๏ธ
Most database systems require a semicolon (;) to mark the end of a command. Itโs like a period at the end of a sentence.
SQL Is Not Case-Sensitive (Mostly) ๐งฑ
SQL does not care if you type:
- select
- SELECT
- Select
All three work the same. However, table names and field names may be case-sensitive depending on the database platform (e.g., MySQL on Linux). So itโs best to keep your naming consistent.
Formatting and Spacing Make SQL Easy to Read ๐
Even though SQL doesnโt require clean formatting, humans do. Example:
SELECT FirstName, LastName
FROM Customers
WHERE Country = 'USA';
Avoid:
SELECT FirstName,LastName FROM Customers WHERE Country='USA';
Readable formatting = fewer mistakes.
Use Clear Naming Rules for Tables and Fields ๐ท๏ธ
While different databases have slightly different rules, beginners can follow these simple guidelines:
โ Allowed
- Letters, numbers, underscores
- Example: Employee_ID, OrderDate, Customer123
โ Avoid
- Spaces โ use underscores instead (First_Name)
- Special characters ($ % ^ & *)
Reserved SQL keywords like SELECT, TABLE, ORDER
๐ Best Practice Tip
Use plural table names (Employees, Orders) and singular fields (FirstName, OrderID). This keeps everything consistent and beginner-friendly.
SQL Follows a โClause Structureโ ๐
Most SQL commands follow a pattern:
SELECT columns
FROM table
WHERE condition;
Think of it like:
- What you want
- Where you want it from
- Which specific part you want
This predictable pattern is why SQL is easy to learn.
SQL Ignores Extra Spaces & Line Breaks๐งช
These two are identical:
SELECT * FROM Products;
SELECT *
FROM Products;
DDL โ Data Definition Language๐งฑ
Building, Reshaping & Removing Database Structures
If SQL syntax is the grammar of database communication, then DDL commands are the construction tools. They shape the structure of the database โ the tables, columns, and entire databases themselves.
DDL does not deal with data inside tables. Instead, it defines the blueprint for everything. Letโs break down each DDL command clearly.
CREATE โ Build Something New ๐๏ธ
CREATE is used when introducing something brand-new into the database:
- Creating a database
- Creating a table
- Creating indexes
- Creating views
Example โ Create a table:
CREATE TABLE Employees (
EmployeeID INT,
FirstName VARCHAR(50),
LastName VARCHAR(50),
HireDate DATE
);
Youโve now constructed a storage โboxโ with defined fields.
ALTER โ Modify an Existing Structure ๐ ๏ธ
ALTER changes structural parts without deleting anything. You can:
- Add a new column
- Modify a column
- Rename a column
- Drop a column
- Add/remove constraints (like NOT NULL, UNIQUE)
Example โ Add a new column:
ALTER TABLE Employees
ADD Email VARCHAR(100);
Think of this as remodeling a room inside a house without tearing the house down.
DROP โ Remove Entire Structures โ
DROP does permanent removal. Once dropped, the structure and the data inside it are gone. You can drop:
โ Tables
DROP TABLE Employees;
โ Databases
DROP DATABASE HRSystem;
โ ๏ธ Warning
DROP has no undo button unless transactions or backups are involved.
Quick Summary of DDL โฉ
| Command | Purpose |
|---|---|
| CREATE | Build new databases, tables, and objects |
| ALTER | Modify existing structures |
| DROP | Remove entire tables/databases |
DML โ Data Manipulation Language ๐
Adding, Changing & Removing the Actual Data Inside Tables.
If DDL shapes the structure of a database, then DML works with the actual information stored inside that structure. These commands affect the rows inside your tables โ the real data people use every day.
Letโs walk through the main DML commands in a simple, beginner-friendly way.
INSERT โ Add New Data โจ
This command is used to input new records into a table.
Example โ Add a single row:
INSERT INTO Employees (EmployeeID, FirstName, LastName, HireDate)
VALUES (101, 'John', 'Smith', '2025-01-10');
Think of this as adding a new entry into a spreadsheet.
Importing/Bulk Loading Data ๐ฅ
Databases often need large amounts of data loaded in one go (CSV, Excel, app exports). This is still considered DML because it adds data.
Depending on the database system, importing can use commands such as:
Example โ Bulk insert (SQL Server):
BULK INSERT Employees
FROM 'C:\data\employees.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n');
Example โ Load data (MySQL):
LOAD DATA INFILE '/data/employees.csv'
INTO TABLE Employees
FIELDS TERMINATED BY ',';
This is like uploading an entire sheet of entries instead of typing them one by one.
UPDATE โ Modify Existing Data ๐
Used to change values inside rows that already exist.
Example โ Update an employeeโs email:
UPDATE Employees
SET Email = 'john.smith@company.com'
WHERE EmployeeID = 101;
Always use a WHERE clause to avoid accidentally updating every record.
DELETE โ Remove Data ๐๏ธ
This removes rows from a table.
Example โ Delete one record:
DELETE FROM Employees
WHERE EmployeeID = 101;
โ ๏ธ Danger:
If you forget WHERE, you wipe the entire table:
DELETE FROM Employees; -- Deletes ALL rows!
DML operations manipulate only data โ the structure stays the same.
Quick Summary of DML ๐งฎ
| Command | Purpose |
|---|---|
| INSERT | Add new rows |
| Bulk Import / Load Data | Add large datasets |
| UPDATE | Modify existing rows |
| DELETE | Remove rows |
DQL โ Data Query Language ๐
Retrieving Information Using the SELECT Statement
DQL is the part of SQL most beginners use first โ and most often. While DDL builds structures, DML fills tables with data, and DQL helps retrieve the exact information needed from those tables. The main (and only) command in DQL is: SELECT
Letโs break it down in the simplest possible way
The SELECT Keyword โ Asking the Database a Question โ
The SELECT statement is like asking the database: โGive me this data from that place.โ
Example:
SELECT FirstName, LastName
FROM Employees;
This retrieves the selected columns from the table.
Selecting All Columns (*) โญ
You can request every field in a table with:
SELECT *
FROM Employees;
Great for quick checks, not recommended for production queries.
Using WHERE to Filter Data ๐ฏ
WHERE narrows down which records you want.
Example โ Find all employees in the USA:
SELECT FirstName, LastName
FROM Employees
WHERE Country = 'USA';
This turns a table of thousands into only the rows you care about.
Sorting Results with ORDER BY ๐
Used to sort data in ascending (ASC) or descending (DESC) order.
Example โ Sort employees by hire date:
SELECT FirstName, LastName, HireDate
FROM Employees
ORDER BY HireDate DESC;
Limiting Results with LIMIT (or TOP) โ๏ธ
Useful when you want only the first few rows.
MySQL / PostgreSQL:
SELECT * FROM Employees
LIMIT 5;
SQL Server:
SELECT TOP 5 * FROM Employees;
Basic DQL Pattern ๐
Every DQL query follows a predictable pattern:
SELECT columns
FROM table
WHERE condition
ORDER BY sorting;
This makes SQL feel almost like reading a simple sentence.
Quick Summary ๐งฎ
| Feature | Purpose |
|---|---|
| SELECT | Retrieve data |
* | Get all columns |
| WHERE | Filter results |
| ORDER BY | Sort results |
| LIMIT/TOP | Restrict number of rows |
DCL โ Data Control Language ๐
Managing Permissions & Controlling Who Can Do What
While DQL helps retrieve data and DML changes it, DCL is all about safety and security.These commands determine who can access the database, what theyโre allowed to do, and what theyโre blocked from doing.
DCL works hand in hand with the roles and permissions covered in earlier article, but here we focus on the actual SQL commands used to manage those permissions. There are two main DCL commands:
GRANT โ Give Permissions to Users or Roles ๐
GRANT allows someone to perform certain actions on a database object (like a table, view, or database).
Example โ Allow a user to read data:
GRANT SELECT ON Employees TO UserA;
Example โ Allow a user to insert and update:
GRANT INSERT, UPDATE ON Employees TO UserA;
This is like giving someone the keys to specific rooms inside a building.
REVOKE โ Remove Permissions ๐
REVOKE takes away permissions previously granted.
Example โ Remove the ability to update
REVOKE UPDATE ON Employees FROM UserA;
Example โ Remove all permissions:
REVOKE ALL PRIVILEGES ON Employees FROM UserA;
This is like collecting keys back from someone who no longer needs access.
How DCL Fits Into the Bigger Picture ๐ง
- DDL builds the database
- DML fills it with data
- DQL retrieves the data
- DCL controls who can do any of the above
- It ensures the right people have the right access โ and no one else.
TCL โ Transaction Control Language ๐
Saving, Undoing, and Protecting Changes in a Database
TCL is all about controlling the safety of your changes. Whenever data is inserted, updated, or deleted, databases allow those actions to be confirmed or cancelled using transactions.
A transaction is like a safety bubble that groups one or more operations. Nothing becomes permanent until you explicitly say so. TCL has two main commands:
COMMIT โ Save All Changes Permanently ๐พ
Once you COMMIT, the database writes all pending changes to disk.
Example:
UPDATE Employees
SET Salary = 90000
WHERE EmployeeID = 103;
COMMIT;
This is like pressing Save in a document โ everything becomes final.
ROLLBACK โ Undo Changes Since the Last Commit โฉ๏ธ
If something goes wrong, ROLLBACK undoes all uncommitted changes.
Example โ You accidentally updated too many rows:
UPDATE Employees
SET Salary = 90000;
ROLLBACK;
All changes inside the current transaction are erased, returning data to the previous state.
Why Transactions Are Important ๐ง
Transactions prevent:
- Accidental mass updates
- Partial changes
- Incomplete data (e.g., money deducted from one account but not added to another)
- Corruption during failures (power loss, crashes, network issues)
They guarantee ACID properties:
- Atomicity โ all-or-nothing
- Consistency โ data stays valid
- Isolation โ one transaction doesnโt interfere with another
- Durability โ once saved, it stays saved
Quick Summary of TCL ๐งฎ
| Command | Purpose |
|---|---|
| COMMIT | Save changes permanently |
| ROLLBACK | Undo changes |
| Transactions | Group operations safely |
Wrapping Up ๐งญ
SQL may look technical at first, but once you understand its structure, it becomes one of the most predictable and readable languages in technology.
In this article, we explored how SQL statements are written and how different command categories each play a specific role:
- DDL builds the structure
- DML fills it with data
- DQL retrieves what you need
- DCL keeps everything secure
- TCL protects your changes
Together, these command types form the foundation of how every relational database works.
Now that this article has covered how SQL statements work and the different command types, the next article will focus on putting those skills into action. Weโll look at how data is modified, extracted for reporting, and safely backed upโall practical tasks that build directly on the SQL fundamentals learned here. ๐From Queries to Backups
This article is part of the Database Fundamentals Series โ where we break down what databases are, how they store information, and how data is organized, accessed, and used behind the scenes. ๐ โ Database Fundamentals: Foundation of Data Storage