SQL Tutorial chapters (5)
  1. 01Introduction to SQL
  2. 02SQL Database & Tables (DDL)
  3. 03SQL SELECT, WHERE & Clauses
  4. 04SQL Joins & Subqueries
  5. 05SQL Views, Indexes & Practice

Chapter 01

Introduction to SQL

5 topics · 1 sections · beginner-friendly study notes · Updated

Share with friends:WhatsAppTelegram

Embed on your site

Free for teachers, schools and blogs. Please keep the attribution link.

Preview

SQL stands for Structured Query Language.

It is the standard language used to store, read, change and delete data in a relational database.

Think of it as the language you use to talk to a database, like asking the college office for a list of students.

Almost every app stores data in a database, and most databases understand SQL.

Learning SQL is the first step for developers, testers, analysts and data scientists.

Websites, banking apps, school and college portals, shopping sites and reports all use SQL to keep and fetch data.

1

What is SQL

SQL (Structured Query Language) is a standard language used to create, read, update and delete data stored in a relational database.

In short: SQL is the language we use to talk to a database. We write a short command, and the database gives us the answer.

SQL talks to the database in statements

Think about it

Before SQL, every database had its own way of asking for data. A common language made it easy to learn once and use everywhere. Imagine a college office with a big register of students. SQL is like the polite request you give the clerk: "Show me all students of the CSE department."

A relational database keeps data in tables.

Each table has columns (fields) and rows (records).

SQL lets us work with these tables without worrying how the data is stored on disk.

SQL is declarative.

We say WHAT we want, not HOW to get it.

For example, we say "give me all students from Delhi" and the database finds the best way.

SQL became an ANSI and ISO standard, so the basic commands are the same in MySQL, PostgreSQL, Oracle, SQL Server and SQLite.

Each product adds a few extra features, called dialects.

Key points

  • Database: An organised collection of data, like a digital cupboard of registers.
  • Table: A grid of rows and columns that stores one type of data, such as students.
  • Row (record): One entry in a table, such as one student.
  • Column (field): One property of every row, such as the name.
  • Query: A SQL command that asks the database a question.
  • SQL is not case sensitive for keywords. SELECT and select work the same.
  • Most databases end a statement with a semicolon (;).
  • SQL is a language, not a database product.

Example

Code
First SQL query:
-- A tiny first query on a student table
SELECT name, city
FROM student;
2

SQL vs DBMS

A DBMS (Database Management System) is the software that stores and manages data. SQL is the language we use to give instructions to a relational DBMS (RDBMS).

In short: DBMS is the machine. SQL is the language we use to operate the machine.

DBMS manages stored data

Think about it

Beginners often mix these two words. Knowing the difference helps in interviews and in choosing tools. A car and the steering wheel. The car (DBMS) does the work. The steering wheel and pedals (SQL) let you control it.

Examples of DBMS software are MySQL, PostgreSQL, Oracle Database, Microsoft SQL Server and SQLite.

They look after storage, security, backup, users and speed.

SQL is just the set of commands those systems understand.

A DBMS can exist without SQL.

For example, MongoDB is a NoSQL DBMS and uses its own query style.

But a relational DBMS (RDBMS) almost always uses SQL.

So when people say "I use MySQL", they mean the software.

When they say "I wrote a SQL query", they mean the language.

Key points

  • RDBMS: Relational DBMS. It stores data in tables, like MySQL or Oracle.
  • NoSQL: Databases that do not use tables, such as MongoDB.
  • Dialect: A small variation of SQL used by one product.
  • SQL = language. DBMS = software.
  • MySQL, Oracle, PostgreSQL are DBMS products, not languages.
  • One SQL skill works on many DBMS products with small changes.

Example

Code
Same query on many databases:
-- Same SQL idea, different DBMS products
-- MySQL, PostgreSQL, Oracle and SQL Server all accept:
SELECT * FROM student;
3

How SQL Works

When we send a SQL query, the database parses it, checks it, plans the best way to run it, executes it and returns the result.

In short: You type a query. The database reads it, thinks about the fastest path, does the work and shows you the rows.

Think about it

Knowing these steps explains why errors appear (syntax error, table not found) and why some queries are slower than others. Ordering food. You tell the waiter your order (query). The kitchen checks the menu (parser), plans the cooking (optimizer), cooks (engine) and the waiter brings the dish (result).

Step 1: The client (an app or a tool) sends the SQL text to the server.

Step 2: The parser checks the spelling and grammar (syntax).

Step 3: The checker confirms that the tables and columns exist and you have permission.

Step 4: The optimizer chooses the fastest plan, such as using an index instead of reading the whole table.

Step 5: The execution engine reads or changes data using the storage engine.

Step 6: The result set is sent back.

For commands like INSERT or UPDATE, the database returns a message such as "3 rows affected".

Key points

  • Parser: Checks if the SQL text is written correctly.
  • Optimizer: Chooses the fastest way to run the query.
  • Execution engine: Actually reads or changes the data.
  • Result set: The rows returned by a SELECT query.
  • A syntax error means the parser could not understand your query.
  • The optimizer can choose different plans for the same query.

Example

Code
See the query plan:
-- Most databases can show the plan the optimizer picked
EXPLAIN SELECT name FROM student WHERE city = "Delhi";
4

Types of SQL Commands

SQL commands are grouped by purpose: DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language) and TCL (Transaction Control Language).

In short: Some commands build the tables, some change the data, some read the data, some give permissions and some save or undo work.

Think about it

Interviews and exams ask for these groups very often. They also help you remember which command does what. In a college office: DDL builds new registers, DML writes in them, DQL reads them, DCL gives keys to staff, TCL confirms or cancels a batch of entries.

DDL: CREATE, ALTER, DROP, TRUNCATE.

They change the structure of the database.

DML: INSERT, UPDATE, DELETE.

They change the data inside tables.

DQL: SELECT.

It reads data.

Some books put SELECT inside DML.

DCL: GRANT, REVOKE.

They give or take away permissions.

TCL: COMMIT, ROLLBACK, SAVEPOINT.

They control transactions, which are groups of changes done together.

Key points

Example

Code
One command from each group:
-- DDL: build a table
CREATE TABLE student (roll_no INT, name VARCHAR(50));

-- DML: add data
INSERT INTO student VALUES (1, "Asha");

-- DQL: read data
SELECT * FROM student;

-- DCL: give permission
GRANT SELECT ON student TO clerk;

-- TCL: save work
COMMIT;
5

Advantages of SQL

SQL is popular because it is easy to learn, fast, standard, powerful and works with almost every database.

In short: SQL reads almost like English, so even beginners can write useful queries on day one.

Think about it

These points are the standard answer to "Why do we use SQL?" in viva and interviews. Like using a calculator. One tool, simple buttons, and the result is quick and correct.

Easy to learn: commands such as SELECT and INSERT are plain English words.

Fast: databases can handle millions of rows in a blink using indexes.

Standard: ANSI and ISO rules keep the basics the same everywhere.

Powerful: with joins, grouping and subqueries we can answer complex questions.

No coding skill needed to read data: analysts use SQL without being programmers.

Secure: permissions control who can see or change data.

Portable: the same ideas work on MySQL, Oracle, PostgreSQL and more.

Works with many languages: Java, Python, PHP and JavaScript can all send SQL to a database.

Key points

  • Portable: Works on many database products with little change.
  • Scalable: Can handle small and very large data.
  • Advantage 1: Simple and English-like.
  • Advantage 2: Fast data retrieval.
  • Advantage 3: Standard language for all RDBMS.
  • Advantage 4: Secure access with permissions.
  • Advantage 5: Handles huge data and many users.

Read more

Example

Code
Readable query:
-- Simple English-like query to find the top three marks
SELECT name, marks
FROM student
ORDER BY marks DESC
LIMIT 3;

Solved examples

Example 01 · Basic

Classify each command as DDL, DML, DQL, DCL or TCL: CREATE, INSERT, SELECT, GRANT, ROLLBACK.

Given Data:
  • Commands: CREATE, INSERT, SELECT, GRANT, ROLLBACK
Required To Find:

Group of each command

Formula Applied:—
Step-by-Step Solution:
Step 1: CREATE builds a new table, so it changes the structure. This is DDL.
Step 2: INSERT adds new rows, so it changes data. This is DML.
Step 3: SELECT only reads data. This is DQL.
Step 4: GRANT gives permission to a user. This is DCL.
Step 5: ROLLBACK undoes changes of a transaction. This is TCL.
Final Answer: CREATE = DDL, INSERT = DML, SELECT = DQL, GRANT = DCL, ROLLBACK = TCL

Approach note: Ask one question: does it change structure, change data, read data, control access or control a transaction?

Example 02 · Basic

Write a first complete session: create a student table, add two students and show them.

Given Data:
  • Columns: roll_no, name, city
Required To Find:

SQL statements

Formula Applied:—
Step-by-Step Solution:
Step 1: Use CREATE TABLE to define the columns.
Step 2: Use INSERT INTO to add two rows.
Step 3: Use SELECT * to see all rows.
Final Answer: CREATE TABLE student ( roll_no INT, name VARCHAR(50), city VARCHAR(50) ); INSERT INTO student VALUES (1, "Asha", "Delhi"); INSERT INTO student VALUES (2, "Ravi", "Pune"); SELECT * FROM student;

Approach note: First build the table, then add data, then read the data. This order always works.

Example 03 · Intermediate

A friend says "I installed SQL on my laptop". What is wrong with this sentence?

Given Data:
  • SQL is a language
Required To Find:

Correct statement

Formula Applied:—
Step-by-Step Solution:
Step 1: SQL is a language. We cannot install a language like a program.
Step 2: What we install is a DBMS such as MySQL or PostgreSQL.
Step 3: After installing the DBMS, we use SQL to talk to it.
Final Answer: He installed a DBMS (for example MySQL). SQL is the language used with it.

Approach note: Language = SQL. Software = MySQL, Oracle, PostgreSQL, SQL Server.

Example 04 · Basic

Which SQL group does each belong to: UPDATE, DROP, REVOKE, COMMIT?

Given Data:
    Required To Find:

    Four classifications

    Formula Applied:—
    Step-by-Step Solution:
    Step 1: UPDATE changes row data → DML
    Step 2: DROP removes a table → DDL
    Step 3: REVOKE removes privileges → DCL
    Step 4: COMMIT ends a transaction → TCL
    Final Answer: UPDATE=DML, DROP=DDL, REVOKE=DCL, COMMIT=TCL

    Approach note: DDL = structure, DML = data, DCL = access, TCL = transactions.

    Real-world applications

    SQL

    Using Introduction to SQL in real projects

    Websites, banking apps, school and college portals, shopping sites and reports all use SQL to keep and fetch data.

    Mechanism: Write small programs, read errors carefully, then grow complexity only after the basics are solid.

    Common mistakes

    ❌ Myth: SQL and MySQL are the same thing.

    Why this is wrong: The names look alike, so people mix them up.

    ✅ Verified Truth: SQL is the language. MySQL is one database software that understands SQL.

    ⚠️ Exam Tip: Write "MySQL is an RDBMS that uses SQL".

    ❌ Myth: SQL is a programming language like Java.

    Why this is wrong: SQL has commands and logic, so it feels like programming.

    ✅ Verified Truth: SQL is a declarative query language made for databases. It says what data we want, not how to loop to find it.

    ⚠️ Exam Tip: In viva, say "SQL is a domain-specific, declarative language".

    ❌ Myth: DROP and DELETE do the same job.

    Why this is wrong: Both remove something.

    ✅ Verified Truth: DELETE removes rows. DROP removes the whole table or database with its structure.

    ⚠️ Exam Tip: DELETE is DML. DROP is DDL.

    Revision notes

    SQL stands for Structured Query Language. It is the standard language used to store, read, change and delete data in a relational database. Think of it as the language you use to talk to a database, like asking the college office for a list of students.

    High-Yield Quick Revision Points

    1. SQL = Structured Query Language, used for relational databases.
    2. SQL is a language. MySQL, Oracle and PostgreSQL are DBMS software.
    3. Query steps: parse, check, optimize, execute, return result.
    4. DDL: CREATE, ALTER, DROP, TRUNCATE.
    5. DML: INSERT, UPDATE, DELETE. DQL: SELECT.
    6. DCL: GRANT, REVOKE. TCL: COMMIT, ROLLBACK, SAVEPOINT.
    7. Advantages: easy, fast, standard, secure, portable and powerful.

    Practice questions

    Question 1 · MCQ (Interview / semester)

    What does SQL stand for?

    Question 2 · MCQ (Interview / semester)

    Which of these is a DBMS and not a language?

    Question 3 · MCQ (Interview / semester)

    Which group does the CREATE TABLE command belong to?

    Question 4 · MCQ (Interview / semester)

    Which command is used to give permission to a user?

    Question 5 · MCQ (Interview / semester)

    Which step decides the fastest way to run a query?