1. What is MySQL Workbench?

MySQL Workbench is a graphical tool used to manage MySQL databases. Instead of working only with command lines, you can use a visual interface to:

  • Connect to a MySQL server
  • Create databases
  • Create tables
  • Insert data
  • Write SQL queries
  • Update and delete records
  • Export data
  • Import SQL files
  • Manage users and permissions
  • Design database diagrams

It is very useful for students, developers, and database administrators.

2. Basic SQL Concepts

Before using Workbench, you need to understand some basic SQL terms.

Database

A database is a container that stores tables.

Example:

school_db

Table

A table stores data in rows and columns.

Example table: students

idnameagecity
1Ali20Rabat
2Sara22Casablanca

Column

A column represents one type of information.

Examples:

id
name
age
city

Row

A row is one record inside a table.

Example:

1, 'Ali', 20, 'Rabat'

SQL Query

A query is an instruction sent to the database.

Example:

SELECT * FROM students;

This means: show all data from the students table.

3. Opening MySQL Workbench

After installing MySQL Workbench:

  1. Open MySQL Workbench
  2. Click your local connection, usually named:

Local instance MySQL

 

  1. Enter your MySQL password
  2. Click OK

You will see the SQL editor.

4. Creating a Database

To create a new database, write:

CREATE DATABASE school_db;

Then execute the query using the lightning button.

To use this database:

USE school_db;

You can also create and use the database together:

CREATE DATABASE school_db;
USE school_db;

5. Creating a Table

Now create a table called students.

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    age INT,
    city VARCHAR(100),
    email VARCHAR(150)
);

Explanation:

id INT AUTO_INCREMENT PRIMARY KEY

Creates an ID that increases automatically.

name VARCHAR(100) NOT NULL

Creates a text column called name. It cannot be empty.

age INT

Creates a number column.

city VARCHAR(100)

Creates a text column for the city.

email VARCHAR(150)

Creates a text column for email.

6. Showing Tables

To show all tables in the current database:

SHOW TABLES;

To see the structure of a table:

DESCRIBE students;

Or:

DESC students;

7. Inserting Data

To insert one student:

INSERT INTO students (name, age, city, email)
VALUES ('Ali', 20, 'Rabat', 'ali@example.com');

To insert multiple students:

INSERT INTO students (name, age, city, email)
VALUES
('Sara', 22, 'Casablanca', 'sara@example.com'),
('Youssef', 19, 'Fes', 'youssef@example.com'),
('Mariam', 21, 'Marrakech', 'mariam@example.com'),
('Omar', 23, 'Agadir', 'omar@example.com');

Important: text values must be written between single quotes:

'Rabat'

Numbers do not need quotes:

20

8. Selecting Data

The most used SQL command is SELECT.

Select all data

SELECT * FROM students;

The * means all columns.

Select specific columns

SELECT name, age FROM students;

This shows only the name and age columns.

Select with aliases

SELECT name AS student_name, city AS student_city
FROM students;

Aliases make column names more readable in the result.

9. Filtering Data with WHERE

The WHERE clause is used to filter results.

Example: students from Rabat

SELECT * FROM students
WHERE city = 'Rabat';

Example: students older than 20

SELECT * FROM students
WHERE age > 20;

Example: students aged 20 or more

SELECT * FROM students
WHERE age >= 20;

Example: students not from Rabat

SELECT * FROM students
WHERE city != 'Rabat';

You can also use:

SELECT * FROM students
WHERE city <> 'Rabat';

Both != and <> mean “not equal”.

10. Common Comparison Operators

OperatorMeaningExample
=Equalcity = 'Rabat'
!=Not equalcity != 'Rabat'
<>Not equalcity <> 'Rabat'
>Greater thanage > 20
<Less thanage < 20
>=Greater or equalage >= 20
<=Less or equalage <= 20

11. Using AND and OR

AND

All conditions must be true.

SELECT * FROM students
WHERE city = 'Rabat' AND age > 18;

This means: students from Rabat and older than 18.

OR

At least one condition must be true.

SELECT * FROM students
WHERE city = 'Rabat' OR city = 'Fes';

This means: students from Rabat or Fes.

Combine AND and OR

SELECT * FROM students
WHERE age > 20 AND (city = 'Rabat' OR city = 'Casablanca');

Parentheses are important when combining conditions.

12. Searching Text with LIKE

The LIKE operator is used to search inside text.

Names starting with A

SELECT * FROM students
WHERE name LIKE 'A%';

% means any number of characters.

Names ending with a

SELECT * FROM students
WHERE name LIKE '%a';

Names containing ar

SELECT * FROM students
WHERE name LIKE '%ar%';

Emails containing example

SELECT * FROM students
WHERE email LIKE '%example%';

13. Sorting Results with ORDER BY

Sort by age ascending

SELECT * FROM students
ORDER BY age ASC;

ASC means ascending: from smallest to largest.

Sort by age descending

SELECT * FROM students
ORDER BY age DESC;

DESC means descending: from largest to smallest.

Sort by city then age

SELECT * FROM students
ORDER BY city ASC, age DESC;

This sorts first by city, then by age.

14. Limiting Results with LIMIT

To show only the first 3 students:

SELECT * FROM students
LIMIT 3;

To skip the first 2 rows and show the next 3:

SELECT * FROM students
LIMIT 3 OFFSET 2;

This is useful for pagination.

Example:

SELECT * FROM students
ORDER BY id ASC
LIMIT 10 OFFSET 0;

Page 1.

SELECT * FROM students
ORDER BY id ASC
LIMIT 10 OFFSET 10;

Page 2.

15. Updating Data

To update a student’s city:

UPDATE students
SET city = 'Tangier'
WHERE id = 1;

Very important: always use WHERE.

Dangerous query:

UPDATE students
SET city = 'Tangier';

This updates all students.

Better:

UPDATE students
SET city = 'Tangier'
WHERE id = 1;

You can update multiple columns:

UPDATE students
SET age = 24, city = 'Rabat'
WHERE id = 2;

16. Deleting Data

To delete one student:

DELETE FROM students
WHERE id = 3;

Dangerous query:

DELETE FROM students;

This deletes all rows from the table.

Before deleting, it is better to check first

SELECT * FROM students
WHERE id = 3;

Then delete:

DELETE FROM students
WHERE id = 3;

17. Using BETWEEN

BETWEEN is used to filter values inside a range.

SELECT * FROM students
WHERE age BETWEEN 20 AND 23;

This includes 20 and 23.

Equivalent:

SELECT * FROM students
WHERE age >= 20 AND age <= 23;

18. Using IN

IN allows you to compare a value with multiple values.

SELECT * FROM students
WHERE city IN ('Rabat', 'Fes', 'Agadir');

This is cleaner than:

SELECT * FROM students
WHERE city = 'Rabat'
   OR city = 'Fes'
   OR city = 'Agadir';

19. Using NOT IN

SELECT * FROM students
WHERE city NOT IN ('Rabat', 'Fes');

This shows students who are not from Rabat or Fes.

20. Working with NULL Values

NULL means empty or unknown.

Insert a student without email:

INSERT INTO students (name, age, city, email)
VALUES ('Hind', 20, 'Rabat', NULL);

To find students without email:

SELECT * FROM students
WHERE email IS NULL;

To find students with email:

SELECT * FROM students
WHERE email IS NOT NULL;

Important: do not use this:

WHERE email = NULL;

That is incorrect.

Use:

WHERE email IS NULL;

21. Counting Records

To count all students:

SELECT COUNT(*) FROM students;

With alias:

SELECT COUNT(*) AS total_students
FROM students;

Count students from Rabat:

SELECT COUNT(*) AS total_rabat_students
FROM students
WHERE city = 'Rabat';

22. Aggregate Functions

Aggregate functions calculate values from many rows.

COUNT

SELECT COUNT(*) FROM students;

AVG

Average age:

SELECT AVG(age) AS average_age
FROM students;

MIN

Youngest student:

SELECT MIN(age) AS youngest_age
FROM students;

MAX

Oldest student:

SELECT MAX(age) AS oldest_age
FROM students;

SUM

If you had a column called score:

SELECT SUM(score) AS total_score
FROM students;

23. Grouping Data with GROUP BY

GROUP BY groups rows that have the same value.

Example: count students by city.

SELECT city, COUNT(*) AS total_students
FROM students
GROUP BY city;

Example result:

citytotal_students
Rabat2
Fes1
Agadir1

24. Filtering Groups with HAVING

WHERE filters rows before grouping.

HAVING filters groups after grouping.

Example:

SELECT city, COUNT(*) AS total_students
FROM students
GROUP BY city
HAVING COUNT(*) > 1;

This shows only cities with more than one student.

25. Difference Between WHERE and HAVING

WHERE

Used before grouping:

SELECT city, COUNT(*) AS total_students
FROM students
WHERE age > 20
GROUP BY city;

HAVING

Used after grouping:

SELECT city, COUNT(*) AS total_students
FROM students
GROUP BY city
HAVING COUNT(*) > 1;

26. Creating a Second Table

Now create another table called courses.

CREATE TABLE courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_name VARCHAR(100) NOT NULL,
    teacher VARCHAR(100)
);

Insert data:

INSERT INTO courses (course_name, teacher)
VALUES
('SQL Basics', 'Mr Ahmed'),
('Python Programming', 'Ms Sara'),
('Web Development', 'Mr Youssef');

Now create a table to connect students with courses.

CREATE TABLE enrollments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    course_id INT,
    enrollment_date DATE,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(id)
);

Insert enrollments:

INSERT INTO enrollments (student_id, course_id, enrollment_date)
VALUES
(1, 1, '2026-04-01'),
(2, 1, '2026-04-02'),
(2, 2, '2026-04-03'),
(4, 3, '2026-04-04');

27. Joining Tables

Joins are used to combine data from multiple tables.

INNER JOIN

Shows only matching records.

SELECT students.name, courses.course_name, enrollments.enrollment_date
FROM enrollments
INNER JOIN students ON enrollments.student_id = students.id
INNER JOIN courses ON enrollments.course_id = courses.id;

This shows students and their courses.

28. Using Aliases in Joins

Aliases make queries shorter.

SELECT s.name, c.course_name, e.enrollment_date
FROM enrollments e
INNER JOIN students s ON e.student_id = s.id
INNER JOIN courses c ON e.course_id = c.id;

Here:

students s

means the students table is called s inside the query.

courses c

means the courses table is called c.

enrollments e

means the enrollments table is called e.

29. LEFT JOIN

A LEFT JOIN shows all records from the left table, even if there is no match.

Example: show all students, even students without courses.

SELECT s.name, c.course_name
FROM students s
LEFT JOIN enrollments e ON s.id = e.student_id
LEFT JOIN courses c ON e.course_id = c.id;

If a student has no course, course_name will be NULL.

30. RIGHT JOIN

A RIGHT JOIN shows all records from the right table.

SELECT s.name, c.course_name
FROM students s
RIGHT JOIN enrollments e ON s.id = e.student_id
RIGHT JOIN courses c ON e.course_id = c.id

In practice, many developers prefer LEFT JOIN because it is easier to read.

31. Subqueries

A subquery is a query inside another query.

Example: students older than the average age.

SELECT *
FROM students
WHERE age > (
    SELECT AVG(age)
    FROM students
);

First, MySQL calculates:

SELECT AVG(age) FROM students;

Then it uses the result in the main query.

32. Using DISTINCT

DISTINCT removes duplicates.

Example:

SELECT DISTINCT city
FROM students;

This shows each city only once.

33. Creating a Table for Orders Example

Let’s create another example for business data.

CREATE TABLE customers (
    id INT AUTO_INCREMENT PRIMARY KEY,
    full_name VARCHAR(100) NOT NULL,
    city VARCHAR(100)
);
CREATE TABLE orders (
    id INT AUTO_INCREMENT PRIMARY KEY,
    customer_id INT,
    total DECIMAL(10,2),
    order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

Insert customers:

INSERT INTO customers (full_name, city)
VALUES
('Ali Ben', 'Rabat'),
('Sara Alami', 'Casablanca'),
('Omar Naji', 'Fes');

Insert orders:

INSERT INTO orders (customer_id, total, order_date)
VALUES
(1, 250.00, '2026-04-01'),
(1, 150.00, '2026-04-03'),
(2, 500.00, '2026-04-05'),
(3, 80.00, '2026-04-06');

34. Useful Business Queries

Show all orders with customer names

SELECT c.full_name, o.total, o.order_date
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id;

Total sales

SELECT SUM(total) AS total_sales
FROM orders;

Total sales by customer

SELECT c.full_name, SUM(o.total) AS total_spent
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
GROUP BY c.full_name;

Customers who spent more than 200

SELECT c.full_name, SUM(o.total) AS total_spent
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
GROUP BY c.full_name
HAVING SUM(o.total) > 200;

Orders between two dates

SELECT *
FROM orders
WHERE order_date BETWEEN '2026-04-01' AND '2026-04-05';

35. Using Dates in SQL

Current date

SELECT CURRENT_DATE();

Current date and time

SELECT NOW();

Extract year

SELECT YEAR(order_date) AS order_year
FROM orders;

Extract month

SELECT MONTH(order_date) AS order_month
FROM orders;

Orders from April 2026

SELECT *
FROM orders
WHERE YEAR(order_date) = 2026
AND MONTH(order_date) = 4;

Better for performance:

SELECT *
FROM orders
WHERE order_date >= '2026-04-01'
AND order_date < '2026-05-01';

36. Altering Tables

ALTER TABLE is used to modify an existing table.

Add a column

ALTER TABLE students
ADD phone VARCHAR(30);

Rename a column

ALTER TABLE students
RENAME COLUMN phone TO phone_number;

Modify a column type

ALTER TABLE students
MODIFY phone_number VARCHAR(50);

Delete a column

ALTER TABLE students
DROP COLUMN phone_number;

37. Dropping Tables and Databases

To delete a table:

DROP TABLE students;

To delete a database:

DROP DATABASE school_db;

Be very careful. These commands permanently remove data.

38. TRUNCATE vs DELETE

DELETE

Deletes rows and can use WHERE.

DELETE FROM students
WHERE city = 'Rabat';

TRUNCATE

Deletes all rows quickly.

TRUNCATE TABLE students;

TRUNCATE removes all data and usually resets AUTO_INCREMENT.

39. Safe Update Mode in MySQL Workbench

Sometimes Workbench shows an error like:

Error Code: 1175. You are using safe update mode

This happens when you try to update or delete without using a key column in the WHERE.

Example that may cause error:

UPDATE students
SET city = 'Rabat'
WHERE name = 'Ali';

Better:

UPDATE students
SET city = 'Rabat'
WHERE id = 1;

To disable safe update mode temporarily:

SET SQL_SAFE_UPDATES = 0;

Then run your query.

To enable it again:

SET SQL_SAFE_UPDATES = 1;

But it is better to keep safe update mode enabled.

40. Exporting Query Results in Workbench

After running a query:

  1. Look at the result grid
  2. Click the small export icon
  3. Choose CSV, JSON, or Excel-compatible format
  4. Save the file

Example query:

SELECT *
FROM students;

Then export the result.

41. Importing an SQL File in Workbench

To import a .sql file:

  1. Open MySQL Workbench
  2. Go to:
Server > Data Import
  1. Choose:
Import from Self-Contained File
  1. Select your .sql file
  2. Choose the target database
  3. Click Start Import

You can also run an SQL file manually:

File > Open SQL Script

Then execute it.

42. Common Workbench Shortcuts

ActionShortcut
Execute queryCtrl + Enter
Execute current statementCtrl + Enter
Execute all or selectedLightning icon
Comment lineCtrl + /
Save scriptCtrl + S
Open scriptCtrl + O

43. Common SQL Mistakes

Forgetting semicolon

Wrong:

SELECT * FROM students

Better:

SELECT * FROM students;

Forgetting quotes around text

Wrong:

SELECT * FROM students
WHERE city = Rabat;

Correct:

SELECT * FROM students
WHERE city = 'Rabat';

Using = with NULL

Wrong

SELECT * FROM students
WHERE email = NULL;

Correct:

SELECT * FROM students
WHERE email IS NULL;

Updating without WHERE

Dangerous:

UPDATE students
SET city = 'Rabat';

Safe:

UPDATE students
SET city = 'Rabat'
WHERE id = 1;

Deleting without WHERE

Dangerous:

DELETE FROM students;

Safe:

DELETE FROM students
WHERE id = 1;

44. Complete Practice Script

You can copy and run this full script in MySQL Workbench.

CREATE DATABASE IF NOT EXISTS school_db;
USE school_db;

DROP TABLE IF EXISTS enrollments;
DROP TABLE IF EXISTS courses;
DROP TABLE IF EXISTS students;

CREATE TABLE students (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    age INT,
    city VARCHAR(100),
    email VARCHAR(150)
);

CREATE TABLE courses (
    id INT AUTO_INCREMENT PRIMARY KEY,
    course_name VARCHAR(100) NOT NULL,
    teacher VARCHAR(100)
);

CREATE TABLE enrollments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    student_id INT,
    course_id INT,
    enrollment_date DATE,
    FOREIGN KEY (student_id) REFERENCES students(id),
    FOREIGN KEY (course_id) REFERENCES courses(id)
);

INSERT INTO students (name, age, city, email)
VALUES
('Ali', 20, 'Rabat', 'ali@example.com'),
('Sara', 22, 'Casablanca', 'sara@example.com'),
('Youssef', 19, 'Fes', 'youssef@example.com'),
('Mariam', 21, 'Marrakech', 'mariam@example.com'),
('Omar', 23, 'Agadir', 'omar@example.com');

INSERT INTO courses (course_name, teacher)
VALUES
('SQL Basics', 'Mr Ahmed'),
('Python Programming', 'Ms Sara'),
('Web Development', 'Mr Youssef');

INSERT INTO enrollments (student_id, course_id, enrollment_date)
VALUES
(1, 1, '2026-04-01'),
(2, 1, '2026-04-02'),
(2, 2, '2026-04-03'),
(4, 3, '2026-04-04');

SELECT * FROM students;

SELECT name, age, city
FROM students
WHERE age >= 20
ORDER BY age DESC;

SELECT city, COUNT(*) AS total_students
FROM students
GROUP BY city;

SELECT s.name, c.course_name, e.enrollment_date
FROM enrollments e
INNER JOIN students s ON e.student_id = s.id
INNER JOIN courses c ON e.course_id = c.id;

45. Final SQL Query Order

A SQL query is usually written in this order:

SELECT column1, column2
FROM table_name
WHERE condition
GROUP BY column
HAVING group_condition
ORDER BY column
LIMIT number;

Example:

SELECT city, COUNT(*) AS total_students
FROM students
WHERE age >= 20
GROUP BY city
HAVING COUNT(*) >= 1
ORDER BY total_students DESC
LIMIT 5;

Remember this order:

SELECT
FROM
WHERE
GROUP BY
HAVING
ORDER BY
LIMIT

46. Best Practices

Use clear table names:

students
courses
orders
customers

Use lowercase table names.

Use id as primary key.

Always test with SELECT before UPDATE or DELETE.

Example:

SELECT * FROM students
WHERE id = 1;

Then:

UPDATE students
SET city = 'Rabat'
WHERE id = 1;

Always backup important databases before big changes.

Use LIMIT when testing large tables:

SELECT * FROM students
LIMIT 10;

Conclusion

MySQL Workbench is a powerful tool for learning and working with SQL. The most important commands to master are:

CREATE DATABASE
CREATE TABLE
INSERT INTO
SELECT
WHERE
ORDER BY
GROUP BY
HAVING
UPDATE
DELETE
JOIN
ALTER TABLE
DROP TABLE

Once you understand these commands, you can manage almost any MySQL database project.