Skip to lesson

Load the practice databases

The practice database is one SQL script that builds ten small tables with realistic Indian data: an online store called ShopLite, a company and a college. Every query in this section runs on it, so load it once now and you can try every example yourself.

ShopLite: the online store this course builds

ShopLite is the running example of this course: a small online store whose data sits in five tables (categories, customers, products, orders and order_items). Every query in this section runs on it.

ShopLite is a small online store selling phones, laptops, books and clothes to customers across India.

Example 1 · ShopLite products

SQL
SELECT * FROM products ORDER BY product_id;
Output
+------------+------------------------+-------------+----------+-------+
| product_id | name                   | category_id | price    | stock |
+------------+------------------------+-------------+----------+-------+
|          1 | Redmi Note 13          |           2 | 17999.00 |    40 |
|          2 | Samsung Galaxy M35     |           2 | 19999.00 |    25 |
|          3 | iPhone 15              |           2 | 69900.00 |     8 |
|          4 | Lenovo IdeaPad Slim 3  |           3 | 42990.00 |    12 |
|          5 | HP Victus Gaming       |           3 | 64990.00 |     5 |
|          6 | boAt Airdopes 141      |           4 |  1299.00 |   120 |
|          7 | Mi Power Bank 20000mAh |           4 |  2199.00 |    60 |
|          8 | Logitech M331 Mouse    |           4 |   849.00 |     0 |
|          9 | Clean Code             |           5 |   499.00 |    30 |
|         10 | Head First Java        |           5 |   650.00 |    22 |
|         11 | Atomic Habits          |           5 |   399.00 |    50 |
|         12 | Cotton Kurta           |           6 |   899.00 |    35 |
|         13 | Running Shoes          |           6 |  2499.00 |    18 |
|         14 | Rubik's Cube           |           7 |   299.00 |    70 |
|         15 | Gift Card Rs. 500      |        NULL |   500.00 |   999 |
+------------+------------------------+-------------+----------+-------+

What this code does

  • category_id links each product to categories: 2 is Mobiles, 3 Laptops, 4 Accessories, 5 Books, 6 Fashion, 7 Toys. The gift card has NULL: it belongs to no category.
  • Prices are DECIMAL(10,2), exact rupees and paise. The Logitech mouse has stock 0, so it is out of stock.

Example 2 · One order and its lines

SQL
SELECT * FROM orders WHERE order_id = 2;SELECT * FROM order_items WHERE order_id = 2 ORDER BY product_id;
Output
+----------+-------------+------------+-----------+
| order_id | customer_id | order_date | status    |
+----------+-------------+------------+-----------+
|        2 |           2 | 2025-01-18 | DELIVERED |
+----------+-------------+------------+-----------+
+----------+------------+----------+------------+
| order_id | product_id | quantity | unit_price |
+----------+------------+----------+------------+
|        2 |          9 |        1 |     499.00 |
|        2 |         10 |        1 |     650.00 |
|        2 |         11 |        2 |     399.00 |
+----------+------------+----------+------------+

What this code does

  • Order 2 was placed by customer 2 (Ravi Kumar) on 18 January 2025 and delivered. Its three lines are three books: one Clean Code, one Head First Java and two Atomic Habits.
  • unit_price is copied into the order line because a product's price can change later; the order must remember what the customer actually paid. The pair (order_id, product_id) is the primary key, so a product appears at most once per order.

This section designs and queries ShopLite's database, and the next sections connect an application to it and grow it into a working store.

Notice how the five tables point at each other with ids, exactly the primary and foreign keys from the Tables, rows and columns lesson.

  • categories: the shop's sections. parent_id points to another category, so Mobiles, Laptops and Accessories sit under Electronics.
  • customers: who buys. Name, a unique email, an optional phone, city and the date they joined.
  • products: what is sold, with its category_id, price (must be above 0) and stock (pieces left).
  • orders and order_items: an order is one checkout by one customer on a date, with a status (PLACED, SHIPPED, DELIVERED or CANCELLED). Its lines are in order_items: which product, how many, and the unit_price paid.

The practice data: store, employees, students and railway tables

The same script also builds two more small worlds, so examples can use whatever fits the idea best. The company has departments and employees; each employee row has a manager_id that points to another employee, which is perfect for hierarchy questions.

Example 3 · The company: departments and employees

SQL
SELECT * FROM departments ORDER BY dept_id;SELECT * FROM employees ORDER BY emp_id;
Output
+---------+-------------+-----------+
| dept_id | dept_name   | location  |
+---------+-------------+-----------+
|       1 | Engineering | Bengaluru |
|       2 | Sales       | Mumbai    |
|       3 | HR          | Pune      |
|       4 | Finance     | Chennai   |
|       5 | Research    | Hyderabad |
+---------+-------------+-----------+
+--------+----------------+---------+------------+-----------+------------+
| emp_id | name           | dept_id | manager_id | salary    | hire_date  |
+--------+----------------+---------+------------+-----------+------------+
|      1 | Vikram Rao     |       1 |       NULL | 250000.00 | 2015-06-01 |
|      2 | Anita Desai    |       1 |          1 | 180000.00 | 2017-03-15 |
|      3 | Karthik Raj    |       1 |          2 | 120000.00 | 2019-07-01 |
|      4 | Pooja Gupta    |       1 |          2 | 120000.00 | 2020-01-10 |
|      5 | Suresh Babu    |       1 |          2 |  95000.00 | 2022-08-22 |
|      6 | Neha Joshi     |       2 |          1 | 150000.00 | 2016-11-30 |
|      7 | Amit Shah      |       2 |          6 |  85000.00 | 2021-04-12 |
|      8 | Farah Ali      |       2 |          6 |  85000.00 | 2023-02-01 |
|      9 | Lakshmi Menon  |       3 |          1 |  90000.00 | 2018-09-09 |
|     10 | Rohan Das      |       3 |          9 |  60000.00 | 2024-05-20 |
|     11 | Gita Reddy     |       4 |          1 | 140000.00 | 2016-02-25 |
|     12 | Manoj Pillai   |       4 |         11 |  75000.00 | 2022-12-05 |
|     13 | Tanvi Kulkarni |    NULL |          1 |  70000.00 | 2025-01-06 |
+--------+----------------+---------+------------+-----------+------------+

What this code does

  • manager_id points to another row of the same table: Anita Desai (2) reports to Vikram Rao (1), and Karthik, Pooja and Suresh report to Anita. Vikram has NULL, as the head of the company.
  • Tanvi Kulkarni has no dept_id yet, and no employee works in Research (5).

Example 4 · The college: students, courses and enrollments

SQL
SELECT * FROM students ORDER BY student_id;SELECT * FROM courses ORDER BY course_id;SELECT * FROM enrollments ORDER BY student_id, course_id;
Output
+------------+-----------------+--------+------+-----------+
| student_id | name            | branch | year | city      |
+------------+-----------------+--------+------+-----------+
|          1 | Aditya Joshi    | CSE    |    2 | Pune      |
|          2 | Bhavana Reddy   | CSE    |    2 | Hyderabad |
|          3 | Chirag Patel    | ECE    |    3 | Ahmedabad |
|          4 | Deepika Nair    | CSE    |    3 | Kochi     |
|          5 | Eshan Gupta     | MECH   |    1 | Delhi     |
|          6 | Fathima Begum   | ECE    |    2 | Chennai   |
|          7 | Gaurav Mishra   | IT     |    4 | Lucknow   |
|          8 | Harini Krishnan | IT     |    1 | Chennai   |
+------------+-----------------+--------+------+-----------+
+-----------+-------+-------------------------+---------+
| course_id | code  | title                   | credits |
+-----------+-------+-------------------------+---------+
|         1 | CS201 | Data Structures         |       4 |
|         2 | CS202 | Database Systems        |       4 |
|         3 | EC301 | Digital Electronics     |       3 |
|         4 | MA101 | Engineering Mathematics |       4 |
|         5 | HS101 | Communication Skills    |       2 |
+-----------+-------+-------------------------+---------+
+------------+-----------+-------+
| student_id | course_id | marks |
+------------+-----------+-------+
|          1 |         1 |    88 |
|          1 |         2 |    92 |
|          1 |         4 |    75 |
|          2 |         1 |    79 |
|          2 |         2 |    85 |
|          3 |         3 |    67 |
|          3 |         4 |    58 |
|          4 |         1 |    95 |
|          4 |         2 |    91 |
|          4 |         5 |    80 |
|          5 |         4 |    45 |
|          5 |         5 |  NULL |
|          6 |         3 |    72 |
|          6 |         4 |    81 |
|          7 |         1 |    64 |
|          7 |         2 |    38 |
+------------+-----------+-------+

What this code does

  • Each enrollment row links one student to one course with the marks scored, so student 1 (Aditya) takes courses 1, 2 and 4 and scored 88, 92 and 75.
  • Eshan (5) has NULL marks in course 5 because it is not graded yet, and Harini (8) has no enrollment rows at all. ORDER BY student_id, course_id sorts by student first, then by course within each student.

The college has students, courses and enrollments, which stores each student's marks in each course.

Some rows are incomplete on purpose, so later lessons have something to find: three customers have no phone, Imran Khan has never ordered, the gift card has no category, the Logitech mouse is out of stock, Vikram Rao has no manager, Tanvi Kulkarni has no department, the Research department has no employees, Harini Krishnan has no enrollments and Eshan Gupta has no marks yet in Communication Skills. Railway tables (trains, stations, bookings) are not in this script. Lessons that use IRCTC-style data, such as the railway reservation project in the Joins module, create their own small tables at the top of each example, as do the hospital, cinema and cricket example.

  • departments: 5 departments with a city each.
  • employees: 13 employees with dept_id, manager_id, salary and hire_date.
  • students and courses: 8 B.Tech students (branch, year, city) and 5 courses with a code and credits.
  • enrollments: who takes which course and the marks (0 to 100, or NULL when not graded yet).

Predict the output

Predict the row

Use the departments table from the lesson. What does this return?

SELECT dept_name
FROM departments
WHERE location = 'Pune';

Run the setup script with SOURCE or Workbench

SOURCE is a mysql client command that runs every statement in a file, one after another. It is how you load the practice database from the file sample-database.sql.

SOURCE file is a mysql client command that runs every statement in a file, one after another.

  1. Connect as root: mysql -u root -p.
  2. Run CREATE DATABASE practice; and then USE practice;.
  3. Run SOURCE followed by the full path of your file (see the paths below).
  4. Run SHOW TABLES; and check that ten tables are listed.

Example 5 · The practice database script (sample-database.sql)

SQL
-- MentorNxt practice database for Section 12 (SQL & Database Design), MySQL 8.0 or later.-- Run it in an empty database: CREATE DATABASE practice; USE practice; SOURCE sample-database.sql;-- Running it again resets every table to this data.-- Three small worlds: the ShopLite online store, a company (employees and departments) and a college-- (students, courses and enrollments). Some rows are missing on purpose (no phone, no manager, a-- customer with no orders) so that NULL, outer joins and anti-joins have something to find.
SET FOREIGN_KEY_CHECKS = 0;DROP TABLE IF EXISTS order_items, orders, products, categories, customers,                     employees, departments, enrollments, courses, students;SET FOREIGN_KEY_CHECKS = 1;
-- ShopLite store -------------------------------------------------------------------------------
CREATE TABLE categories (  category_id INT AUTO_INCREMENT PRIMARY KEY,  name        VARCHAR(50) NOT NULL UNIQUE,  parent_id   INT NULL,  FOREIGN KEY (parent_id) REFERENCES categories (category_id));
CREATE TABLE customers (  customer_id INT AUTO_INCREMENT PRIMARY KEY,  name        VARCHAR(60)  NOT NULL,  email       VARCHAR(100) NOT NULL UNIQUE,  phone       VARCHAR(15)  NULL,  city        VARCHAR(40)  NOT NULL,  joined_on   DATE         NOT NULL);
CREATE TABLE products (  product_id  INT AUTO_INCREMENT PRIMARY KEY,  name        VARCHAR(80)   NOT NULL,  category_id INT           NULL,  price       DECIMAL(10,2) NOT NULL CHECK (price > 0),  stock       INT           NOT NULL DEFAULT 0,  FOREIGN KEY (category_id) REFERENCES categories (category_id));
CREATE TABLE orders (  order_id    INT AUTO_INCREMENT PRIMARY KEY,  customer_id INT  NOT NULL,  order_date  DATE NOT NULL,  status      ENUM('PLACED', 'SHIPPED', 'DELIVERED', 'CANCELLED') NOT NULL DEFAULT 'PLACED',  FOREIGN KEY (customer_id) REFERENCES customers (customer_id));
CREATE TABLE order_items (  order_id   INT           NOT NULL,  product_id INT           NOT NULL,  quantity   INT           NOT NULL CHECK (quantity > 0),  unit_price DECIMAL(10,2) NOT NULL,  PRIMARY KEY (order_id, product_id),  FOREIGN KEY (order_id)   REFERENCES orders (order_id),  FOREIGN KEY (product_id) REFERENCES products (product_id));
INSERT INTO categories (category_id, name, parent_id) VALUES  (1, 'Electronics', NULL),  (2, 'Mobiles', 1),  (3, 'Laptops', 1),  (4, 'Accessories', 1),  (5, 'Books', NULL),  (6, 'Fashion', NULL),  (7, 'Toys', NULL);
INSERT INTO customers (customer_id, name, email, phone, city, joined_on) VALUES  (1,  'Asha Rao',       'asha.rao@example.com',      '9876500001', 'Bengaluru', '2024-11-03'),  (2,  'Ravi Kumar',     'ravi.kumar@example.com',    '9876500002', 'Chennai',   '2024-12-15'),  (3,  'Meena Iyer',     'meena.iyer@example.com',    NULL,         'Chennai',   '2025-01-08'),  (4,  'Kabir Singh',    'kabir.singh@example.com',   '9876500004', 'Delhi',     '2025-01-20'),  (5,  'Priya Sharma',   'priya.sharma@example.com',  '9876500005', 'Pune',      '2025-02-02'),  (6,  'Arjun Mehta',    'arjun.mehta@example.com',   NULL,         'Mumbai',    '2025-02-14'),  (7,  'Divya Nair',     'divya.nair@example.com',    '9876500007', 'Kochi',     '2025-03-01'),  (8,  'Rahul Verma',    'rahul.verma@example.com',   '9876500008', 'Delhi',     '2025-03-11'),  (9,  'Sneha Patil',    'sneha.patil@example.com',   '9876500009', 'Pune',      '2025-04-05'),  (10, 'Imran Khan',     'imran.khan@example.com',    NULL,         'Hyderabad', '2025-04-18');
INSERT INTO products (product_id, name, category_id, price, stock) VALUES  (1,  'Redmi Note 13',            2, 17999.00, 40),  (2,  'Samsung Galaxy M35',       2, 19999.00, 25),  (3,  'iPhone 15',                2, 69900.00, 8),  (4,  'Lenovo IdeaPad Slim 3',    3, 42990.00, 12),  (5,  'HP Victus Gaming',         3, 64990.00, 5),  (6,  'boAt Airdopes 141',        4, 1299.00, 120),  (7,  'Mi Power Bank 20000mAh',   4, 2199.00, 60),  (8,  'Logitech M331 Mouse',      4, 849.00, 0),  (9,  'Clean Code',               5, 499.00, 30),  (10, 'Head First Java',          5, 650.00, 22),  (11, 'Atomic Habits',            5, 399.00, 50),  (12, 'Cotton Kurta',             6, 899.00, 35),  (13, 'Running Shoes',            6, 2499.00, 18),  (14, 'Rubik''s Cube',            7, 299.00, 70),  (15, 'Gift Card Rs. 500',        NULL, 500.00, 999);
INSERT INTO orders (order_id, customer_id, order_date, status) VALUES  (1,  1, '2025-01-05', 'DELIVERED'),  (2,  2, '2025-01-18', 'DELIVERED'),  (3,  1, '2025-02-02', 'DELIVERED'),  (4,  3, '2025-02-10', 'CANCELLED'),  (5,  4, '2025-02-21', 'DELIVERED'),  (6,  5, '2025-03-03', 'DELIVERED'),  (7,  2, '2025-03-15', 'SHIPPED'),  (8,  6, '2025-03-28', 'DELIVERED'),  (9,  7, '2025-04-04', 'DELIVERED'),  (10, 1, '2025-04-12', 'SHIPPED'),  (11, 8, '2025-04-25', 'PLACED'),  (12, 4, '2025-05-02', 'DELIVERED'),  (13, 9, '2025-05-09', 'PLACED'),  (14, 5, '2025-05-20', 'CANCELLED'),  (15, 2, '2025-06-01', 'PLACED');
INSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES  (1,  1, 1, 17999.00), (1,  6, 2, 1299.00),  (2,  9, 1, 499.00),   (2, 10, 1, 650.00),  (2, 11, 2, 399.00),  (3,  4, 1, 42990.00), (3,  8, 1, 849.00),  (4,  3, 1, 69900.00),  (5,  2, 1, 19999.00), (5,  7, 1, 2199.00),  (6, 12, 3, 899.00),   (6, 13, 1, 2499.00),  (7,  6, 1, 1299.00),  (7, 14, 2, 299.00),  (8,  5, 1, 64990.00),  (9, 11, 1, 399.00),   (9,  9, 1, 499.00),  (10, 7, 2, 2199.00),  (11, 1, 1, 17999.00), (11, 6, 1, 1299.00),  (12, 13, 2, 2499.00),  (13, 10, 2, 650.00),  (14, 4, 1, 42990.00),  (15, 6, 3, 1299.00),  (15, 15, 2, 500.00);
-- Company ----------------------------------------------------------------------------------------
CREATE TABLE departments (  dept_id   INT AUTO_INCREMENT PRIMARY KEY,  dept_name VARCHAR(40) NOT NULL UNIQUE,  location  VARCHAR(40) NOT NULL);
CREATE TABLE employees (  emp_id     INT AUTO_INCREMENT PRIMARY KEY,  name       VARCHAR(60)   NOT NULL,  dept_id    INT           NULL,  manager_id INT           NULL,  salary     DECIMAL(10,2) NOT NULL,  hire_date  DATE          NOT NULL,  FOREIGN KEY (dept_id)    REFERENCES departments (dept_id),  FOREIGN KEY (manager_id) REFERENCES employees (emp_id));
INSERT INTO departments (dept_id, dept_name, location) VALUES  (1, 'Engineering', 'Bengaluru'),  (2, 'Sales',       'Mumbai'),  (3, 'HR',          'Pune'),  (4, 'Finance',     'Chennai'),  (5, 'Research',    'Hyderabad');
INSERT INTO employees (emp_id, name, dept_id, manager_id, salary, hire_date) VALUES  (1,  'Vikram Rao',    1, NULL, 250000.00, '2015-06-01'),  (2,  'Anita Desai',   1, 1,    180000.00, '2017-03-15'),  (3,  'Karthik Raj',   1, 2,    120000.00, '2019-07-01'),  (4,  'Pooja Gupta',   1, 2,    120000.00, '2020-01-10'),  (5,  'Suresh Babu',   1, 2,     95000.00, '2022-08-22'),  (6,  'Neha Joshi',    2, 1,    150000.00, '2016-11-30'),  (7,  'Amit Shah',     2, 6,     85000.00, '2021-04-12'),  (8,  'Farah Ali',     2, 6,     85000.00, '2023-02-01'),  (9,  'Lakshmi Menon', 3, 1,     90000.00, '2018-09-09'),  (10, 'Rohan Das',     3, 9,     60000.00, '2024-05-20'),  (11, 'Gita Reddy',    4, 1,    140000.00, '2016-02-25'),  (12, 'Manoj Pillai',  4, 11,    75000.00, '2022-12-05'),  (13, 'Tanvi Kulkarni', NULL, 1, 70000.00, '2025-01-06');
-- College ----------------------------------------------------------------------------------------
CREATE TABLE students (  student_id INT AUTO_INCREMENT PRIMARY KEY,  name       VARCHAR(60) NOT NULL,  branch     VARCHAR(10) NOT NULL,  year       TINYINT     NOT NULL CHECK (year BETWEEN 1 AND 4),  city       VARCHAR(40) NOT NULL);
CREATE TABLE courses (  course_id INT AUTO_INCREMENT PRIMARY KEY,  code      CHAR(6)     NOT NULL UNIQUE,  title     VARCHAR(60) NOT NULL,  credits   TINYINT     NOT NULL);
CREATE TABLE enrollments (  student_id INT     NOT NULL,  course_id  INT     NOT NULL,  marks      TINYINT NULL CHECK (marks BETWEEN 0 AND 100),  PRIMARY KEY (student_id, course_id),  FOREIGN KEY (student_id) REFERENCES students (student_id),  FOREIGN KEY (course_id)  REFERENCES courses (course_id));
INSERT INTO students (student_id, name, branch, year, city) VALUES  (1, 'Aditya Joshi',   'CSE',  2, 'Pune'),  (2, 'Bhavana Reddy',  'CSE',  2, 'Hyderabad'),  (3, 'Chirag Patel',   'ECE',  3, 'Ahmedabad'),  (4, 'Deepika Nair',   'CSE',  3, 'Kochi'),  (5, 'Eshan Gupta',    'MECH', 1, 'Delhi'),  (6, 'Fathima Begum',  'ECE',  2, 'Chennai'),  (7, 'Gaurav Mishra',  'IT',   4, 'Lucknow'),  (8, 'Harini Krishnan','IT',   1, 'Chennai');
INSERT INTO courses (course_id, code, title, credits) VALUES  (1, 'CS201', 'Data Structures', 4),  (2, 'CS202', 'Database Systems', 4),  (3, 'EC301', 'Digital Electronics', 3),  (4, 'MA101', 'Engineering Mathematics', 4),  (5, 'HS101', 'Communication Skills', 2);
INSERT INTO enrollments (student_id, course_id, marks) VALUES  (1, 1, 88), (1, 2, 92), (1, 4, 75),  (2, 1, 79), (2, 2, 85),  (3, 3, 67), (3, 4, 58),  (4, 1, 95), (4, 2, 91), (4, 5, 80),  (5, 4, 45), (5, 5, NULL),  (6, 3, 72), (6, 4, 81),  (7, 2, 38), (7, 1, 64);
Sample output
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 10 warnings (0.02 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 7 rows affected (0.01 sec)
Records: 7  Duplicates: 0  Warnings: 0

Query OK, 10 rows affected (0.01 sec)
Records: 10  Duplicates: 0  Warnings: 0

Query OK, 15 rows affected (0.01 sec)
Records: 15  Duplicates: 0  Warnings: 0

Query OK, 15 rows affected (0.01 sec)
Records: 15  Duplicates: 0  Warnings: 0

Query OK, 25 rows affected (0.01 sec)
Records: 25  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

Query OK, 13 rows affected (0.01 sec)
Records: 13  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 0 rows affected (0.03 sec)

Query OK, 8 rows affected (0.01 sec)
Records: 8  Duplicates: 0  Warnings: 0

Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

Query OK, 16 rows affected (0.01 sec)
Records: 16  Duplicates: 0  Warnings: 0

What this code does

  • This is the complete script: save it as sample-database.sql and run it with SOURCE inside an empty practice database. SET FOREIGN_KEY_CHECKS = 0 lets the first DROP remove tables in any order; the second SET turns the check back on.
  • Each statement prints a Query OK line, and each multi-row INSERT also prints Records:. The 10 warnings are notes that the tables did not exist yet, which is expected on the first run. Timings differ on your computer.
  • The CREATE TABLE lines use keys and rules (PRIMARY KEY, UNIQUE, NOT NULL, CHECK, FOREIGN KEY) that the next two modules explain one by one. For now, read the column names and the INSERT rows.

Example 6 · Check the tables after loading

SQL
SHOW TABLES;
Sample output
+--------------------+
| Tables_in_practice |
+--------------------+
| categories         |
| courses            |
| customers          |
| departments        |
| employees          |
| enrollments        |
| order_items        |
| orders             |
| products           |
| students           |
+--------------------+

What this code does

  • Ten tables, listed in alphabetical order. The header holds the database name, so it reads Tables_in_practice only if you named the database practice.
  • If a table is missing, scroll up for the first ERROR line in the SOURCE output, fix it and run SOURCE again; the script is safe to run any number of times.

First save the script below as a plain text file named sample-database.sql: open a new file in any text editor (Notepad, TextEdit in plain-text mode, or a code editor), paste the script with the copy button and save it.

Then create an empty database, switch to it and run the file. Finish with SHOW TABLES;: you should see ten tables. Save the file in a short folder without spaces, such as C:/mentornxt/sql on Windows or ~/mentornxt/sql on macOS. Write the path with forward slashes: SOURCE C:/mentornxt/sql/sample-database.sql. On macOS the client does not expand ~, so write the full path, such as /Users/yourname/mentornxt/sql/sample-database.sql (pwd shows it). In Workbench, double-click practice in the Navigator, choose File → Open SQL Script and run it all with Ctrl+Shift+Enter (Cmd+Shift+Enter on macOS).

Order of execution

Load the practice database

Put the steps in the order you run them.

ERROR: Failed to open file '...', error: 2 means the path is wrong (error 2 is "file not found"). ERROR 1046 (3D000): No database selected means you skipped USE practice;.

Reset the practice data at any time

Running the script again resets every practice table to its original rows. Its first lines drop all ten tables (DROP TABLE IF EXISTS) and the rest builds them again, so it does not matter what you changed or deleted.

SQL
UPDATE customers SET city = 'Mysuru' WHERE customer_id = 1;SELECT customer_id, name, city FROM customers WHERE customer_id = 1;
Output
+-------------+----------+--------+
| customer_id | name     | city   |
+-------------+----------+--------+
|           1 | Asha Rao | Mysuru |
+-------------+----------+--------+

Reset whenever an output stops matching a lesson; usually an earlier UPDATE or DELETE changed the data. Tables you created yourself in examples, such as trains or hostel_rooms, are not touched by the reset.

For a completely clean start, drop the whole database and load it again: DROP DATABASE practice;, CREATE DATABASE practice;, USE practice;, then SOURCE the file.

Next step

Check your understanding

10 quick questions