PM & AI Chronicles

From Product Thinking to Prompt Engineering โ€“ One Tool at a Time

Working With Databases ๐Ÿ›ข๏ธ: Understanding SQL Syntax & Command Types ๐Ÿ“œ๐Ÿ“Š

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.

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 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.โ€

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 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.

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.

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.

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.

These two are identical:

SELECT * FROM Products;
SELECT *
FROM Products;

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 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 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 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.

CommandPurpose
CREATEBuild new databases, tables, and objects
ALTERModify existing structures
DROPRemove entire tables/databases

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.

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.

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.

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.

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.

CommandPurpose
INSERTAdd new rows
Bulk Import / Load DataAdd large datasets
UPDATEModify existing rows
DELETERemove rows

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 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.

You can request every field in a table with:

SELECT * 
FROM Employees;

Great for quick checks, not recommended for production queries.

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.

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;

Useful when you want only the first few rows.

MySQL / PostgreSQL:

SELECT * FROM Employees
LIMIT 5; 

SQL Server:

SELECT TOP 5 * FROM Employees; 

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.

FeaturePurpose
SELECTRetrieve data
*Get all columns
WHEREFilter results
ORDER BYSort results
LIMIT/TOPRestrict number of rows

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 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 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.

  • 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.

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:

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.

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.

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
CommandPurpose
COMMITSave changes permanently
ROLLBACKUndo changes
TransactionsGroup operations safely

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