π Speak the language of databases
SQL (Structured Query Language) is the standard language used to communicate with relational databases like SQL Server, MySQL, and SQLite. Whether you’re building desktop apps, websites, or data-driven tools, SQL is the bridge between your code and the stored data.
In this guide, youβll learn the basic syntax, command structure, and how to perform the most common SQL operations like querying, inserting, and filtering data.
π What is SQL Used For?
- π Creating and modifying databases and tables
- π₯ Inserting new records
- π Querying and filtering data
- βοΈ Updating existing records
- β Deleting records
- π Managing access and structure
π§± SQL Syntax Basics
SQL commands follow a clear, readable structure, and are not case-sensitive (but best practice is to capitalise keywords):
SELECT column1, column2
FROM TableName
WHERE condition;
π‘ Statements always end with a semicolon (;).
πΉ SQL Statement Categories
| Category | Purpose | Examples |
|---|---|---|
| DDL | Data Definition Language | CREATE, ALTER, DROP |
| DML | Data Manipulation Language | SELECT, INSERT, UPDATE, DELETE |
| DCL | Data Control Language | GRANT, REVOKE |
| TCL | Transaction Control Language | COMMIT, ROLLBACK |
For beginners, DML and DDL are your main focus.
π Creating Tables
CREATE TABLE Customers (
CustomerID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100),
Age INT
);
π‘ Each column has a name and a data type (like INT, VARCHAR, DATE, etc.)
π₯ Inserting Data
INSERT INTO Customers (CustomerID, FirstName, LastName, Email, Age)
VALUES (1, 'Alice', 'Smith', 'alice@example.com', 28);
You can add one row at a time or multiple:
INSERT INTO Customers VALUES
(2, 'Bob', 'Jones', 'bob@example.com', 35),
(3, 'Clara', 'White', 'clara@example.com', 22);
π Reading Data β SELECT
The SELECT statement is the most commonly used command in SQL.
SELECT * FROM Customers;
| Symbol | Meaning |
|---|---|
* | Select all columns |
WHERE | Add a filter condition |
ORDER BY | Sort the results |
LIMIT | Limit the number of rows |
π― Examples:
SELECT FirstName, LastName FROM Customers
WHERE Age > 30
ORDER BY LastName ASC;
SELECT COUNT(*) FROM Customers WHERE Email LIKE '%@gmail.com';
βοΈ Updating Data
UPDATE Customers
SET Age = 29
WHERE CustomerID = 1;
π‘ Always use WHERE to avoid updating every row!
β Deleting Data
DELETE FROM Customers
WHERE CustomerID = 3;
Without a WHERE clause, it would delete every row.
π Common Data Types
| Type | Description |
|---|---|
INT | Whole numbers |
FLOAT | Decimal numbers |
VARCHAR(n) | Text up to n characters |
DATE | Date values (YYYY-MM-DD) |
BOOLEAN | True/False |
π§ͺ Quick SQL Challenge
- Create a table called
Bookswith:BookID,Title,Author,Year,InStock(boolean)
- Insert three books into the table
- Select all books published after 2010
- Update the stock of one book to false
- Delete a book by
BookID
π§ Best Practices
β
Use capital letters for SQL keywords (SELECT, FROM, etc.)
β
Always include WHERE when updating or deleting
β
Use meaningful column names
β
Keep column types appropriate to the data
β
Format your queries for readability
π Summary Table
| Command | Purpose |
|---|---|
SELECT | Retrieve data from a table |
INSERT | Add new data to a table |
UPDATE | Modify existing data |
DELETE | Remove data from a table |
CREATE TABLE | Define a new table structure |
π Whatβs Next?
SQL is a core skill in full-stack development β learning it well sets you up for APIs, web apps, reports, and more. Join our SQL Essentials course to further your understanding.