Topic 2 – SQL Basics: Getting Started with SQL

image 2

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

  1. Retrieving Data (SELECT) SELECT * FROM products; (Gets all products from the table.)
  2. Adding Data (INSERT) INSERT INTO customers (name, email, city) VALUES ('Rahul', 'rahul@email.com', 'Delhi'); (Adds a new customer to the database.)
  3. Updating Data (UPDATE) UPDATE customers SET city = 'Noida' WHERE name = 'Rahul'; (Changes Rahul’s city to Noida.)
  4. 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 TypeDescriptionExample
INTStores whole numbersage INT β†’ 25
VARCHAR(n)Stores text (up to n characters)name VARCHAR(50) β†’ ‘Amit’
DECIMAL(m, d)Stores precise decimal valuesprice DECIMAL(10,2) β†’ 199.99
DATEStores date valuesdob 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 TypeDescriptionExample
NUMBER(p,s)Stores numbers with precision & scalesalary NUMBER(10,2) β†’ 45000.75
VARCHAR2(n)Stores variable-length textname VARCHAR2(50) β†’ ‘Ravi’
TIMESTAMPStores date and timecreated_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 TypePostgreSQLSQL Server
IntegerINTEGERINT
TextTEXTNVARCHAR(n)
DecimalNUMERIC(p,s)DECIMAL(p,s)
Date & TimeTIMESTAMPDATETIME

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, and DATE.
  • Oracle uses NUMBER, VARCHAR2, and TIMESTAMP.
  • PostgreSQL uses TEXT, NUMERIC, and TIMESTAMP.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *