📘 CodingMarble Learn

SQL with MySQL: Create Tables, Change Rows and Ask Questions

SQL (Structured Query Language) is the language used to talk to a relational DBMS like MySQL. DDL commands (CREATE, ALTER, DROP) define tables; DML commands (INSERT, UPDATE, DELETE) change rows; DQL (SELECT) reads data. Each column has a data type such as INT, FLOAT, CHAR, VARCHAR or DATE. SELECT … WHERE filters rows using relational operators, BETWEEN, AND/OR/NOT and IS NULL.

🎬 Step-by-step story

  1. SQL commands come in three families: DDL builds, DML changes rows, DQL asks.
  2. CREATE DATABASE and CREATE TABLE build the structure; ALTER changes it; DROP removes it.
  3. INSERT adds rows to the table one by one.
  4. SELECT with WHERE picks only the rows that pass a condition.
  5. UPDATE changes values and DELETE removes rows; WHERE decides which.
  6. Your turn: run a query and see which rows match.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

🤔 Common doubts, cleared

Is SQL the same as MySQL?

No. SQL is the language. MySQL is a DBMS program that understands SQL. Oracle and PostgreSQL also use SQL.

When should I use CHAR instead of VARCHAR?

Use CHAR when every value has the same length (PIN code, phone number). Use VARCHAR when lengths vary (names).

What happens if I insert a row with a Roll that already exists?

Roll is the primary key, so MySQL rejects it with a "Duplicate entry" error.

Does BETWEEN include the end values?

Yes. BETWEEN 60 AND 80 includes 60 and 80.

I forgot WHERE in my UPDATE. What happened?

Every row was changed. Always run a SELECT with the same WHERE first to see which rows will be hit.

Why is Mohan missing from "NOT City = 'Delhi'"?

His City is NULL (unknown). A comparison with NULL is never True, even with NOT. Use IS NULL to find him.

What is SQL? DDL, DML and DQL

SQL (Structured Query Language) is the standard language to create and use relational databases. MySQL is a free, open-source DBMS that understands SQL. SQL keywords are not case-sensitive; each statement ends with a semicolon ;.

Data types in MySQL

Text and dates go in single quotes: 'Asha', '2026-04-01'.

CREATE DATABASE, CREATE TABLE, DROP and ALTER

CREATE DATABASE school;
USE school;
SHOW TABLES;
CREATE TABLE student (
  Roll  INT PRIMARY KEY,
  Name  VARCHAR(20) NOT NULL,
  Marks INT,
  City  VARCHAR(15)
);
DESCRIBE student;               -- see the structure
ALTER TABLE student ADD Phone CHAR(10);
ALTER TABLE student MODIFY City VARCHAR(25);
ALTER TABLE student DROP Phone;
DROP TABLE student;             -- removes table + data
DROP DATABASE school;

PRIMARY KEY and NOT NULL are constraints (rules the DBMS checks). ALTER can also add a primary key: ALTER TABLE student ADD PRIMARY KEY (Roll);

INSERT: adding rows

INSERT INTO student VALUES (1, 'Asha', 92, 'Delhi');
INSERT INTO student (Roll, Name, Marks) VALUES (3, 'Mohan', 58);   -- City becomes NULL

Values must be in column order and match the data types. A duplicate primary key is rejected.

SELECT with WHERE

SELECT * FROM student;                       -- all rows, all columns
SELECT Name, Marks FROM student;             -- chosen columns
SELECT * FROM student WHERE Marks > 80;

Relational operators: = <> (or !=) < > <= >=.

BETWEEN

WHERE Marks BETWEEN 60 AND 80 includes both ends (60 and 80). Same as Marks >= 60 AND Marks <= 80.

Logical operators

AND (both true), OR (at least one true), NOT (reverse). WHERE City = 'Delhi' AND Marks > 90.

IS NULL

NULL means "unknown", so = NULL never works. Use WHERE City IS NULL or IS NOT NULL.

UPDATE and DELETE

UPDATE student SET Marks = Marks + 5 WHERE Marks < 70;
UPDATE student SET City = 'Agra' WHERE Roll = 3;
DELETE FROM student WHERE City IS NULL;
DELETE FROM student;          -- removes ALL rows, table stays

Always check the WHERE clause first. Without it, UPDATE changes every row and DELETE empties the table. DELETE removes rows (DML); DROP removes the table itself (DDL).

Try it: your class in MySQL

Install MySQL (or use any online SQL playground). Create a table of 5 friends with Roll, Name, Marks, City. Insert rows, leaving one City empty. Before running each query, write down which names you expect: marks above 80, marks between 60 and 80, city IS NULL. Then run them. Compare with step 6 of the 3D.

Key formulas and definitions

Worked examples

1. Write the command to create table BOOK with ISBN (13 characters, primary key), Title (up to 40 characters), Price (decimal with 2 places).

CREATE TABLE book (ISBN CHAR(13) PRIMARY KEY, Title VARCHAR(40), Price DECIMAL(8,2));

2. Add a column Author VARCHAR(30) to BOOK.

ALTER TABLE book ADD Author VARCHAR(30);

3. Using the STUDENT table (Asha 92 Delhi, Ravi 75 Pune, Mohan 58 NULL, Zoya 81 Delhi, Kabir 66 Jaipur), what does SELECT Name FROM student WHERE Marks BETWEEN 60 AND 80; return?

Ravi (75) and Kabir (66). Asha, Zoya are above 80; Mohan is below 60.

4. Show names of students from Delhi who scored more than 90.

SELECT Name FROM student WHERE City = 'Delhi' AND Marks > 90; → Asha.

5. Find students whose city is not recorded.

SELECT * FROM student WHERE City IS NULL; → Mohan.

6. Give 5 grace marks to students below 70, then delete rows with no city. How many rows remain?

UPDATE: Mohan 58 → 63, Kabir 66 → 71. DELETE removes Mohan (City NULL). 4 rows remain.

7. What is the difference between DELETE FROM student; and DROP TABLE student;?

DELETE removes all rows but keeps the empty table (DML). DROP removes the table and its structure completely (DDL).

Common mistakes

Practice quiz

1. Which is a DDL command?
2. Which data type saves space for names of different lengths?
3. Marks BETWEEN 60 AND 80 includes:
4. The correct way to find missing cities is:
5. DELETE FROM student; (no WHERE) will:

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What are DDL, DML and DQL?

DDL defines structure (CREATE, ALTER, DROP); DML changes rows (INSERT, UPDATE, DELETE); DQL reads data (SELECT).

What is the difference between CHAR and VARCHAR?

CHAR(n) always uses n characters; VARCHAR(n) uses only as many as the value needs, up to n.

How do you find NULL values in SQL?

Use WHERE column IS NULL (or IS NOT NULL). = NULL does not work.

Where this is taught

NetherlandsHAVO 4 (bovenbouw, 2e fase)Information
NetherlandsVWO 5Information
RomaniaClasa a XII-aModule 1: Databases (compulsory)
RomaniaClasa a XII-aModule 2: Database management systems (optional)
RomaniaClasa a XII-aModule 5: Procedural database programming (optional)
Ukraine10 класElective: databases (35 h)
Ukraine11 класElective: databases (35 h)
CBSE (India)Class 11Database Concepts and SQL
England (GCSE, A level)Year 113.7 Relational databases and SQL
Japan高校(専門学科)1〜3年Databases
Germany (Bavaria)Jahrgangsstufe 9Data modelling and relational databases basics
China高二Sel.3 Data management and analysis

Learn first

Related lessons

All Informatics Practices lessons