SQL & Relational Database Mastery · Chapter 1 · ~15 min · Beginner
Your First Database Table & Queries
Learn how databases actually store information, how to create your first table, add rows, and ask questions using SELECT. Zero prior SQL experience needed.
- Understand tables, columns, rows, and primary keys
- Write CREATE TABLE to define structured schemas
- Insert new records safely with INSERT INTO
- Query and inspect data using SELECT and WHERE
1A Table is a Structured Spreadsheet with Strict Rules
Think of a relational database as a collection of supercharged spreadsheets. Just like an Excel sheet, a table has columns (vertical attributes, like name and email) and rows (horizontal records, where each row is one single person or product).
However, unlike spreadsheets where anyone can accidentally type text into a date cell, databases enforce strict data types:
Whole Numbers
Used for counts, quantities, and IDs: 42, 1001.
Strings of Words
Names, emails, descriptions: 'Ada Lovelace'. Always wrapped in single quotes!
True/False & Time
Flags like is_active = TRUE or timestamps like '2026-01-01'.
Every good table also has a Primary Key. A Primary Key is a unique identifier (like an ID number or passport number) that guarantees no two rows ever get confused with each other.
What is the main purpose of a Primary Key in a database table?
2Creating Your First Table (CREATE TABLE)
Before you can store customer accounts or order records, you must tell the database what columns exist and what type of data belongs in each. In SQL (Structured Query Language), we do this with CREATE TABLE:
CREATE TABLE users (
id INT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
role TEXT
);• id INT PRIMARY KEY: An integer that serves as the unique ID for each user.
• name TEXT NOT NULL: The person's name. NOT NULL means this field can never be left blank!
• email TEXT UNIQUE: The email address. UNIQUE means the database will reject any duplicate sign-ups with the same email.
What happens if an application tries to insert a user without a name into a column marked 'NOT NULL'?
3Adding Data to Your Table (INSERT INTO)
Now that our empty table exists, we add records with the INSERT INTO command:
INSERT INTO users (id, name, email, role)
VALUES (1, 'Ada Lovelace', 'ada@example.com', 'Admin');Notice how the columns inside the first parentheses (id, name, email, role) match the values inside the second parentheses (1, 'Ada Lovelace', ...) in exact position order!
Which of the following is valid SQL to insert a user named Bob?
4Asking Questions with SELECT
Reading data is what developers and data analysts do 90% of the day. In SQL, we use SELECT to fetch rows.
1. Fetching Everything with Asterisk (*)
The asterisk * means "give me every single column in the table":
SELECT * FROM users;2. Picking Specific Columns
In production apps with millions of users, fetching every column wastes memory and network speed. Instead, ask for only the columns you need:
SELECT name, role FROM users;3. Filtering Rows with WHERE
To find a specific user or group, attach a WHERE condition:
SELECT * FROM users WHERE role = 'Developer';What does the asterisk (*) represent in 'SELECT * FROM users'?
5Hands-On: Test Your Queries Live
Use the interactive runner below to see a real database in action. Click each query step to create the table, insert rows, and run filtering queries!
Live SQL Query & Table Playground
Step-by-step query actions:
Current SQL Query:
-- Click a query button below to execute it against your live databaseLive Database Table (users):
No table in database yet.
Click "1. CREATE TABLE users" above to create the table structure.
Database ready. In-memory engine waiting for instructions...
6Chapter Knowledge Challenge
Test your understanding of tables, schemas, constraints, and query filtering before moving to Chapter 2:
You have an 'employees' table with columns (id, name, department, salary). Which query returns only the names and salaries of employees in 'Engineering'?
What happens if you run 'INSERT INTO users (id, name) VALUES (1, 'Bob');' when a user with id = 1 already exists?
7Recap
- A database table is composed of columns (typed attributes) and rows (individual records).
- Every row should have a Primary Key (
id INT PRIMARY KEY) guaranteeing unique identity. - Use
CREATE TABLEto specify columns and constraints likeNOT NULLandUNIQUE. - Add rows using
INSERT INTO ... VALUES (...)with string literals enclosed in single quotes. - Read rows using
SELECT columns FROM table WHERE condition;.
Ready for Chapter 2? Mark this chapter as complete to proceed: