RDBMS SQL Basics
Understand the basic SQL commands used to work with relational databases.
SQL Basics in RDBMS
SQL, which stands for Structured Query Language, is the standard language used to communicate with a Relational Database Management System. It lets you create databases, store information, retrieve records, update existing data, and remove unwanted data.
Think of SQL as the language you use to talk to a database. Instead of searching through thousands of records manually, you write an SQL query, and the database returns the information you need.
Whether you are building a website, mobile application, banking system, or school management software, SQL is one of the most important skills for working with databases.
What is SQL?
SQL is a programming language designed specifically for managing relational databases. It helps users interact with data stored in tables.
With SQL, you can:
- Create databases and tables
- Insert new records
- View stored data
- Update existing information
- Delete records
- Control user access
- Manage database transactions
Almost every popular RDBMS, such as MySQL, PostgreSQL, Oracle Database, Microsoft SQL Server, and SQLite, supports SQL.
Why is SQL Important?
Databases often contain thousands or even millions of records. SQL makes it easy to work with this data quickly and accurately.
Some benefits of SQL include:
- Easy to learn
- Fast data retrieval
- Supports large databases
- Reduces manual work
- Helps maintain data accuracy
- Works with most relational database systems
SQL Categories
SQL commands are grouped into different categories based on their purpose.
DDL (Data Definition Language)
DDL commands are used to create and modify database structures such as tables and databases.
- CREATE
- ALTER
- DROP
- TRUNCATE
CREATE TABLE Students (
StudentID INT,
Name VARCHAR(100),
Age INT
);This command creates a table named Students with three columns.
DML (Data Manipulation Language)
DML commands are used to add, modify, and delete records inside tables.
- INSERT
- UPDATE
- DELETE
INSERT INTO Students
(StudentID, Name, Age)
VALUES
(101, 'Rahul', 18);This command inserts a new student record into the table.
UPDATE Students
SET Age = 19
WHERE StudentID = 101;DELETE FROM Students
WHERE StudentID = 101;DQL (Data Query Language)
DQL is mainly used to retrieve information from the database. The most commonly used command is SELECT.
SELECT * FROM Students;This query displays all records from the Students table.
SELECT Name, Age
FROM Students;DCL (Data Control Language)
DCL commands control access to the database.
- GRANT
- REVOKE
These commands allow administrators to give or remove permissions for different users.
TCL (Transaction Control Language)
TCL commands manage database transactions.
- COMMIT
- ROLLBACK
- SAVEPOINT
These commands are useful when multiple operations need to be treated as a single unit.
Writing a Simple SQL Query
Suppose you have a table called Students.
| StudentID | Name | Age | City |
|---|---|---|---|
| 101 | Rahul | 18 | Delhi |
| 102 | Priya | 19 | Mumbai |
| 103 | Aman | 20 | Jaipur |
To display all students:
SELECT * FROM Students;To display only students from Delhi:
SELECT *
FROM Students
WHERE City = 'Delhi';The WHERE clause filters records based on a condition.
SQL Rules to Remember
When writing SQL queries, keep these basic rules in mind:
- SQL keywords are not case-sensitive, but uppercase keywords improve readability.
- Table and column names should match the names used in the database.
- Text values are enclosed in single quotes.
- Every SQL statement usually ends with a semicolon.
- Use meaningful table and column names for better organization.