› Module 5: SQL Fundamentals › 5.1 Introduction to SQL & Sublanguages (DDL, DML, DQL, DCL, TCL)
M O D U L E

Module 5: SQL Fundamentals

The standard declarative language for relational database definition, manipulation, and query

5.1 Introduction to SQL & Sublanguages (DDL, DML, DQL, DCL, TCL)

Structured Query Language (SQL) is the standardized declarative programming language designed for managing relational databases. SQL statements are categorized into five primary sublanguages based on operational intent.

Figure 5.1: The Five Sublanguages of SQL DDL Data Definition • CREATE • ALTER • DROP • TRUNCATE • RENAME DML Data Manipulation • INSERT • UPDATE • DELETE • MERGE (Row-level) DQL Data Query • SELECT Filters, projects, joins, and aggregates tuples without mutating tables DCL Data Control • GRANT • REVOKE Controls user permissions and privileges (RBAC) TCL Transaction Control • COMMIT • ROLLBACK • SAVEPOINT • SET TRAN

Figure 5.1: The SQL command taxonomy organizes database operations into structure definition (DDL), records (DML), query (DQL), security (DCL), and transactions (TCL).

Crucial Interview Contrast: DROP vs TRUNCATE vs DELETE

Feature DELETE TRUNCATE DROP
Sublanguage DML DDL DDL
WHERE clause? Yes (filters rows) No (clears whole table) No (destroys object)
Table Schema? Preserved Preserved Completely removed
Performance Slow (logs each row delete) Fast (deallocates data pages) Instant (drops catalog entry)
Rollbackable? Yes (inside transaction) DBMS dependent (Auto-commits in MySQL) Cannot rollback in MySQL/Oracle

5.2 Querying Data: SELECT, WHERE, ORDER BY & Logical Execution Order

Writing SQL queries requires understanding both the lexical order (the order you type the clauses) and the logical execution order (the internal order in which the database query engine executes them).

Figure 5.2: Logical SQL Query Execution Order 1. FROM & JOINs 2. WHERE Row Filter 3. GROUP BY Group Rows 4. HAVING Group Filter 5. SELECT Project cols 6. DISTINCT Dedup 7. ORDER BY Sort Output 8. LIMIT / OFFSET Pagination Why Aliases in SELECT Cannot Be Used in WHERE Clause: Because WHERE (Step 2) is evaluated BEFORE SELECT (Step 5)! But ORDER BY (Step 7) CAN use aliases.

Figure 5.2: SQL execution proceeds logically from FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.

Common SQL Clauses & Patterns

-- Pattern 1: Pattern matching with LIKE
SELECT emp_name, salary 
FROM Employees 
WHERE emp_name LIKE 'S%t'; -- Starts with 'S' and ends with 't'

-- Pattern 2: Range & Set membership
SELECT emp_name, department_id 
FROM Employees 
WHERE salary BETWEEN 50000 AND 90000 
  AND department_id IN (10, 20, 30);

-- Pattern 3: NULL safety
-- NEVER use '= NULL'; always use IS NULL or IS NOT NULL!
SELECT emp_name 
FROM Employees 
WHERE manager_id IS NULL;

5.3 Aggregate Functions, GROUP BY, and HAVING

Aggregate functions perform a calculation across a set of rows and return a single summary scalar value. When combined with the GROUP BY clause, they calculate summaries across partitioned subsets of records.

Core Aggregate Functions

  • COUNT(*): Counts total rows in the group, including rows containing NULL values.
  • COUNT(column_name): Counts non-null values in the specified column.
  • SUM(column_name): Returns the arithmetic sum of numeric values (ignores NULLs).
  • AVG(column_name): Calculates the arithmetic mean (sum / count of non-nulls).
  • MIN(col) / MAX(col): Returns minimum / maximum value (works with numbers, dates, strings).

Critical Interview Distinction: WHERE vs HAVING

Feature WHERE Clause HAVING Clause
Evaluation Point Before rows are grouped (Step 2) After groups are formed (Step 4)
Filtering Scope Individual raw rows Aggregated row groups
Aggregate Functions Permitted? NO (e.g. WHERE AVG(sal) > 5k is INVALID) YES (e.g. HAVING AVG(sal) > 5k is VALID)
-- Find departments with > 5 active employees earning an average salary > 60,000:
SELECT department_id, 
       COUNT(*) AS total_employees, 
       AVG(salary) AS avg_salary
FROM Employees
WHERE status = 'ACTIVE'                -- Row filter before grouping
GROUP BY department_id                -- Aggregate by department
HAVING COUNT(*) > 5                   -- Group filter after aggregation
   AND AVG(salary) > 60000
ORDER BY avg_salary DESC;
MODULE COMPREHENSIVE QUIZ

Module 5 Final Quiz

Test your understanding across all 3 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 5 Final Quiz →
Yukti Mascot