What is SQL?
SQL (Structured Query Language) is the language used to communicate with a relational database.
Think of it like this:
Java ---> JVM
Browser ---> Web Server
SQL ---> Database
Whenever you want to:
- Store data
- Retrieve data
- Update data
- Delete data
You use SQL.
Example
Suppose we have this table.
Employees
| Employee_ID | Name | Age | Department | Salary |
| 101 | Rahul | 25 | IT | 60000 |
| 102 | Priya | 28 | HR | 55000 |
| 103 | Amit | 30 | IT | 70000 |
| 104 | Neha | 24 | Sales | 50000 |
| 105 | Rohan | 29 | Finance | 65000 |
Now suppose you ask:
Show all employees.
SQL:
SELECT * FROM Employees;
SQL is simply a language for asking questions to the database.
Categories of SQL Commands
Interviewers often ask:
How many types of SQL commands are there?
There are five categories.
| Category | Purpose |
| DDL | Define database structure |
| DML | Manipulate data |
| DQL | Retrieve data |
| DCL | Control permissions |
| TCL | Manage transactions |
1. DDL (Data Definition Language)
Used to create or modify the database structure.
Commands:
- CREATE
- ALTER
- DROP
- TRUNCATE
- RENAME
Create a table:
CREATE TABLE Employees (
Employee_ID INT,
Name VARCHAR(50),
Age INT
);
Add a new column:
ALTER TABLE Employees
ADD Salary INT;
Delete the table completely:
DROP TABLE Employees;
Remove all rows but keep the table:
TRUNCATE TABLE Employees;
Rename the table:
RENAME TABLE Employees TO Employee;
2. DML (Data Manipulation Language)
Used to work with the data inside tables.
Commands:
- INSERT
- UPDATE
- DELETE
Insert:
INSERT INTO Employees
VALUES (101,'Rahul',25,'IT',60000);
Update:
UPDATE Employees
SET Salary = 70000
WHERE Employee_ID = 101;
Delete:
DELETE FROM Employees
WHERE Employee_ID = 101;
Notice: the table still exists. Only rows are affected.
3. DQL (Data Query Language)
Used to retrieve data.
Command:
- SELECT
Example:
SELECT * FROM Employees;
Only reads data. It doesn't modify anything.
4. DCL (Data Control Language)
Used to give or remove permissions.
Commands:
- GRANT
- REVOKE
Allow a user to read a table:
GRANT SELECT
ON Employees
TO Rahul;
Remove permission:
REVOKE SELECT
ON Employees
FROM Rahul;
Mostly used by Database Administrators.
5. TCL (Transaction Control Language)
Used to manage transactions.
Commands:
- COMMIT
- ROLLBACK
- SAVEPOINT
Example:
UPDATE Employees
SET Salary = Salary + 5000;
If everything looks good:
COMMIT;
If something goes wrong:
ROLLBACK;
Transactions are covered in detail later.
SELECT Statement
The most used SQL statement.
General syntax:
SELECT column1, column2
FROM table_name;
Example:
SELECT Name, Salary
FROM Employees;
SELECT *
Returns every column.
SELECT *
FROM Employees;
Should we use SELECT *?
In interviews: yes, it is fine for quick examples.
In production code: usually no.
Why?
Suppose a table has 50 columns, but your application only needs Name and Salary.
Instead of:
SELECT *
FROM Employees;
Write:
SELECT Name, Salary
FROM Employees;
Benefits:
- Transfers less data.
- Uses less memory.
- Can improve performance.
- Makes the query's intent clearer.
SQL Execution Order
Many people think SQL executes left to right. It doesn't.
For this query:
SELECT Name
FROM Employees
WHERE Department = 'IT';
The logical execution order is:
1. FROM Employees
|
v
2. WHERE Department = 'IT'
|
v
3. SELECT Name
This explains why the database first identifies the source table, then filters rows, and only then returns the requested columns.
SQL is Case Insensitive
These are equivalent:
SELECT * FROM Employees;
select * from employees;
However, the convention is:
- SQL keywords: UPPERCASE
- Table names: PascalCase or snake_case
- Column names: snake_case or camelCase, depending on the project style
Example:
SELECT employee_id, employee_name
FROM employees;
Summary Table
| Command | Purpose |
| CREATE | Create a table |
| ALTER | Modify a table |
| DROP | Delete a table |
| TRUNCATE | Remove all rows, keep the table |
| INSERT | Add new rows |
| UPDATE | Modify existing rows |
| DELETE | Remove rows |
| SELECT | Retrieve data |
| GRANT | Give permissions |
| REVOKE | Remove permissions |
| COMMIT | Save transaction |
| ROLLBACK | Undo transaction |
Interview Questions
1. What is SQL?
SQL is the standard language used to create, retrieve, update, and manage data in relational databases.
2. What are the five categories of SQL commands?
- DDL: Data Definition Language
- DML: Data Manipulation Language
- DQL: Data Query Language
- DCL: Data Control Language
- TCL: Transaction Control Language
3. What is the difference between DELETE, TRUNCATE, and DROP?
| DELETE | TRUNCATE | DROP |
| Removes selected rows | Removes all rows | Deletes the entire table |
| Can use WHERE | No WHERE | Removes table structure too |
| Table remains | Table remains | Table no longer exists |
| Generally logged row by row | Typically minimally logged, DBMS-dependent | Removes metadata and data |
4. Why should we avoid SELECT * in production?
Because it retrieves unnecessary columns, increasing network transfer, memory usage, and potentially reducing query performance.
Next Topic
The next step is SQL Filtering & Sorting, where you'll use:
WHERE- Comparison operators
- Logical operators
ORDER BYLIMITDISTINCTLIKEINBETWEENIS NULL
These are the commands you'll use in almost every SQL query.