SQL Study Guide: DML, DDL & SELECT
Statements
🎯 Quick Reference for Your Exam
DML (Data Manipulation Language) - The Big 4
● SELECT - Retrieve data
● INSERT - Add new data
● UPDATE - Modify existing data
● DELETE - Remove data
DDL (Data Definition Language)
● CREATE - Create tables/databases
● ALTER - Modify table structure
● DROP - Delete tables/databases
📋 SELECT Statement - Master This First!
Basic Syntax:
SELECT column1, column2, *
FROM table_name
[WHERE condition]
[ORDER BY column ASC|DESC];
Key SELECT Concepts:
1. Basic Selection
SELECT * FROM employees; -- All columns
SELECT first_name, salary FROM employees; -- Specific columns
2. WHERE Clause (Filtering Rows)
SELECT * FROM employees WHERE salary > 50000;
SELECT * FROM employees WHERE department_id = 10;
3. Comparison Operators
● = (equal), >, <, >=, <=, != or <>
● BETWEEN: WHERE salary BETWEEN 30000 AND 50000
● IN: WHERE department_id IN (10, 20, 30)
● LIKE: WHERE first_name LIKE 'J%' (starts with J)
● IS NULL: WHERE commission_pct IS NULL
4. Logical Operators
● AND: Both conditions must be true
● OR: Either condition can be true
● NOT: Negates the condition
SELECT * FROM employees
WHERE salary > 50000 AND department_id = 10;
5. ORDER BY (Sorting)
SELECT * FROM employees ORDER BY salary DESC; -- Highest to lowest
SELECT * FROM employees ORDER BY last_name ASC; -- A to Z (default)
➕ INSERT Statement - Adding Data
Basic Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Examples:
-- Insert with all columns specified
INSERT INTO departments(department_id, department_name, manager_id, location_id)
VALUES (70, 'Public Relations', 100, 1700);
-- Insert with some columns (others will be NULL)
INSERT INTO departments (department_id, department_name)
VALUES (80, 'Marketing');
-- Insert current date
INSERT INTO employees (employee_id, first_name, hire_date)
VALUES (200, 'John', SYSDATE);
Handling NULL Values:
-- Explicit NULL
INSERT INTO departments VALUES (90, 'Finance', NULL, NULL);
-- Implicit NULL (omit from column list)
INSERT INTO departments (department_id, department_name)
VALUES (100, 'IT');
✏️ UPDATE Statement - Modifying Data
Basic Syntax:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
[WHERE condition];
Examples:
-- Update specific row
UPDATE employees
SET salary = 60000
WHERE employee_id = 113;
-- Update multiple columns
UPDATE employees
SET salary = 55000, department_id = 20
WHERE employee_id = 113;
-- Update all rows (NO WHERE clause)
UPDATE employees SET salary = salary * 1.1; -- 10% raise for everyone
⚠️ Warning: Always use WHERE clause unless you want to update ALL rows!
🗑️ DELETE Statement - Removing Data
Basic Syntax:
DELETE FROM table_name
[WHERE condition];
Examples:
-- Delete specific row
DELETE FROM departments WHERE department_name = 'Finance';
-- Delete multiple rows
DELETE FROM employees WHERE salary < 30000;
-- Delete ALL rows (dangerous!)
DELETE FROM copy_emp; -- Removes all data but keeps table structure
🔗 Joins - Combining Tables
Types You Need to Know:
1. Inner Join (Equijoin)
SELECT e.first_name, d.department_name
FROM employees e, departments d
WHERE e.department_id = d.department_id;
2. Self Join
SELECT e.first_name, m.first_name AS manager_name
FROM employees e, employees m
WHERE e.manager_id = m.employee_id;
📊 Group Functions - Aggregating Data
The Essential Functions:
● COUNT() - Number of rows
● SUM() - Total of numeric values
● AVG() - Average of numeric values
● MAX() - Highest value
● MIN() - Lowest value
Examples:
SELECT COUNT(*) FROM employees; -- Total employees
SELECT AVG(salary) FROM employees; -- Average salary
SELECT MAX(salary), MIN(salary) FROM employees; -- Highest and lowest
SELECT SUM(salary) FROM employees WHERE department_id = 10;
GROUP BY Clause:
SELECT department_id, COUNT(*), AVG(salary)
FROM employees
GROUP BY department_id;
HAVING Clause (Filtering Groups):
SELECT department_id, AVG(salary)
FROM employees
GROUP BY department_id
HAVING AVG(salary) > 50000;
🎯 Exam Strategy Based on Your Question
For the Restaurant Ordering ERD Question:
1. CREATE TABLE (DDL)
CREATE TABLE Payment_Transaction (
Transaction_id NUMBER,
Cust_no NUMBER,
Cust_name VARCHAR2(50),
Transaction_Type VARCHAR2(20),
Transaction_Status VARCHAR2(20),
Order_no NUMBER,
Quantity NUMBER,
Price NUMBER(8,2),
Menu_Order VARCHAR2(100)
);
2. INSERT Data
INSERT INTO Payment_Transaction
VALUES (1001, 6788, 'Ms Chan', 'Qr Code', 'Successful', 3004, 4, 19.99, 'Pizza');
INSERT INTO Payment_Transaction
VALUES (1002, 6787, 'Mr steven', 'Cash', 'Pending', 3005, 6, 20.99, 'chocolate lava cake');
-- Continue for all records...
3. SELECT with ORDER BY
SELECT * FROM Payment_Transaction
ORDER BY Cust_Name ASC;
4. UPDATE Statement
UPDATE menu
SET price = 29.90
WHERE ID = 'P301';
5. ALTER TABLE (Add Column)
ALTER TABLE Payment_Transaction
ADD Transaction_date DATE;
6. DELETE Specific Record
DELETE FROM Payment_Transaction
WHERE Transaction_id = 1001;
7. DELETE All Records
DELETE FROM Payment_Transaction;
🔥 Critical Points for Success
Must Remember:
1. Always use WHERE clause in UPDATE/DELETE unless you want to affect ALL rows
2. String values need single quotes: 'John Smith', not "John Smith"
3. Column names are case-insensitive, but string values are case-sensitive
4. NULL handling: Use IS NULL or IS NOT NULL, never = NULL
5. Date functions: SYSDATE for current date, TO_DATE() for specific dates
Common Exam Mistakes to Avoid:
● Forgetting WHERE clause in UPDATE/DELETE
● Using double quotes instead of single quotes for strings
● Mixing up DDL and DML commands
● Forgetting semicolons at end of statements
● Case sensitivity issues with data values
Quick Checklist Before Exam:
✅ Can you write a basic SELECT with WHERE?
✅ Can you INSERT data with and without specifying all columns?
✅ Can you UPDATE specific rows safely?
✅ Can you DELETE specific records?
✅ Do you know the difference between DDL and DML?
✅ Can you use GROUP BY and aggregate functions?
💡 Practice Pattern for Your Exam
Based on your exam question format, practice these patterns:
1. CREATE TABLE → INSERT multiple records → SELECT with ORDER BY
2. UPDATE specific records → ALTER TABLE structure → DELETE operations
3. Use subqueries in INSERT/UPDATE/DELETE
4. Combine SELECT with WHERE, GROUP BY, ORDER BY
Focus on getting the syntax exactly right - SQL is very particular about punctuation and
keywords!