-
Hajipur, Bihar, 844101
SQL stands for Structured Query Language. It’s the standard language used to communicate with databases. Every time you create, read, update, or delete data in MySQL, you’re actually using SQL. It’s not a programming language like Python or Java — instead, it’s a query language that tells the database what to do.
SQL is the foundation of MySQL. Without SQL, you can’t interact with your database. Whether you want to create tables, insert records, or retrieve information, every operation in MySQL runs through SQL statements.
SQL is a language used to store, manage, and manipulate data in a relational database. It works on the concept of tables, which contain rows and columns of data. You can think of each table as a spreadsheet, and SQL as the set of instructions you use to interact with that spreadsheet.
Here are some of the things SQL lets you do:
Create new databases and tables
Insert new records
Update existing data
Retrieve specific information using filters
Delete unnecessary data
Control user access and privileges
For example, this simple SQL statement retrieves all rows from a table named students:
SELECT * FROM students;
It means: “Select all columns from the table named students.”
MySQL itself is a Relational Database Management System (RDBMS). But SQL is the language MySQL understands to perform operations. Think of MySQL as the engine and SQL as the instructions that drive it.
Without SQL:
You couldn’t create or manage tables.
You couldn’t insert or retrieve data.
You couldn’t perform analysis or reporting.
So, learning SQL is the first step in mastering MySQL.
SQL commands in MySQL are grouped into different categories based on what they do. Each type has a specific role.
DDL commands define the structure of the database. They help you create, modify, or delete tables and databases.
| Command | Description |
|---|---|
CREATE |
Creates a new table or database |
ALTER |
Modifies an existing table (like adding a column) |
DROP |
Deletes a table or database |
TRUNCATE |
Removes all data from a table but keeps its structure |
Example:
CREATE TABLE courses (
id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL
);
DML commands deal with data inside tables — adding, changing, or removing records.
| Command | Description |
|---|---|
INSERT |
Adds new rows to a table |
UPDATE |
Changes data in existing rows |
DELETE |
Removes rows from a table |
Example:
INSERT INTO courses (course_name) VALUES ('Mathematics');
DQL commands are used to fetch data from the database.
There’s mainly one command: SELECT.
Example:
SELECT course_name FROM courses;
The SELECT statement is one of the most powerful features of SQL. You can use it to filter, sort, and combine data from multiple tables.
DCL commands handle user permissions — who can do what inside the database.
| Command | Description |
|---|---|
GRANT |
Gives privileges to a user |
REVOKE |
Removes privileges from a user |
Example:
GRANT ALL PRIVILEGES ON school.* TO 'user1'@'localhost';
TCL commands manage database transactions. A transaction is a set of operations that should either all happen or none at all.
| Command | Description |
|---|---|
COMMIT |
Saves changes permanently |
ROLLBACK |
Cancels uncommitted changes |
SAVEPOINT |
Sets a temporary point to roll back to later |
Example:
START TRANSACTION;
UPDATE students SET age = 20 WHERE id = 5;
COMMIT;
Before writing SQL queries, it’s important to understand a few basic rules:
SQL keywords are not case-sensitive.SELECT and select mean the same thing. However, writing keywords in uppercase makes queries easier to read.
Each SQL statement ends with a semicolon ( ; ).
String values are enclosed in single quotes ('), not double quotes.
Example:
INSERT INTO students (name) VALUES ('Ananya');
Comments can be added for better readability:
-- This is a single-line comment
/* This is a
multi-line comment */
White spaces and line breaks are ignored by SQL, so formatting doesn’t affect execution.
When you run an SQL statement in MySQL, it goes through a series of steps:
Parsing: MySQL checks if the syntax is valid.
Optimization: The SQL optimizer figures out the most efficient way to execute the command.
Execution: MySQL performs the operation and returns the result.
For example:
SELECT name, age FROM students WHERE age > 18;
Here’s what happens:
MySQL locates the students table.
It checks each row where age > 18.
It retrieves the name and age columns of matching records.
Here are some frequently used SQL commands with examples.
CREATE DATABASE school;
USE school;
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT,
city VARCHAR(50)
);
INSERT INTO students (name, age, city)
VALUES ('Riya', 19, 'Mumbai');
SELECT * FROM students;
UPDATE students SET city = 'Delhi' WHERE id = 1;
DELETE FROM students WHERE id = 3;
DROP TABLE students;
Simple and Easy to Learn:
SQL syntax is close to plain English. You can learn the basics quickly.
Powerful and Flexible:
With a few commands, you can handle large amounts of data efficiently.
Portable:
The same SQL queries work on most RDBMS systems (like MySQL, Oracle, or PostgreSQL).
Supports Data Integrity:
SQL ensures that the data entered into the database follows the rules you set (like no duplicates or nulls).
Compatible with Many Languages:
SQL works smoothly with programming languages like PHP, Java, and Python.
Let’s say you’re managing a school database. You create two tables — students and courses — and want to connect them.
CREATE TABLE courses (
id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100)
);
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
age INT,
course_id INT,
FOREIGN KEY (course_id) REFERENCES courses(id)
);
Now, you can insert data:
INSERT INTO courses (course_name)
VALUES ('Science'), ('Maths'), ('English');
INSERT INTO students (name, age, course_id)
VALUES ('Meera', 18, 2), ('Pooja', 19, 1), ('Aditi', 20, 3);
To view each student’s course:
SELECT students.name, courses.course_name
FROM students
JOIN courses ON students.course_id = courses.id;
This shows how SQL connects data between tables and fetches meaningful results.
SQL (Structured Query Language) is the backbone of MySQL.
It allows you to create, manage, and manipulate data.
SQL statements are divided into five categories: DDL, DML, DQL, DCL, and TCL.
MySQL processes SQL through parsing, optimization, and execution.
SQL’s simplicity and power make it essential for every database developer.
Write a SQL statement to create a database named school.
Create a table students with columns id, name, age, and city. Make id the primary key and auto-incremented.
Insert three sample student records into the students table.
Write a SQL query to retrieve all columns and rows from the students table.
Update the city of the student whose id is 2 to Delhi.
Delete the record of the student whose name is 'Riya'.
Create a table courses with columns id (primary key) and course_name.
Insert at least three courses into the courses table.
Write a query to fetch the names of all students along with the courses they are enrolled in (assume a course_id column in students).
Drop the courses table from the database.