
Play Store Application link β SQL in 18 steps – App on Google Play
SQL Syntax and Commands
SQL is a simple yet powerful language used to interact with databases. Think of it as giving instructions to a digital assistant that manages your data. Hereβs a basic structure of an SQL query:
SELECT column_name FROM table_name WHERE condition;
Letβs break it down:
SELECT
β Used to retrieve data.column_name
β The specific data you want (e.g., names, prices).FROM table_name
β Specifies which table to fetch data from.WHERE condition
β Filters results based on a condition.
Example: Fetching Customers
Imagine you own a shopping website and want to see all customers from Noida.
SELECT name, email FROM customers WHERE city = 'Noida';
This fetches only those customers who live in Noida.
Common SQL Commands
- Retrieving Data (
SELECT
)SELECT * FROM products;
(Gets all products from the table.) - Adding Data (
INSERT
)INSERT INTO customers (name, email, city) VALUES ('Rahul', 'rahul@email.com', 'Delhi');
(Adds a new customer to the database.) - Updating Data (
UPDATE
)UPDATE customers SET city = 'Noida' WHERE name = 'Rahul';
(Changes Rahulβs city to Noida.) - Deleting Data (
DELETE
)DELETE FROM customers WHERE name = 'Rahul';
(Removes Rahul from the database.)
SQL Data Types
Every column in a database has a specific type of data it can store. Just like in Excel, where a cell can hold text, numbers, or dates, databases also have predefined data types.
MySQL Data Types
Data Type | Description | Example |
---|---|---|
INT | Stores whole numbers | age INT β 25 |
VARCHAR(n) | Stores text (up to n characters) | name VARCHAR(50) β ‘Amit’ |
DECIMAL(m, d) | Stores precise decimal values | price DECIMAL(10,2) β 199.99 |
DATE | Stores date values | dob DATE β ‘2000-01-15’ |
Example Table Creation in MySQL:
CREATE TABLE employees (
id INT PRIMARY KEY,
name VARCHAR(100),
salary DECIMAL(10,2),
joining_date DATE
);
Oracle Data Types
Oracle has some additional types specific to its architecture.
Data Type | Description | Example |
---|---|---|
NUMBER(p,s) | Stores numbers with precision & scale | salary NUMBER(10,2) β 45000.75 |
VARCHAR2(n) | Stores variable-length text | name VARCHAR2(50) β ‘Ravi’ |
TIMESTAMP | Stores date and time | created_at TIMESTAMP β ‘2025-03-04 12:30:00’ |
Example Table Creation in Oracle:
CREATE TABLE orders (
order_id NUMBER PRIMARY KEY,
customer_name VARCHAR2(100),
total_price NUMBER(10,2),
order_date TIMESTAMP
);
PostgreSQL & SQL Server Data Types
Data Type | PostgreSQL | SQL Server |
---|---|---|
Integer | INTEGER | INT |
Text | TEXT | NVARCHAR(n) |
Decimal | NUMERIC(p,s) | DECIMAL(p,s) |
Date & Time | TIMESTAMP | DATETIME |
Example Table Creation in PostgreSQL:
CREATE TABLE sales (
id SERIAL PRIMARY KEY,
product_name TEXT,
price NUMERIC(8,2),
sold_at TIMESTAMP
);
Key Takeaway:
- MySQL uses
VARCHAR
,DECIMAL
, andDATE
. - Oracle uses
NUMBER
,VARCHAR2
, andTIMESTAMP
. - PostgreSQL uses
TEXT
,NUMERIC
, andTIMESTAMP
.
Understanding these differences helps when switching between database systems.
Conclusion
SQL commands and data types form the foundation of database management. Knowing the right data type ensures better performance and accuracy.