Introduction to SQL
What SQL is, how relational databases store data, and the five command families every statement belongs to.
What is SQL?
SQL (Structured Query Language) is a standard programming language used to create, modify, query, and manage relational databases.
Why SQL?
SQL is used to store data, retrieve data, update data, and perform operations on relational databases—it is the interface between you and the tables your company runs on.
Data Storage Systems
Before writing a query, it helps to know which layer of the data stack you are talking to.
A collection of related data.
Software used to manage a database.
A DBMS that stores data in related tables and enforces relationships with keys and constraints. PostgreSQL—the database behind Queryflo—is an RDBMS.
A large-scale system built to analyze combined data from many sources rather than run the live application. Examples include BigQuery, Redshift, and Snowflake.
SQL Architecture
Every query travels through the same chain, whether it starts in a dashboard, an application, or a terminal.
Types of SQL Commands
Every SQL statement belongs to one of five categories:
CREATE · ALTER · DROP · TRUNCATEINSERT · UPDATE · DELETESELECTGRANT · REVOKECOMMIT · ROLLBACK · SAVEPOINTDatabase, Tables & Data Types
Create a Database
CREATE DATABASE queryflo_learning;Create a Table
Every column needs a name, a data type, and a clear purpose. Queryflo already owns the table below; the DDL is shown to explain its structure and is not run in the practice sandbox.
| Column Name | PostgreSQL Type | Description |
|---|---|---|
| id | integer | Campaign identifier (primary key) |
| name | text | Campaign name |
| channel | text | Acquisition channel |
| spend | numeric | Campaign spend |
| start_date | date | Campaign start date |
| end_date | date | Campaign end date |
| status | text | Current campaign status |
| target_segment | text | Audience segment |
CREATE TABLE marketing.campaigns (
id integer PRIMARY KEY,
name text NOT NULL,
channel text,
spend numeric,
start_date date,
end_date date,
status text,
target_segment text
);A column—or combination of columns—that uniquely identifies each row. Here, id uniquely identifies a campaign.
Common Data Types
integer— whole numbers such as a campaign IDtext— strings such as campaign names and channelsnumeric— exact decimal values such as campaign spenddate— calendar dates such as campaign start and end datesboolean— true/false values, used by tables such asproduct.ab_tests.convertedtimestamptz— a timestamp with timezone, used by tables such asgrowth.users.created_at
Example Table: marketing.campaigns
| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
Categories of SQL Commands
Now go one level deeper into each category, starting with the one that shapes the table itself.
DDLData Definition Language
DDL commands define and change a table’s structure—not its rows.
CREATE— build a new table, database, or objectALTER— change an existing table’s structureDROP— permanently remove a table or databaseTRUNCATE— remove all rows while preserving the table structure
— marketing.campaigns, as defined above —| id | name | channel | spend | start_date | end_date | status | target_segment |
|---|---|---|---|---|---|---|---|
| 1 | Spring Launch | google_ads | 55000.00 | 2024-01-15 | 2024-03-31 | active | smb |
| 2 | Retention Webinar | 45000.00 | 2024-02-10 | 2024-04-15 | active | enterprise | |
| 3 | Finance Retargeting | 50000.00 | 2024-03-12 | 2024-05-31 | active | enterprise | |
| 4 | Enterprise Search | google_ads | 60000.00 | 2024-04-01 | 2024-06-30 | active | enterprise |
Starting table — press Next to run each simulated DDL command
DQLData Query Language
DQL is the command family analysts use most: SELECT. It reads data without changing it. Every WHERE, JOIN, and GROUP BY lesson in this course is a deeper use of DQL.
SELECT name, spend
FROM marketing.campaigns
WHERE channel = 'google_ads';DCLData Control Language
DCL controls who is allowed to access the data.
GRANTgives a user or role permission.REVOKEtakes that permission away.
analyst_role has no access to marketing.campaigns
TCLTransaction Control Language
TCL controls when a write becomes permanent. A change remains pending until you choose its outcome.
COMMITmakes the change permanent.ROLLBACKreturns to the last committed state.SAVEPOINTmarks a partial rollback point.
UPDATE marketing.campaigns SET spend =65000.00Uncommitted — choose an outcome
DMLData Manipulation Language
DML is how data enters and changes inside the structures created by DDL.
INSERTadds new rows.UPDATEmodifies existing rows.DELETEremoves selected rows.
Start reading data with SELECT.
Queryflo practice stays read-only, so the next lessons focus on DQL and business analysis.
Continue to How SQL Queries WorkSign up free to try it on a real business scenario