What is SQL?
SQL (Structured Query Language) is a programming language used to manage and interact with databases. It allows you to store, retrieve, update, and delete data.
Let’s take a store example (supermarket) and see how SQL is used in real-time:
1.Store (Add) Data - Insert
A new customer buys items, so we record it in the database.
INSERT INTO Customers (CustomerID, Name, City)
VALUES (101, ‘Ravi Kumar’, ‘Hyderabad’);
–Adds Ravi as a new customer.
2.Retrieve (Read) Data – SELECT
The manager wants to see all customers from Hyderabad.
SELECT Name, City
FROM Customers
WHERE City = ‘Hyderabad’;
–Displays list of Hyderabad customers.
3.Update Data – UPDATE
A customer changes their phone number.
UPDATE Customers
SET Phone = ‘9876543210’
WHERE CustomerID = 101;
–Ravi’s phone number is updated.
4.Delete Data – DELETE
A customer requests account deletion.
DELETE FROM Customers
WHERE CustomerID = 101;
–Removes Ravi’s record from the database.
In real-life apps (like Amazon, BigBasket, or a store’s billing system):
-
INSERT
happens when a new order is placed. -
SELECT
is used when showing product listings or search results. -
UPDATE
occurs if stock quantity or customer details change. -
DELETE
happens if products are discontinued or accounts are closed.
REAL TIME EXAMPLE:
Tables
1.Customers
CustomerID | Name | City | Phone |
---|---|---|---|
101 | Ravi Kumar | Hyderabad | 9876543210 |
102 | Meena Rao | Chennai | 9123456789 |
2. Products
ProductID | ProductName | Price | Stock |
---|---|---|---|
201 | Rice Bag | 800 | 20 |
202 | Sugar 1Kg | 50 | 100 |
203 | Milk 1L | 40 | 50 |
3. Orders
OrderID | CustomerID | ProductID | Quantity | OrderDate |
---|---|---|---|---|
301 | 101 | 202 | 2 | 2025-08-22 |
302 | 102 | 203 | 5 | 2025-08-22 |
SQL Operations
1.Store (Add)
Customer places an order for Rice Bag.
INSERT INTO Orders (OrderID, CustomerID, ProductID, Quantity, OrderDate)
VALUES (303, 101, 201, 1, ‘2025-08-22’);
2.Retrieve (Read)
Get all orders placed by Ravi.
SELECT o.OrderID, p.ProductName, o.Quantity, o.OrderDate
FROM Orders o
JOIN Products p ON o.ProductID = p.ProductID
WHERE o.CustomerID = 101;
3.Update
Stock decreases after a purchase.
UPDATE Products
SET Stock = Stock – 1
WHERE ProductID = 201;
4.Delete
If an order is canceled.
DELETE FROM Orders
WHERE OrderID = 303;