The animation could not start (the drawing library did not load). The notes below still cover the whole lesson.
Database, DBMS, tables, SQL vs NoSQL and the 5 SQL command families, told as RegularMart's story.
The animation could not start (the drawing library did not load). The notes below still cover the whole lesson.
Aim: By the end of this page you can explain what a database, a DBMS and an RDBMS are, read any table by its rows, columns and keys, and name the five families of SQL commands with an example of each.
Description: Every app you use, from banking to food delivery, keeps its data in a database, and SQL is the language we use to ask that database questions. This page tells RegularMart’s story: from a paper notebook and messy Excel copies to one safe database. You learn how a DBMS manages the data, how tables are linked by keys, how SQL and NoSQL differ, and what DDL, DML, DQL, DCL and TCL do, using the RegularMart employees, customers and orders tables, then practise with real-time scenarios.
Before you start: nothing! This is the first page. Just know what an Excel sheet looks like.
| Family | Full form | Commands | Example on our tables | Real result |
|---|---|---|---|---|
| 🏗️ DDL | Data Definition Language | CREATE, ALTER, DROP, TRUNCATE | CREATE TABLE customers (customer_id INT PRIMARY KEY, name VARCHAR(50), city VARCHAR(50)); | empty table, 3 columns, 0 rows |
| 🛋️ DML | Data Manipulation Language | INSERT, UPDATE, DELETE | INSERT INTO customers VALUES (1,'Arjun','Hyderabad'), … (5,'Rahul','Delhi'); | 5 rows inserted |
| 🔍 DQL | Data Query Language | SELECT | SELECT name, salary FROM employees WHERE salary > 50000; | Priya 72000 · Anjali 65000 · Meena 58000 |
| 🔑 DCL | Data Control Language | GRANT, REVOKE | GRANT SELECT ON employees TO suresh; | suresh can read employees, not change it |
| 💾 TCL | Transaction Control Language | COMMIT, ROLLBACK, SAVEPOINT | BEGIN; UPDATE customers SET city = 'Pune'; ROLLBACK; | 5 rows changed, then all restored |
One script creates the 3 tables used on this page, with exactly the rows you saw in the animation.
-- RegularMart sample data for SQL Concepts · 1 · SQL Basics (MySQL 8) CREATE TABLE employees ( emp_id INT PRIMARY KEY, name VARCHAR(50), dept VARCHAR(30), city VARCHAR(30), salary INT, join_date DATE ); INSERT INTO employees VALUES (101,'Ravi','Sales','Hyderabad',45000,'2021-04-12'), (102,'Priya','IT','Bengaluru',72000,'2019-08-01'), (103,'Kiran','HR','Chennai',38000,'2022-01-17'), (104,'Anjali','IT','Hyderabad',65000,'2020-11-23'), (105,'Suresh','Sales','Pune',41000,'2023-06-05'), (106,'Meena','Finance','Chennai',58000,'2018-03-14'); CREATE TABLE customers ( customer_id INT PRIMARY KEY, name VARCHAR(50), city VARCHAR(50) ); INSERT INTO customers VALUES (1,'Arjun','Hyderabad'), (2,'Divya','Chennai'), (3,'Farhan','Mumbai'), (4,'Lakshmi','Bengaluru'), (5,'Rahul','Delhi'); CREATE TABLE orders ( order_id INT PRIMARY KEY, customer_id INT, -- no foreign key on purpose: order 5005 has customer 6 product VARCHAR(30), amount INT ); INSERT INTO orders VALUES (5001,1,'Mobile',15000), (5002,2,'Laptop',55000), (5003,1,'Headphones',2000), (5004,4,'Watch',3500), (5005,6,'Shoes',2500);
Works in MySQL 8 / MySQL Workbench. For an online playground, paste it into any MySQL 8 site.
employees 6 rows
| emp_id | name | dept | city | salary | join_date |
|---|---|---|---|---|---|
| 101 | Ravi | Sales | Hyderabad | 45000 | 2021-04-12 |
| 102 | Priya | IT | Bengaluru | 72000 | 2019-08-01 |
| 103 | Kiran | HR | Chennai | 38000 | 2022-01-17 |
| 104 | Anjali | IT | Hyderabad | 65000 | 2020-11-23 |
| 105 | Suresh | Sales | Pune | 41000 | 2023-06-05 |
| 106 | Meena | Finance | Chennai | 58000 | 2018-03-14 |
customers 5 rows
| customer_id | name | city |
|---|---|---|
| 1 | Arjun | Hyderabad |
| 2 | Divya | Chennai |
| 3 | Farhan | Mumbai |
| 4 | Lakshmi | Bengaluru |
| 5 | Rahul | Delhi |
orders 5 rows
| order_id | customer_id | product | amount |
|---|---|---|---|
| 5001 | 1 | Mobile | 15000 |
| 5002 | 2 | Laptop | 55000 |
| 5003 | 1 | Headphones | 2000 |
| 5004 | 4 | Watch | 3500 |
| 5005 | 6 | Shoes | 2500 |
Q1 📋 Your team lead says: “Show me everything in the employees table.”
💭 Hint: SELECT * means every column; no WHERE means every row.
SELECT * FROM employees;
✅ real output · 6 rows
| emp_id | name | dept | city | salary | join_date |
|---|---|---|---|---|---|
| 101 | Ravi | Sales | Hyderabad | 45000 | 2021-04-12 |
| 102 | Priya | IT | Bengaluru | 72000 | 2019-08-01 |
| 103 | Kiran | HR | Chennai | 38000 | 2022-01-17 |
| 104 | Anjali | IT | Hyderabad | 65000 | 2020-11-23 |
| 105 | Suresh | Sales | Pune | 41000 | 2023-06-05 |
| 106 | Meena | Finance | Chennai | 58000 | 2018-03-14 |
Q2 💼 Payroll only needs two columns: each employee’s name and salary.
💭 Hint: List the column names after SELECT, separated by commas.
SELECT name, salary FROM employees;
✅ real output · 6 rows
| name | salary |
|---|---|
| Ravi | 45000 |
| Priya | 72000 |
| Kiran | 38000 |
| Anjali | 65000 |
| Suresh | 41000 |
| Meena | 58000 |
Q3 💰 HR asks: who earns more than ₹50,000?
💭 Hint: WHERE keeps only the rows that pass the test.
SELECT name, salary FROM employees WHERE salary > 50000;
✅ real output · 3 rows
| name | salary |
|---|---|
| Priya | 72000 |
| Anjali | 65000 |
| Meena | 58000 |
Q4 🧮 The owner asks: how many records (rows) does each of our three tables have?
💭 Hint: COUNT(*) counts rows. One small SELECT per table, side by side.
SELECT (SELECT COUNT(*) FROM employees) AS employees, (SELECT COUNT(*) FROM customers) AS customers, (SELECT COUNT(*) FROM orders) AS orders;
✅ real output · 1 row
| employees | customers | orders |
|---|---|---|
| 6 | 5 | 5 |
Q5 📞 A customer calls and gives her customer ID: 4. Pull up exactly that one customer.
💭 Hint: customer_id is the primary key: unique, so it finds exactly one row.
SELECT * FROM customers WHERE customer_id = 4;
✅ real output · 1 row
| customer_id | name | city |
|---|---|---|
| 4 | Lakshmi | Bengaluru |
Q6 🔗 Arjun (customer 1) asks the support team: “What have I ordered?”
💭 Hint: orders carries customer_id, the key that links it to customers.
SELECT order_id, product, amount FROM orders WHERE customer_id = 1;
✅ real output · 2 rows
| order_id | product | amount |
|---|---|---|
| 5001 | Mobile | 15000 |
| 5003 | Headphones | 2000 |
Q7 🏗️ The catalogue team needs a new table of product categories with three rows, and wants to see it.
💭 Hint: CREATE TABLE builds it (DDL), INSERT fills it (DML), SELECT reads it (DQL).
CREATE TABLE categories ( category_id INT PRIMARY KEY, category_name VARCHAR(30) ); INSERT INTO categories VALUES (1,'Electronics'), (2,'Accessories'), (3,'Fashion'); SELECT * FROM categories;
✅ real output · 3 rows
| category_id | category_name |
|---|---|
| 1 | Electronics |
| 2 | Accessories |
| 3 | Fashion |
Q8 ↩️ Suresh ran an UPDATE without WHERE and every customer moved to Pune! Undo it before anyone sees, then check the cities.
💭 Hint: Inside a transaction, ROLLBACK throws away every change since BEGIN.
BEGIN; UPDATE customers SET city = 'Pune'; -- oops: 5 rows changed ROLLBACK; -- undo all of it SELECT customer_id, name, city FROM customers;
✅ real output · 5 rows
| customer_id | name | city |
|---|---|---|
| 1 | Arjun | Hyderabad |
| 2 | Divya | Chennai |
| 3 | Farhan | Mumbai |
| 4 | Lakshmi | Bengaluru |
| 5 | Rahul | Delhi |
Q9 ⛔ Finance found an order whose customer does not exist. Which order is it? (A FOREIGN KEY would have stopped it.)
💭 Hint: Look for orders whose customer_id is NOT IN the customers table.
SELECT * FROM orders WHERE customer_id NOT IN (SELECT customer_id FROM customers);
✅ real output · 1 row
| order_id | customer_id | product | amount |
|---|---|---|---|
| 5005 | 6 | Shoes | 2500 |
💡 Say this in the interview: “A database stores the data, the DBMS is the software that manages it, and an RDBMS stores it in tables linked by keys.”
CREATE TABLE customers (...); -- DDL INSERT INTO customers VALUES (...); -- DML SELECT name FROM customers; -- DQL GRANT SELECT ON employees TO suresh; -- DCL COMMIT; -- TCL
💡 Say this in the interview: “DDL builds the house, DML moves the furniture, DQL looks around, DCL hands out the keys and TCL saves or undoes.”
SELECT o.order_id, o.customer_id
FROM orders o
WHERE NOT EXISTS (SELECT 1 FROM customers c
WHERE c.customer_id = o.customer_id);✅ real output
| order_id | customer_id |
|---|---|
| 5005 | 6 |
💡 Say this in the interview: “The primary key identifies a row in its own table; the foreign key points to that key from another table and blocks orphan rows.”
💡 Say this in the interview: “SQL is for structured data that must stay correct, NoSQL for flexible or very fast-changing data, and NoSQL means Not Only SQL.”
DELETE FROM orders WHERE order_id = 5005; -- 1 row gone, table stays TRUNCATE TABLE orders; -- all rows gone, empty table stays DROP TABLE orders; -- the table itself is gone
💡 Say this in the interview: “DELETE removes rows and can be rolled back, TRUNCATE empties the table fast, and DROP removes the table itself.”
The owner wants one row for the Monday meeting: number of employees, the monthly salary bill, employees earning above ₹50,000, number of customers, number of orders and total sales.
💭 Hint: One SELECT with small sub-SELECTs side by side: COUNT(*), SUM(...) and a WHERE where needed.
SELECT (SELECT COUNT(*) FROM employees) AS employees, (SELECT SUM(salary) FROM employees) AS salary_bill, (SELECT COUNT(*) FROM employees WHERE salary > 50000) AS high_earners, (SELECT COUNT(*) FROM customers) AS customers, (SELECT COUNT(*) FROM orders) AS orders, (SELECT SUM(amount) FROM orders) AS total_sales;
✅ real output · 1 row
| employees | salary_bill | high_earners | customers | orders | total_sales |
|---|---|---|---|---|---|
| 6 | 319000 | 3 | 5 | 5 | 78000 |