Skip to content
TeachTrails
Data EngineeringSQL

What Is SQL? Databases and Tables for Beginners

What SQL is and why every data job needs it: databases, tables, rows, keys and the five kinds of SQL commands, explained simply with one shop's real database.

Sukanya Saha, Technology Consultant

8 min read

Beginner friendly

Meet ShopKart

ShopKart is a small online shop. It sells headphones, smartwatches and phone accessories to customers across India. Every sign-up, order and product lives in a database. Throughout this course, you'll answer real questions about ShopKart with SQL: “Who are our best customers?”, “Which products never sell?”, “How did revenue change from April to May?”.

Tables, rows and columns

A table is like a spreadsheet with strict rules. Each row is one thing (one customer). Each column is one fact about it (name, city), and every value in a column has the same type (text, number, date).

ShopKart · PostgreSQL
SELECT * FROM customers;
Result8 rows
customer_idnamecitysignup_date
1AshaKolkata2026-01-05
2RahulMumbai2026-01-20
3PriyaBengaluru2026-02-03
4ImranDelhi2026-02-14
5MeeraChennai2026-03-01
6ArjunKolkata2026-03-18
7SaraMumbai2026-04-02
8VikramNULL2026-04-10

SELECT * means “all columns”. Notice Vikram's city is NULL: the value is unknown. NULLs cause many beginner bugs; lesson 2 shows how to handle them.

Keys: how tables connect

  • Primary key (PK): a column that uniquely identifies each row, never empty. customer_id in customers.
  • Foreign key (FK): a column that points to a primary key in another table. orders.customer_id says which customer placed the order. The database refuses an order for a customer that doesn't exist.
📊 The ShopKart database

Who buys

customers

PK customer_id

  • name
  • city
  • signup_date

logins

FK customer_id

  • login_date

What they buy

orders

PK order_id · FK customer_id

  • order_date
  • status

order_items

PK (order_id, product_id)

  • quantity
  • unit_price

products

PK product_id

  • name
  • category
  • price

Who works here

employees

PK emp_id · FK manager_id → emp_id

  • name
  • department
  • salary

One customer has many orders (one-to-many). One order has many items, and one product appears in many orders, so order_items sits between them (many-to-many). This is how almost every real database is shaped.

How ShopKart was created

This is the exact script we used. Don't worry about every word yet; lesson 7 explains CREATE TABLE and constraints.

shopkart-setup.sql
-- ShopKart: a small online shop's database
CREATE TABLE customers (
  customer_id  INT PRIMARY KEY,
  name         TEXT NOT NULL,
  city         TEXT,                 -- can be unknown (NULL)
  signup_date  DATE NOT NULL
);

CREATE TABLE products (
  product_id  INT PRIMARY KEY,
  name        TEXT NOT NULL,
  category    TEXT NOT NULL,
  price       NUMERIC(10, 2) NOT NULL CHECK (price > 0)
);

CREATE TABLE orders (
  order_id     INT PRIMARY KEY,
  customer_id  INT NOT NULL REFERENCES customers (customer_id),
  order_date   DATE NOT NULL,
  status       TEXT NOT NULL          -- PLACED, DELIVERED, CANCELLED, RETURNED
);

CREATE TABLE order_items (
  order_id    INT REFERENCES orders (order_id),
  product_id  INT REFERENCES products (product_id),
  quantity    INT NOT NULL CHECK (quantity > 0),
  unit_price  NUMERIC(10, 2) NOT NULL,
  PRIMARY KEY (order_id, product_id)
);

CREATE TABLE employees (
  emp_id      INT PRIMARY KEY,
  name        TEXT NOT NULL,
  manager_id  INT REFERENCES employees (emp_id),
  department  TEXT NOT NULL,
  salary      INT NOT NULL
);

CREATE TABLE logins (
  customer_id  INT REFERENCES customers (customer_id),
  login_date   DATE NOT NULL
);
Terminal
# create a database and load ShopKart (after installing PostgreSQL)
$createdb shopkart
$psql -d shopkart -f shopkart-setup.sql
# open an interactive SQL prompt
$psql -d shopkart

Your first real query

ShopKart · PostgreSQL
SELECT name, city
FROM customers;
Result8 rows
namecity
AshaKolkata
RahulMumbai
PriyaBengaluru
ImranDelhi
MeeraChennai
ArjunKolkata
SaraMumbai
VikramNULL

SELECT lists the columns you want, FROM names the table. SQL keywords aren't case-sensitive (select works too), but writing them in capitals makes queries easier to read. End each statement with ;.

The five kinds of SQL commands

GroupStands forCommandsDoes
DQLData Query LanguageSELECTReads data
DMLData Manipulation LanguageINSERT, UPDATE, DELETE, MERGEChanges rows
DDLData Definition LanguageCREATE, ALTER, DROP, TRUNCATEChanges structure
DCLData Control LanguageGRANT, REVOKEPermissions
TCLTransaction Control LanguageBEGIN, COMMIT, ROLLBACKGroups changes safely

SQL everywhere

WhereExampleSame SQL?
App databases (OLTP)PostgreSQL, MySQL, SQL Server, OracleYes, small dialect differences
Cloud warehousesSnowflake, BigQuery, RedshiftYes, plus extras like QUALIFY
LakehouseDatabricks SQL, Spark SQLYes (see our Databricks course)
Transformation toolsdbtSQL files turned into pipelines

🎯 Interview questions on this topic

Try to answer out loud first, then tap to check.

Q1What is the difference between a primary key and a foreign key?Fresher
A primary key uniquely identifies each row of its own table and can't be NULL (one per table, possibly multi-column). A foreign key is a column whose values must exist in another table's primary (or unique) key, enforcing the relationship (referential integrity).
Q2DELETE vs TRUNCATE vs DROP?Fresher
DELETE removes chosen rows (WHERE), is logged row by row and can be rolled back. TRUNCATE removes all rows quickly, resets storage, and can't filter. DROP removes the whole table, structure included.
Q3What is a relational database?Fresher
A database that stores data in tables with defined columns and types, and links tables through keys. You query it with SQL, and it enforces rules (constraints) and transactions.
Q4What is a composite key? Give an example.1–3 yrs
A primary key made of several columns that together are unique. order_items uses (order_id, product_id): one order can contain many products, but each product only once per order.
Q5Can a foreign key be NULL?1–3 yrs
Yes, unless declared NOT NULL. A NULL foreign key means 'no related row', e.g. the CEO's manager_id is NULL in employees.

Key takeaways

  • SQL is how you ask a database questions; it's the core skill of every data job.
  • Tables connect through primary and foreign keys.
  • ShopKart has customers, orders, order_items, products, employees and logins.
  • Next: filtering rows with WHERE, and the NULL trap.

Frequently asked questions

What is SQL in simple words?

SQL (Structured Query Language) is the language you use to ask a database questions and change its data. You describe what you want, such as 'customers in Kolkata', and the database works out how to find it.

Is SQL a programming language?

It's a declarative query language: you say what result you want, not the step-by-step how. It isn't used to build apps on its own, but it's the most important language for working with data.

Which SQL should I learn: MySQL, PostgreSQL or SQL Server?

Learn standard SQL with any of them; 90% is identical. This course uses PostgreSQL because it's free, popular and close to the standard. Databricks SQL and Snowflake work almost the same way.

How long does it take to learn SQL?

The basics (SELECT, WHERE, GROUP BY, JOIN) take a few days of practice. Interview-level SQL (window functions, CTEs, modelling) takes a few weeks of solving problems.

Why do data engineers need SQL?

Almost every data tool speaks SQL: warehouses, Databricks, Snowflake, dbt, BI tools. Data engineers use it to explore data, build pipeline transformations, test data quality and serve analysts.

🧠 Quick check: what is SQL?

  1. 1. What uniquely identifies each row in a table?

  2. 2. orders.customer_id points to customers.customer_id. It's a...

  3. 3. SELECT belongs to which group of commands?

  4. 4. CREATE TABLE is...

SQL for Data Engineers

Part 1 of 12: What Is SQL