L
LLLOS.ai
Learn
L

Chapter 4 — Database Query Using Sql

Class 12 · Informatics Practices

Overview

Chapter 4 — Database Query Using Sql Master Diagram

Introduction: This chapter introduces Structured Query Language (SQL) as the standard language for creating, manipulating and retrieving data from relational databases. It covers the core concepts of relational tables, keys and constraints, and the classification of SQL commands (DDL, DML, DCL, TCL). Students learn to write queries to perform real-world data retrieval and updates. Importance: SQL is a foundational skill for database-driven applications, data analysis and software development. For Class 12 Informatics Practices, mastering SQL enables students to design simple databases, perform accurate queries for reports and analytics, and prepare for board practicals and real-world tasks such as managing school records or small business data. Key themes: The chapter emphasises (1) creating and modifying database schema (CREATE, ALTER, DROP), (2) inserting and updating data (INSERT, UPDATE, DELETE), (3) querying data using SELECT with WHERE, ORDER BY, DISTINCT, and functions, (4) grouping and aggregation (GROUP BY, HAVING, aggregate functions), (5) combining data using JOINs and set operations, (6) subqueries and nested queries, (7) enforcing integrity with constraints (PRIMARY…

Learning Objectives

  • Define SQL and distinguish DDL, DML, DCL and TCL commands.
  • Explain the syntax and use of SELECT with WHERE, ORDER BY, DISTINCT and column/table aliases to retrieve and format data.
  • Apply aggregate functions (COUNT, SUM, AVG, MIN, MAX) together with GROUP BY and HAVING to summarize and filter grouped records.
  • Demonstrate INNER, LEFT, RIGHT and FULL JOINs and CROSS JOIN to combine related data from multiple tables.
  • Use subqueries (single-row and multi-row) in SELECT, WHERE and FROM clauses to perform nested queries.
  • Construct queries using set operators (UNION, UNION ALL, INTERSECT, EXCEPT/MINUS) to combine and compare result sets.
  • Write DML statements (INSERT, UPDATE, DELETE) with appropriate WHERE clauses and manage transactions using COMMIT and ROLLBACK.
  • Create and modify tables with DDL commands (CREATE TABLE, ALTER TABLE, DROP TABLE) and define/interpret constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK).

Topics in this chapter

13 topics · tap a topic title to jump straight to it.

💻1

Introduction to SQL

💻 COMPUTER SCIENCE / IT

Introduction to SQL

Key Point: SELECT [columns] FROM table_name WHERE condition; -- basic retrieval

What is SQL?
SQL (Structured Query Language) is the standard language used to communicate with relational database management systems (RDBMS). It lets you create, read, update and delete (CRUD) data and define database structures. SQL is declarative: you specify what you want, and the database engine figures out how to get it.

Key areas of SQL

  • DDL (Data Definition Language): Commands to define schema and structures — CREATE, ALTER, DROP.
  • DML (Data Manipulation Language): Commands to manipulate data — SELECT, INSERT, UPDATE, DELETE.
  • DCL (Data Control Language): Commands to control access — GRANT, REVOKE.
  • TCL (Transaction Control Language): Commands to manage transactions — COMMIT, ROLLBACK.

Basic SELECT query structure

SELECT column1, column2
FROM table_name
WHERE condition
GROUP BY column
HAVING group_condition
ORDER BY column ASC|DESC
LIMIT n;

Important concepts

  • Filters: WHERE clause filters rows using operators (=, <>, <, >, BETWEEN, IN, LIKE).
  • Aggregation: Functions such as COUNT(), SUM(), AVG(), MIN(), MAX() used with GROUP BY.
  • Joins: Combine rows from two or more tables using relationships: INNER JOIN, LEFT (OUTER) JOIN, RIGHT JOIN, FULL OUTER JOIN.
  • Subqueries: A query inside another query (can be in SELECT, FROM, WHERE).
  • Constraints: Enforce rules on data: PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK.
  • Transactions: Ensure atomic operations using BEGIN/COMMIT/ROLLBACK to keep data consistent.

Why SQL matters (real-life uses)
Retailers use SQL to find top-selling products, banks use it to fetch account transactions, schools use it to manage student records, and hospitals use it to retrieve patient histories. Any application that stores structured data typically uses SQL on the backend.

Example workflow (simple): A school web app stores student data in a Students table. When a teacher requests 'all students with marks >= 75', the app runs a SELECT query with a WHERE clause, optionally orders results, and shows them on screen.

Good practices

  • Use meaningful names for tables/columns.
  • Normalize data to avoid redundancy (use separate tables and foreign keys).
  • Use indexes on columns frequently used in WHERE or JOIN to improve performance.
  • Always test queries on sample data and use transactions when performing multiple related updates.

📌 Examples
  • School: List names and marks of students scoring >= 75 in Mathematics SELECT student_id, name, math_marks FROM Students WHERE math_marks >= 75 ORDER BY math_marks DESC;
  • Library: Find books by author 'R.K. Narayan' SELECT book_id, title, author FROM Books WHERE author = 'R.K. Narayan';
  • E-commerce: Total sales amount per product in June SELECT product_id, SUM(amount) AS total_sales FROM Orders WHERE order_date BETWEEN '2025-06-01' AND '2025-06-30' GROUP BY product_id ORDER BY total_sales DESC;
  • Join example: Get order details with customer name SELECT o.order_id, c.customer_name, o.order_date, o.amount FROM Orders o INNER JOIN Customers c ON o.customer_id = c.customer_id WHERE o.order_date >= '2025-01-01';
  • Insert / Update / Delete examples: INSERT INTO Students(student_id, name, age) VALUES (101, 'Anita', 17); UPDATE Students SET age = 18 WHERE student_id = 101; DELETE FROM Students WHERE student_id = 101;
🧮 Formulas
  1. \[SELECT [columns] FROM table_name WHERE condition\]
    \[-- basic retrieval\]
  2. \[SELECT col1\]
    \[AGG_FUNC(col2) FROM table GROUP BY col1 HAVING AGG_FUNC(col2) condition\]
    \[-- aggregation with filter (HAVING)\]
  3. \[INSERT INTO table (col1\]
    \[col2) VALUES (val1\]
    \[val2)\]
    \[-- add a row\]
  4. \[UPDATE table SET col1 = val1 WHERE condition\]
    \[-- modify rows\]
  5. \[DELETE FROM table WHERE condition\]
    \[-- remove rows\]
  6. \[CREATE TABLE table_name (col1 datatype PRIMARY KEY\]
    \[col2 datatype\]
    \[col3 datatype\]
    \[FOREIGN KEY (colX) REFERENCES other_table(colY))\]
💻2

SQL Command Categories

💻 COMPUTER SCIENCE / IT

SQL Command Categories

Key Point: CREATE TABLE table_name (col1 datatype CONSTRAINTS, col2 datatype, ...);

Overview

SQL commands are grouped by purpose into categories so that we can manage database structure, data, security and transactions in an organized way. The main categories used in Class 12 Informatics Practices are:

  • DDL (Data Definition Language)
  • DML (Data Manipulation Language)
  • DQL (Data Query Language)
  • DCL (Data Control Language)
  • TCL (Transaction Control Language)

1. DDL — Data Definition Language

DDL commands define or modify the schema (structure) of database objects such as tables, views, and indexes. They change metadata and are usually auto-committed.

Common commands:

  • CREATE — create database objects (tables, views)
  • ALTER — change structure (add/drop columns, change types)
  • DROP — delete objects (table, view)
  • TRUNCATE — remove all rows from a table (fast, cannot rollback in many DBMS)
  • RENAME — rename an object

2. DML — Data Manipulation Language

DML commands change the data stored in tables. These operations can usually be rolled back or committed (depending on transaction control).

  • INSERT — add new rows
  • UPDATE — modify existing rows
  • DELETE — remove rows (DELETE can be rolled back if within transaction)

3. DQL — Data Query Language

DQL is used to retrieve data from the database. The core command is SELECT (often taught as separate category because querying is a primary activity).

  • SELECT — fetch data using projections, filtering (WHERE), grouping (GROUP BY), ordering (ORDER BY)

4. DCL — Data Control Language

DCL manages access and permissions to the database objects.

  • GRANT — give privileges to users/roles
  • REVOKE — remove granted privileges

5. TCL — Transaction Control Language

TCL commands handle transactions — a set of operations that must succeed or fail as a unit.

  • COMMIT — save transaction changes permanently
  • ROLLBACK — undo changes since last commit (useful for error recovery)
  • SAVEPOINT — define intermediate points within a transaction to roll back to

Important points

  • DDL changes affect structure and are often auto-committed.
  • DML and DQL operate on table data; use TCL to control atomicity and persistence.
  • DCL enforces security and multi-user control.

Short Example Flow (library system)

-- DDL: create table Books
CREATE TABLE Books(book_id INT PRIMARY KEY, title VARCHAR(100), author VARCHAR(50), copies INT);

-- DML: add books
INSERT INTO Books VALUES(1,'Physics', 'A. Author', 3);

-- DQL: view available books
SELECT title, copies FROM Books WHERE copies > 0;

-- DML: update after lending one copy
UPDATE Books SET copies = copies - 1 WHERE book_id = 1;

-- TCL: commit the lending operation
COMMIT;

-- DCL: allow librarian to manage table
GRANT SELECT, INSERT, UPDATE ON Books TO librarian_role;
📌 Examples
  • Library: CREATE TABLE Books(...); INSERT INTO Books(...) VALUES(...); SELECT title FROM Books WHERE copies>0; UPDATE Books SET copies=copies-1 WHERE book_id=10; COMMIT;
  • Banking: BEGIN TRANSACTION; UPDATE Accounts SET balance=balance-500 WHERE acc_no=101; UPDATE Accounts SET balance=balance+500 WHERE acc_no=202; IF all OK THEN COMMIT ELSE ROLLBACK;
  • E‑commerce inventory: ALTER TABLE Products ADD COLUMN stock INT; DELETE FROM Products WHERE discontinued=1; TRUNCATE TABLE TempOrders;
  • Security: GRANT SELECT, INSERT ON Employees TO hr_user; REVOKE DELETE ON Employees FROM temp_user;
🧮 Formulas
  1. \[CREATE TABLE table_name (col1 datatype CONSTRAINTS\]
    \[col2 datatype, ...)\]
  2. \[ALTER TABLE table_name ADD|DROP|MODIFY column_definition;\]
  3. \[DROP TABLE table_name\]
    \[TRUNCATE TABLE table_name\]
  4. \[INSERT INTO table_name (col1\]
    \[col2) VALUES (val1\]
    \[val2)\]
  5. \[UPDATE table_name SET col1 = expr\]
    \[col2 = expr WHERE condition\]
  6. \[DELETE FROM table_name WHERE condition;\]
📊3

Data Types

💻 COMPUTER SCIENCE / IT

Data Types

Key Point: Range for DECIMAL(p,s): values between - (10^(p-s) - 10^(-s)) and + (10^(p-s) - 10^(-s)). Example: DECIMAL(5,2) -> -999.99 to 999.99.

Data Types (SQL) — Class 12 Informatics Practices

In SQL, a data type defines the kind of values a column can hold and how those values are stored, processed and compared. Choosing the correct data type ensures data integrity, efficient storage and correct query results.

Main categories

  • Numeric types: for integer and real numbers (e.g., INT, SMALLINT, DECIMAL, NUMERIC, FLOAT, REAL).
  • Character types: for text (e.g., CHAR(n), VARCHAR(n), TEXT / CLOB).
  • Date and time types: DATE, TIME, TIMESTAMP (store dates, times, or both).
  • Boolean: TRUE / FALSE (some DBMS use BOOLEAN or TINYINT(1)).
  • Binary / Large Object types: BLOB, BYTEA for images/files.
  • Special types: ENUM/SET (MySQL), UUID, JSON (some DBMS).

Key properties

  • Precision & scale (DECIMAL/NUMERIC): DECIMAL(p,s) stores p total digits where s digits are after decimal point. Use for exact money values.
  • Fixed vs variable length: CHAR(n) is fixed-length (padded), VARCHAR(n) is variable-length up to n characters.
  • NULL vs NOT NULL: Whether a column can have missing (NULL) values.
  • Storage & performance: Smaller and appropriate types improve speed and reduce disk usage.

SQL examples (brief)

CREATE TABLE Students (
  AdmissionNo INT PRIMARY KEY,
  Name VARCHAR(60) NOT NULL,
  DOB DATE,
  Mobile CHAR(10),
  AverageMarks DECIMAL(5,2),   -- total 5 digits, 2 after decimal
  IsPass BOOLEAN,
  Photo BLOB
);

-- Insert example
INSERT INTO Students (AdmissionNo, Name, DOB, Mobile, AverageMarks, IsPass)
VALUES (101, 'Anita Sharma', '2004-08-12', '9876543210', 85.75, TRUE);

Best practices

  • Use INT (or SMALLINT/TINYINT) for counts/IDs; do not use VARCHAR for numbers.
  • Use DECIMAL for money; do not use FLOAT for exact financial calculations.
  • Pick length limits (VARCHAR) that reflect real data to save space.
  • Use DATE/TIMESTAMP for dates and times, not VARCHAR.
  • Avoid NULL if a value is always required; use NOT NULL and defaults.

Understanding and selecting correct data types is fundamental to designing efficient and reliable databases.

📌 Examples
  • Student database: AdmissionNo INT, Name VARCHAR(60), DOB DATE, Mobile CHAR(10), AverageMarks DECIMAL(5,2), IsPass BOOLEAN, Photo BLOB.
  • Bank transactions: TransactionID BIGINT, AccountNo VARCHAR(20), Amount DECIMAL(12,2), TransactionDate TIMESTAMP, Description TEXT.
  • E‑commerce product: ProductID INT, Title VARCHAR(150), Price DECIMAL(8,2), Stock INT, CreatedAt TIMESTAMP, Image BLOB.
  • Attendance log: EntryID INT, StudentID INT, AttDate DATE, AttTime TIME, Status ENUM('Present','Absent','Late').
🧮 Formulas
  1. \[Range for DECIMAL(p,s): values between - (10^(p-s) - 10^(-s)) and + (10^(p-s) - 10^(-s))\]
    \[Example: DECIMAL(5,2) -> -999.99 to 999.99.\]
  2. \[CHAR(n) storage: fixed n bytes (space-padded)\]
    \[VARCHAR(n) storage: up to n bytes plus 1 or 2 bytes for length prefix (depends on DBMS).\]
  3. \[Signed integer range (b bytes): from - (2^(8*b - 1)) to 2^(8*b - 1) - 1\]
    \[Example for 4 bytes (INT): -2,147,483,648 to 2,147,483,647.\]
  4. \[Precision note for floating types: FLOAT and REAL are approximate—use DECIMAL/NUMERIC for exact decimal arithmetic.\]
💻4

Constraints and Keys

💻 COMPUTER SCIENCE / IT

Constraints and Keys

Key Point: CREATE TABLE table_name ( column1 datatype CONSTRAINT constraint_name constraint_type, column2 datatype, ... ); Example: CREATE TABLE T (id INT PRIMARY KEY, x VARCHAR(50) NOT NULL, y INT CHECK (y>=0));

Overview
In a relational database, constraints enforce rules on table data (validity, uniqueness, relationships) and keys identify rows uniquely. Constraints ensure data integrity; keys are special columns or sets of columns used to enforce uniqueness and relationships.

Common Constraints

  • NOT NULL — column cannot contain NULL values.
  • UNIQUE — values in a column (or combination of columns) must be unique across rows.
  • PRIMARY KEY — UNIQUE + NOT NULL; identifies each row uniquely. Can be single-column or composite (multi-column).
  • FOREIGN KEY — enforces referential integrity by linking a column (or columns) in one table to a PRIMARY/UNIQUE key in another table.
  • CHECK — enforces a boolean expression on column values (e.g., salary >= 0).
  • DEFAULT — supplies a default value when none is provided on INSERT.

Key Types and Properties

  • Superkey — any set of columns that uniquely identifies rows (may have extra attributes).
  • Candidate key — a minimal superkey (no proper subset is a superkey). There can be multiple candidate keys.
  • Primary key — one chosen candidate key to identify the row; must be unique and NOT NULL.
  • Alternate key — candidate keys not chosen as primary.
  • Composite (compound) key — a key made of two or more columns.
  • Foreign key — column(s) that reference a primary/unique key in another table; maintains referential integrity.
  • Surrogate key — system-generated key (e.g., auto-increment ID) used as primary key when no natural key is suitable.

Referential Integrity & Cascading
A foreign key maintains relationships: INSERT/UPDATE/DELETE operations that would break the relationship are rejected or handled via actions such as ON DELETE CASCADE, ON UPDATE CASCADE, ON DELETE SET NULL, etc.

When to use which constraint
Use PRIMARY KEY for row identity, UNIQUE for alternate identifiers (email, phone), NOT NULL for required fields, CHECK for business rules (age >= 0), and FOREIGN KEY to link related tables (orders → customers).

How constraints are enforced
Most DBMS enforce constraints at INSERT/UPDATE time. Violations cause an error and the statement is rolled back (unless deferred constraints are used).

Short example summary
A typical small schema: students, courses, enrollments. student_id is PRIMARY KEY in students; course_id is PRIMARY KEY in courses; enrollment uses (student_id, course_id) as a composite PRIMARY KEY and both are FOREIGN KEYs referencing students and courses respectively.

📌 Examples
  • Student–Course enrollment (one student can enroll in many courses): CREATE TABLE Students ( student_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) UNIQUE ); CREATE TABLE Courses ( course_id INT PRIMARY KEY, title VARCHAR(150) NOT NULL ); CREATE TABLE Enrollments ( student_id INT, course_id INT, enrolled_on DATE DEFAULT CURRENT_DATE, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES Students(student_id) ON DELETE CASCADE, FOREIGN KEY (course_id) REFERENCES Courses(course_id) );
  • Bank account example (surrogate PK and CHECK): CREATE TABLE Customers ( cust_id SERIAL PRIMARY KEY, -- surrogate key name VARCHAR(100) NOT NULL, ssn CHAR(9) UNIQUE ); CREATE TABLE Accounts ( acct_no BIGINT PRIMARY KEY, cust_id INT REFERENCES Customers(cust_id), balance DECIMAL(12,2) CHECK (balance >= 0) );
  • E‑commerce order (FK with ON DELETE SET NULL): CREATE TABLE Products ( product_id INT PRIMARY KEY, name VARCHAR(200) NOT NULL ); CREATE TABLE Orders ( order_id INT PRIMARY KEY, product_id INT REFERENCES Products(product_id) ON DELETE SET NULL, qty INT CHECK (qty > 0) );
🧮 Formulas
  1. \[CREATE TABLE table_name ( column1 datatype CONSTRAINT constraint_name constraint_type\]
    \[column2 datatype, ... )\]
    \[Example: CREATE TABLE T (id INT PRIMARY KEY\]
    \[x VARCHAR(50) NOT NULL\]
    \[y INT CHECK (y>=0))\]
  2. \[ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY (col) REFERENCES other_table(col) ON DELETE CASCADE;\]
  3. \[Functional dependency notation: A -> B (A functionally determines B)\]
    \[Keys are minimal attribute sets A such that A -> all attributes of the relation.\]
  4. \[Key set relations: Superkey ⊇ CandidateKey ⊇ PrimaryKey\]
    \[AlternateKey = CandidateKey - PrimaryKey\]
  5. \[Composite PK syntax: PRIMARY KEY (col1\]
    \[col2, ...)\]
💻5

SELECT Statement Basics

💻 COMPUTER SCIENCE / IT

SELECT Statement Basics

Key Point: Basic select template: SELECT column1, column2 FROM table_name WHERE condition;

The SELECT statement is the primary SQL command to retrieve data from one or more database tables. It specifies which columns or expressions to return and from which table(s). SELECT can also filter rows, remove duplicates, rename columns, sort results, and compute simple expressions.

Basic syntax:

SELECT <column_list or expressions>
FROM <table_name>
[WHERE <condition>]
[GROUP BY <columns>]
[HAVING <group_condition>]
[ORDER BY <columns> [ASC|DESC]]
[LIMIT <number>];

Key parts explained:

  • SELECT — columns, expressions, or * (all columns). You may use arithmetic expressions (salary * 12) or functions (COUNT(), AVG()).
  • FROM — table(s) to read. Can include joins for multiple tables.
  • WHERE — row-level filter using comparison operators (=, <>, <, >, <=, >=), logical operators (AND, OR, NOT), IN, BETWEEN, LIKE, IS NULL.
  • DISTINCT — removes duplicate rows for the selected columns.
  • AS — gives an alias to a column or expression (SELECT name AS student_name).
  • ORDER BY — sorts results by one or more columns (ascending is default).
  • GROUP BY and HAVING — group rows to apply aggregate functions (SUM, COUNT, AVG, MIN, MAX); HAVING filters groups.
  • Execution order (conceptual): FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT.

Tips: Use WHERE to filter rows before aggregation. Use DISTINCT when you only want unique values. Use aliases to make output readable. Use LIMIT to show only top N rows (or TOP in some SQL dialects).

📌 Examples
  • Example 1 — Student marks: Retrieve names and marks of students who scored above 75: SQL: SELECT name, marks FROM students WHERE marks > 75 ORDER BY marks DESC; Result: Rows of student names and marks sorted by marks (highest first).
  • Example 2 — Remove duplicates: List all distinct cities from a student table: SQL: SELECT DISTINCT city FROM students; Result: One row per city with duplicates removed.
  • Example 3 — Column alias and expression: Calculate annual salary from monthly salary: SQL: SELECT emp_id, name, salary_monthly, (salary_monthly * 12) AS salary_annual FROM employees; Result: Shows employee id, name, monthly salary and computed annual salary under column 'salary_annual'.
  • Example 4 — Aggregate with GROUP BY: Count books in each category (library): SQL: SELECT category, COUNT(*) AS num_books FROM books GROUP BY category ORDER BY num_books DESC; Result: Each category with total books, sorted by count.
  • Example 5 — Pattern match: Find product names starting with 'Pro': SQL: SELECT product_id, product_name FROM products WHERE product_name LIKE 'Pro%'; Result: Products whose names begin with 'Pro'.
🧮 Formulas
  1. \[Basic select template: SELECT column1\]
    \[column2 FROM table_name WHERE condition\]
  2. \[Select all columns: SELECT * FROM table_name;\]
  3. \[Distinct: SELECT DISTINCT column FROM table_name;\]
  4. \[Alias: SELECT column AS alias_name FROM table_name;\]
  5. \[Order: SELECT columns FROM table ORDER BY column1 ASC\]
    \[column2 DESC\]
  6. \[Limit (MySQL/Postgres): SELECT columns FROM table LIMIT n\]
    \[(SQL Server uses TOP n: SELECT TOP n ...)\]
🔶6

Operators, Pattern Matching and NULL Handling

💻 COMPUTER SCIENCE / IT

Operators, Pattern Matching and NULL Handling

Key Point: Comparison: column operator value — e.g. salary >= 30000

Overview

This topic covers how SQL uses operators to perform comparisons, arithmetic and logical tests; how to search text using pattern matching; and how NULL values (unknown/missing data) behave and must be handled safely in queries.

1. Operators

  • Arithmetic: +, -, *, /, % (modulo). Used in SELECT and UPDATE to compute values. Example: salary * 1.10.
  • Comparison: =, <>, !=, <, <=, >, >=. Compare column values in WHERE or HAVING. Example: marks >= 40.
  • Logical (Boolean): AND, OR, NOT. Combine conditions. SQL uses three-valued logic (TRUE, FALSE, UNKNOWN) when NULLs are involved.
  • Membership & range: IN (…), BETWEEN a AND b.
  • Null tests: IS NULL, IS NOT NULL (do not use = NULL).

2. Pattern Matching

Pattern matching is used to test strings. The most common tool is the LIKE operator with wildcards:

  • '%' matches zero or more characters. Example: name LIKE 'A%' finds names starting with A.
  • '_' matches exactly one character. Example: code LIKE 'AB_1' matches 'ABX1'.
  • Some DBMS support bracket expressions (SQL Server) or regular expressions (REGEXP in MySQL, ~ in PostgreSQL) for advanced patterns.
  • Use ESCAPE to search for literal '%' or '_' characters.

3. NULL Handling

  • Meaning: NULL represents unknown or missing data. NULL is not the same as an empty string ('') or zero.
  • Comparisons: Any comparison with NULL returns UNKNOWN (not TRUE). Use IS NULL or IS NOT NULL to test for NULL.
  • Three-valued logic: Conditions evaluate to TRUE, FALSE or UNKNOWN. In a WHERE clause, only TRUE rows are returned; UNKNOWN acts like FALSE.
  • Aggregation: Aggregate functions (SUM, AVG, MAX, MIN) ignore NULLs. COUNT(column) counts non-NULLs; COUNT(*) counts rows including NULL values in columns.
  • Null-substitution functions: Use COALESCE(col, value) (standard), IFNULL(col, value) (MySQL), or NVL(col, value) (Oracle) to replace NULLs with default values. NULLIF(expr1, expr2) returns NULL if expressions are equal.

Rules & Common Pitfalls

  • Avoid writing WHERE col = NULL or col <> NULL — they are invalid for NULL tests.
  • Be careful with logical expressions: WHERE salary > 1000 OR salary IS NULL explicitly includes missing salaries.
  • When computing with possible NULLs, substitute defaults: COALESCE(bonus,0) so arithmetic works as expected.
  • When filtering with IN or BETWEEN, NULLs are excluded unless tested explicitly.

Example behaviour

-- Wrong: returns no rows for NULLs
SELECT * FROM students WHERE middle_name = NULL;

-- Correct: test NULL explicitly
SELECT * FROM students WHERE middle_name IS NULL;

-- Replace NULL with 0 when summing
SELECT SUM(COALESCE(extra_marks,0)) FROM exams;

-- Pattern matching
SELECT * FROM products WHERE description LIKE '%wireless%';

Summary: Use the correct operators for arithmetic, comparison and logic; use LIKE/REGEXP for string patterns; and always handle NULLs explicitly (IS NULL / COALESCE) to avoid unexpected results.

📌 Examples
  • Find employees with salary between 20000 and 50000: SELECT * FROM employees WHERE salary BETWEEN 20000 AND 50000;
  • Find students whose name starts with 'A': SELECT * FROM students WHERE name LIKE 'A%';
  • Find products containing 'phone' anywhere in description: SELECT * FROM products WHERE description LIKE '%phone%';
  • Count students with missing marks (NULL): SELECT COUNT(*) AS total_missing FROM marks WHERE score IS NULL;
  • Replace NULL middle names with 'N/A' in output: SELECT id, COALESCE(middle_name, 'N/A') AS middle_name FROM students;
  • Calculate total pay ignoring NULL bonuses: SELECT id, salary + COALESCE(bonus,0) AS total_pay FROM payroll;
🧮 Formulas
  1. \[Comparison: column operator value — e.g. salary >= 30000\]
  2. \[Range: column BETWEEN low AND high — e.g. date BETWEEN '2024-01-01' AND '2024-12-31'\]
  3. \[Set membership: column IN (v1\]
    \[v2, ...) — e.g. dept IN ('HR','IT')\]
  4. \[Pattern match: column LIKE 'pattern' — wildcards: % (many), _ (one)\]
  5. \[NULL test: column IS NULL / column IS NOT NULL\]
  6. \[Null-substitution: COALESCE(column\]
    \[default) (also IFNULL / NVL in some DBs)\]
💻7

Aggregate Functions and Grouping

📐 MATHEMATICAL FORMULA / THEOREM

Aggregate Functions and Grouping

Key Point: COUNT(column) — counts non-NULL values in column. Example: COUNT(emp_id).

Overview

Aggregate functions compute a single value from a set of rows. They are used with SQL SELECT to produce summaries such as totals, averages, counts, minima and maxima. When you want summaries per category (for example, average marks per class), you use GROUP BY. To filter groups based on aggregate values (for example, departments with average salary > 50,000) you use HAVING.

Common aggregate functions

  • COUNT(expr) — counts rows; COUNT(*) counts all rows; COUNT(DISTINCT col) counts distinct non-NULL values.
  • SUM(expr) — sum of numeric values (NULLs ignored).
  • AVG(expr) — average of numeric values (NULLs ignored).
  • MIN(expr) — minimum value.
  • MAX(expr) — maximum value.

GROUP BY

GROUP BY groups rows that have the same values in specified columns so aggregate functions can be applied per group. Every column in SELECT that is not an aggregate must appear in GROUP BY.

HAVING vs WHERE

  • WHERE filters rows before aggregation (cannot use aggregates).
  • HAVING filters groups after aggregation (can use aggregates).

NULL handling

Aggregate functions (except COUNT(*)) ignore NULL values. So AVG(salary) computes average of non-NULL salaries; COUNT(emp_id) ignores NULL emp_id values.

Example patterns

-- total sales per product
SELECT product_id, SUM(amount) AS total_sales
FROM sales
GROUP BY product_id;

-- average marks per class and section, only classes with avg >= 60
SELECT class, section, AVG(marks) AS avg_marks
FROM student_marks
GROUP BY class, section
HAVING AVG(marks) >= 60;

-- count of employees per department (including departments with zero employees not returned)
SELECT dept_id, COUNT(*) AS emp_count
FROM employees
GROUP BY dept_id;

-- distinct customers count
SELECT COUNT(DISTINCT customer_id) AS unique_customers
FROM orders;

Good practices

  • Include non-aggregated selected columns in GROUP BY.
  • Use meaningful aliases (AS) for aggregated columns.
  • Use HAVING for group-level conditions and WHERE for row-level filters.
📌 Examples
  • School marks: Find average marks per class. SQL: SELECT class, AVG(marks) AS avg_marks FROM student_marks GROUP BY class; Real life: Helps identify classes that need extra coaching.
  • Retail sales: Total sales per product and top-selling products. SQL: SELECT product_name, SUM(sales_amount) AS total_sales FROM sales GROUP BY product_name ORDER BY total_sales DESC; Real life: Inventory and replenishment decisions.
  • Employees: Maximum salary and number of employees per department. SQL: SELECT dept_name, MAX(salary) AS highest_salary, COUNT(*) AS emp_count FROM employees GROUP BY dept_name HAVING COUNT(*) &gt; 5; -- only departments with more than 5 employees Real life: Budgeting and pay-structure analysis.
  • Hospital: Number of patients per disease category. SQL: SELECT disease, COUNT(*) AS patient_count FROM patients GROUP BY disease ORDER BY patient_count DESC; Real life: Resource allocation and epidemic tracking.
🧮 Formulas
  1. \[COUNT(column) — counts non-NULL values in column\]
    \[Example: COUNT(emp_id).\]
  2. \[COUNT(*) — counts all rows (including NULL columns).\]
  3. \[COUNT(DISTINCT column) — counts distinct non-NULL values.\]
  4. \[SUM(column) — sum of numeric values\]
    \[Example: SUM(amount).\]
  5. \[AVG(column) — average of numeric values\]
    \[AVG = SUM(column) / COUNT(column) (excluding NULLs).\]
  6. \[MIN(column) — minimum value in the group.\]
💻8

Joins and Combining Tables

💻 COMPUTER SCIENCE / IT

Joins and Combining Tables

Key Point: Basic join template: SELECT columns FROM tableA JOIN_TYPE tableB ON tableA.col = tableB.col;

Combining data from two or more tables is a fundamental part of SQL. There are two broad ways to combine tables:

  • Joins: combine rows from tables based on a related column (relationship-based). Typical joins: inner, left outer, right outer, full outer, cross, self, natural and non-equi (theta) joins.
  • Set operators: combine results of two SELECT queries as sets (UNION, UNION ALL, INTERSECT, MINUS/EXCEPT).

Key points:

  • An INNER JOIN returns only rows that match in both tables (intersection of rows based on condition).
  • A LEFT OUTER JOIN returns all rows from the left table and matched rows from the right table; unmatched right columns are NULL.
  • A RIGHT OUTER JOIN is symmetric to LEFT: all rows from right and matching left rows.
  • A FULL OUTER JOIN returns rows that match plus unmatched rows from both tables (may not be supported in all DBMS).
  • A CROSS JOIN returns Cartesian product: every row of A paired with every row of B (useful for combinations).
  • A SELF JOIN joins a table to itself (useful for hierarchical data like employee-manager).
  • A NATURAL JOIN automatically joins on all columns with the same names (use cautiously).
  • UNION combines rows of two queries and removes duplicates; UNION ALL keeps duplicates. INTERSECT returns common rows; MINUS/EXCEPT returns rows in first query but not in second.

Use join condition(s) in the ON clause (or USING). Without a condition, JOIN becomes CROSS JOIN (Cartesian product). For performance, always join on indexed columns when possible and avoid unnecessarily large Cartesian products.

Example conceptual flow for a join:

SELECT columns
FROM tableA
JOIN_TYPE tableB
ON tableA.key = tableB.key
WHERE conditions;
📌 Examples
  • Inner join (students and marks): SELECT s.roll_no, s.name, m.subject, m.marks FROM students s INNER JOIN marks m ON s.roll_no = m.roll_no; -- returns only students who have marks records.
  • Left outer join (employees and departments): SELECT e.emp_id, e.name, d.dept_name FROM employee e LEFT JOIN department d ON e.dept_id = d.dept_id; -- returns all employees; dept_name is NULL if no department assigned.
  • Right outer join (customers and orders): SELECT c.cust_id, c.name, o.order_id FROM customers c RIGHT JOIN orders o ON c.cust_id = o.cust_id; -- returns all orders; customer info NULL if missing (useful when analysing orders including guest orders).
  • Full outer join (products and suppliers) — if DB supports it: SELECT p.product_id, p.name, s.supplier_id, s.name FROM products p FULL OUTER JOIN suppliers s ON p.supplier_id = s.supplier_id; -- returns all products and all suppliers, matched where supplier_id equals.
  • Cross join (sizes and colors combinations): SELECT p.product_name, c.color FROM products p CROSS JOIN colors c; -- produces every combination of product and color (useful for inventory combinations).
  • Self join (employee-manager hierarchy): SELECT e.emp_id, e.name AS employee, m.emp_id AS manager_id, m.name AS manager FROM employee e LEFT JOIN employee m ON e.manager_id = m.emp_id; -- lists each employee with their manager's name (NULL if top-level).
🧮 Formulas
  1. \[Basic join template: SELECT columns FROM tableA JOIN_TYPE tableB ON tableA.col = tableB.col;\]
  2. \[Inner join (only matches): SELECT ..\]
    \[FROM A INNER JOIN B ON A.k = B.k\]
  3. \[Left outer join (all left rows): SELECT ..\]
    \[FROM A LEFT JOIN B ON A.k = B.k\]
  4. \[Right outer join (all right rows): SELECT ..\]
    \[FROM A RIGHT JOIN B ON A.k = B.k\]
  5. \[Full outer join (both sides all rows): SELECT ..\]
    \[FROM A FULL OUTER JOIN B ON A.k = B.k\]
  6. \[Cross join (Cartesian product): SELECT ..\]
    \[FROM A CROSS JOIN B\]
    \[-- or FROM A\]
    \[B\]
💻9

Subqueries and Nested Queries

💻 COMPUTER SCIENCE / IT

Subqueries and Nested Queries

Key Point: Basic WHERE IN (column list): WHERE column IN (SELECT col FROM table WHERE condition)

What is a subquery (nested query)?

A subquery (or nested query) is a SQL query placed inside another SQL query. The outer query uses results returned by the inner query. Subqueries let you break complex problems into smaller queries and express filtering, comparison and aggregation conditions compactly.

Where can you use subqueries? Subqueries can appear in the SELECT list, FROM clause (as a derived table), WHERE clause, and HAVING clause.

Types of subqueries

  • Scalar subquery: returns a single value (one row, one column). Can be used wherever a single value is allowed (e.g., SELECT list, WHERE comparison).
  • Row subquery: returns a single row with multiple columns; used with row-wise comparisons.
  • Column (single-column) subquery: returns a list/column of values; commonly used with IN.
  • Table subquery / Derived table: returns a full table result and appears in FROM (needs an alias).

Correlated vs Non-correlated subqueries

  • Non-correlated: inner query runs once and provides a result set for the outer query. Example: WHERE dept_id IN (SELECT id FROM dept WHERE location = 'X')
  • Correlated: inner query references columns from the outer query; it is re-evaluated for each outer row. Example: WHERE salary > (SELECT AVG(salary) FROM emp e2 WHERE e2.dept = e1.dept)

Common operators with subqueries: IN, NOT IN, EXISTS, NOT EXISTS, =, >>, ANY / SOME, ALL. Use EXISTS for existence checks (fast with indexes); use IN when matching against a known list.

Execution order: For non-correlated subqueries the inner query executes first and its result feeds the outer query. For correlated subqueries the inner query depends on the current outer row and runs repeatedly (logically inner runs per outer row).

Best practices & notes

  • Prefer joins or derived tables for large datasets when appropriate — they often perform better than correlated subqueries.
  • Use EXISTS when testing for presence/absence; it stops on first match.
  • Ensure scalar subqueries return exactly one value; otherwise SQL errors occur (or unexpected behavior with IN/EXISTS differences).
  • Alias derived tables: FROM (subquery) AS t.

Short example (non-correlated):

SELECT name FROM Student
WHERE marks > (SELECT AVG(marks) FROM Student WHERE class = '12A');

Short example (correlated):

SELECT e.emp_name
FROM Employee e
WHERE e.salary > (SELECT AVG(salary) FROM Employee e2 WHERE e2.dept_id = e.dept_id);

Subqueries are a powerful tool in SQL for conditional logic, filtering, aggregation-based comparisons and creating temporary result sets.

📌 Examples
  • Find customers who ordered any product priced above the current average price: SELECT DISTINCT c.customer_id, c.name FROM Customers c JOIN Orders o ON c.customer_id = o.customer_id WHERE o.product_id IN (SELECT p.product_id FROM Products p WHERE p.price &gt; (SELECT AVG(price) FROM Products));
  • Employees earning more than their department average (correlated subquery): SELECT e.emp_id, e.name, e.salary FROM Employee e WHERE e.salary &gt; (SELECT AVG(salary) FROM Employee e2 WHERE e2.dept_id = e.dept_id);
  • Scalar subquery in SELECT to show each student's difference from class average: SELECT s.student_id, s.name, s.marks, s.marks - (SELECT AVG(marks) FROM Student WHERE class = s.class) AS diff_from_class_avg FROM Student s;
  • Derived table (subquery in FROM) to get top 3 products by sales and then filter: SELECT t.product_id, t.total_sales FROM ( SELECT product_id, SUM(quantity*price) AS total_sales FROM Sales GROUP BY product_id ) AS t WHERE t.total_sales &gt; 10000;
  • Use EXISTS to find suppliers without orders: SELECT s.supplier_id, s.name FROM Suppliers s WHERE NOT EXISTS ( SELECT 1 FROM Orders o WHERE o.supplier_id = s.supplier_id );
  • Using ALL / ANY: find products cheaper than every product in a competitor's list (ALL) or cheaper than some product (ANY): -- cheaper than all competitor prices SELECT product_id FROM Products p WHERE price &lt; ALL (SELECT price FROM CompetitorProducts); -- cheaper than at least one competitor product SELECT product_id FROM Products p WHERE price &lt; ANY (SELECT price FROM CompetitorProducts);
🧮 Formulas
  1. \[Basic WHERE IN (column list): WHERE column IN (SELECT col FROM table WHERE condition)\]
  2. \[Scalar subquery (single value): (SELECT AVG(col) FROM table WHERE condition)\]
  3. \[Correlated subquery template: WHERE outer.col OP (SELECT AGG(inner.col) FROM table inner WHERE inner.link = outer.link)\]
  4. \[EXISTS template: WHERE EXISTS (SELECT 1 FROM table t WHERE t.col = outer.col AND condition)\]
  5. \[Derived table template: FROM (SELECT ..\]
    \[FROM ..\]
    \[WHERE ...) AS alias\]
  6. \[ANY / SOME and ALL: value > ANY (subquery) -- true if value greater than at least one returned value &gt\]
    \[ALL (subquery) -- true if value greater than every returned Note: subquery must return a single column for ANY/ALL/IN.\]
⚖️10

Set Operations

💻 COMPUTER SCIENCE / IT

Set Operations

Key Point: Basic syntax: SELECT cols FROM table1 UNION | UNION ALL | INTERSECT | EXCEPT|MINUS SELECT cols FROM table2;

What are set operations? In SQL, set operations combine the result sets of two or more SELECT queries into a single result. They are based on set theory and let you perform union, intersection and difference of rows returned by queries.

  • Common set operations:
    • UNION — returns distinct rows that appear in either or both queries (duplicates removed).
    • UNION ALL — returns all rows from both queries including duplicates.
    • INTERSECT — returns rows common to both queries.
    • MINUS (Oracle) / EXCEPT (standard SQL) — returns rows in the first query that are not in the second.
  • Rules and requirements
    • Each SELECT must return the same number of columns.
    • Corresponding columns must have compatible data types (or be implicitly convertible).
    • An ORDER BY applies to the final combined result and should appear once after the last SELECT (or use parentheses to order partial results before combining).
    • Column names in the result are taken from the first SELECT in many SQL dialects. Use aliases in the first SELECT if you want specific column names.
  • Duplicates and NULLs
    • UNION removes duplicate rows; UNION ALL preserves them.
    • NULL values are considered equal for the purpose of deduplication (two rows with NULL in the same position are treated as equal when deciding duplicates).
  • Precedence & grouping
    • Use parentheses to control the order in which set operations are applied. Example: (A UNION B) INTERSECT C.
  • Performance notes
    • UNION ALL is usually faster than UNION because it does not require duplicate elimination (no sort/unique step).
    • Indexes and careful filtering in each SELECT help performance; avoid returning unneeded columns or rows before combining.

When to use each operation (short guidance)

  • UNION — merge two lists but remove repeats (e.g., compile a master list of emails from two sources).
  • UNION ALL — concatenate results and keep duplicates (e.g., keep repeated transaction rows from two sources to preserve counts).
  • INTERSECT — find common items (e.g., students enrolled in both courses).
  • MINUS / EXCEPT — find items in A not in B (e.g., products in catalog A not yet published in catalog B).
📌 Examples
  • Example 1 - UNION (distinct): SELECT student_name FROM Students_Math UNION SELECT student_name FROM Students_Physics -- Returns all student names who took Math or Physics, duplicates removed.
  • Example 2 - UNION ALL (keep duplicates): SELECT customer_id FROM Online_Orders UNION ALL SELECT customer_id FROM Store_Orders -- Returns all customer_id rows from both sources, duplicates retained (useful when counting total orders).
  • Example 3 - INTERSECT (common rows): SELECT employee_id FROM Project_A_Team INTERSECT SELECT employee_id FROM Project_B_Team -- Returns employees who are on both Project A and Project B.
  • Example 4 - MINUS / EXCEPT (difference): -- Oracle syntax (MINUS) SELECT product_id FROM Master_Catalog MINUS SELECT product_id FROM Discontinued_Products; -- Standard SQL (EXCEPT) SELECT product_id FROM Master_Catalog EXCEPT SELECT product_id FROM Discontinued_Products; -- Returns products in the master catalog that are not discontinued.
  • Example 5 - Using ORDER BY after set operation: SELECT name, city FROM Customers_A UNION SELECT name, city FROM Customers_B ORDER BY name; -- ORDER BY applies to the combined result. Column names come from first SELECT.
  • Example 6 - Parentheses to control order: (SELECT id FROM A UNION SELECT id FROM B) INTERSECT SELECT id FROM C; -- The UNION is performed first, then intersected with C.
🧮 Formulas
  1. \[Basic syntax: SELECT cols FROM table1 UNION | UNION ALL | INTERSECT | EXCEPT|MINUS SELECT cols FROM table2;\]
  2. \[Column and type rule: number_of_columns(select1) = number_of_columns(select2)\]
    \[and types must be compatible.\]
  3. \[UNION removes duplicates: Result = distinct(rows(select1) ∪ rows(select2)).\]
  4. \[UNION ALL keeps duplicates: Result = rows(select1) + rows(select2) (multiset union).\]
  5. \[INTERSECT: Result = rows(select1) ∩ rows(select2).\]
  6. \[MINUS / EXCEPT: Result = rows(select1) \ rows(select2).\]
💻11

SQL Functions (Built-in)

📐 MATHEMATICAL FORMULA / THEOREM

SQL Functions (Built-in)

Key Point: Aggregate basic: SELECT AGG_FUNC(column) FROM table [WHERE condition] [GROUP BY column_list] [HAVING aggregate_condition]; -- AGG_FUNC = COUNT, SUM, AVG, MIN, MAX

Overview: Built-in SQL functions are pre-defined operations that perform computations on data values and return a single value. They fall into two broad categories: aggregate (group) functions that operate on sets of rows and return a summary value, and scalar (single-row) functions that operate on individual column values.

Categories and common functions:

  • Aggregate functions — work on groups or whole tables: COUNT(), SUM(), AVG(), MIN(), MAX(). Often used with GROUP BY and HAVING.
  • Numeric/scalar functions — operate on numeric values: ROUND(), CEIL(), FLOOR(), ABS().
  • String/text functions — manipulate text: CONCAT(), SUBSTR() or SUBSTRING(), LENGTH(), UPPER(), LOWER(), TRIM(), REPLACE().
  • Date/time functions — handle dates and times: CURDATE()/CURRENT_DATE, NOW(), DATE_ADD(), DATE_SUB(), DATEDIFF(), YEAR(), MONTH(), DAY().
  • Conversion and conditional — change types or handle NULLs: CAST(... AS ...), CONVERT(), COALESCE() (first non-NULL), NULLIF().

NULL behavior: Many functions ignore NULLs in aggregates (e.g., SUM() skips NULL rows). Use COALESCE(column, default) to substitute values for NULL.

Usage patterns:

  • Aggregates with grouping: SELECT dept, AVG(salary) FROM Employee GROUP BY dept;
  • Filter groups: HAVING applies after grouping (e.g., departments with average salary > 50000)
  • Scalar functions in SELECT or WHERE to transform or filter values (e.g., WHERE UPPER(name) = 'RAHUL')

Best practices:

  • Use indexes for columns used in WHERE to improve performance; applying functions to indexed columns in WHERE can prevent index usage (e.g., avoid WHERE UPPER(col) = 'X' if possible).
  • Use HAVING only for filtering aggregated results; prefer WHERE for row-level filtering before grouping.
  • Be explicit with CAST when combining different types (dates and strings).

Example brief summary: Built-in SQL functions let you compute totals, averages, manipulate strings, work with dates, handle NULLs, and convert data types — all essential for data reporting and transformation in real-life applications such as sales reporting, student marks analysis, payroll, and inventory management.

📌 Examples
  • School (marks): Calculate each student's average and find toppers per class SQL: SELECT class, student_name, AVG(marks) AS avg_marks FROM Marks GROUP BY class, student_name ORDER BY class, avg_marks DESC;
  • E-commerce (sales summary): Monthly sales total per product SQL: SELECT product_id, MONTH(sale_date) AS month, SUM(quantity * price) AS revenue FROM Sales WHERE YEAR(sale_date) = 2025 GROUP BY product_id, MONTH(sale_date);
  • Library (string/date): Find overdue books and days overdue SQL: SELECT member_id, book_id, DATEDIFF(CURDATE(), due_date) AS days_overdue FROM Loans WHERE returned = 'N' AND due_date < CURDATE();
  • Employees (NULL handling & aggregation): Average salary by department, ignoring NULL salaries, and include only departments with > 5 employees SQL: SELECT dept, AVG(COALESCE(salary,0)) AS avg_salary, COUNT(*) AS num_emp FROM Employees GROUP BY dept HAVING COUNT(*) > 5;
  • Customer names (string functions): Standardize and search case-insensitively SQL: SELECT customer_id, CONCAT(UPPER(LEFT(first_name,1)), LOWER(SUBSTR(first_name,2))) AS formatted_name FROM Customers WHERE LOWER(last_name) = 'gupta';
🧮 Formulas
  1. \[Aggregate basic: SELECT AGG_FUNC(column) FROM table [WHERE condition] [GROUP BY column_list] [HAVING aggregate_condition]\]
    \[-- AGG_FUNC = COUNT\]
    \[SUM\]
    \[AVG\]
    \[MIN\]
    \[MAX\]
  2. \[Count distinct values: SELECT COUNT(DISTINCT column) FROM table;\]
  3. \[Numeric rounding: SELECT ROUND(number\]
    \[decimals) FROM table\]
    \[-- decimals default 0 if omitted\]
  4. \[String extract: SELECT SUBSTR(column\]
    \[start_position\]
    \[length) FROM table\]
    \[-- or SUBSTRING(...)\]
  5. \[Date difference: SELECT DATEDIFF(date1\]
    \[date2) FROM table\]
    \[-- returns days between dates\]
  6. \[Add/subtract date: SELECT DATE_ADD(date_column\]
    \[INTERVAL n DAY) FROM table\]
    \[SELECT DATE_SUB(date_column\]
    \[INTERVAL n MONTH) FROM table\]
💻12

Transactions and Concurrency (Basics)

💻 COMPUTER SCIENCE / IT

Transactions and Concurrency (Basics)

Key Point: Conflict condition: Two operations conflict if they access the same data item and at least one is a write. (Read-Write, Write-Read, Write-Write are conflicts.)

What is a transaction?
A transaction is a logical unit of work performed on a database. It is a sequence of one or more SQL operations (SELECT/INSERT/UPDATE/DELETE) that must be executed as a single unit so that the database moves from one consistent state to another.

Why transactions?
To ensure correctness when multiple operations are needed to accomplish a task (for example, transferring money between bank accounts), and to handle failures and concurrent access safely.

ACID properties

  • Atomicity — All operations in a transaction succeed or none do. If any operation fails, the transaction is rolled back.
  • Consistency — A transaction transforms the database from one valid state into another, respecting all integrity constraints.
  • Isolation — Concurrent transactions should not interfere with each other; intermediate states of a transaction are invisible to others.
  • Durability — Once a transaction commits, its changes are permanent even if the system crashes.

Transaction states

  • Active: Transaction has started and is executing.
  • Partially committed: Final statement executed, but commit not yet complete.
  • Committed: All changes are permanently recorded.
  • Failed: An error occurred; transaction can no longer proceed.
  • Aborted/Rolled back: Changes undone; transaction terminated without commit.

Basic SQL control commands
START TRANSACTION; / BEGIN TRANSACTION;
COMMIT; — make changes permanent
ROLLBACK; — undo changes
SAVEPOINT name; — create a save point inside a transaction
ROLLBACK TO SAVEPOINT name;

Concurrency and why it is needed
Databases serve many users/processes at the same time. Concurrency allows multiple transactions to run in overlapping time periods to improve throughput and resource usage. Without control, concurrent execution can produce incorrect results.

Common concurrency problems (anomalies)

  • Lost update: Two transactions read the same item and update it; one update overwrites the other.
  • Dirty read: A transaction reads data written by another transaction that has not yet committed (and may later be rolled back).
  • Non-repeatable read: A transaction reads the same item twice and gets different values because another committed transaction modified it in-between.
  • Phantom read: A transaction re-executes a query returning a set of rows and finds additional rows (phantoms) inserted by another committed transaction.

Isolation levels (practical trade-offs)
Different DBMSs provide levels of isolation that balance correctness and performance. Typical levels (from weakest to strongest):

  • READ UNCOMMITTED — allows dirty reads
  • READ COMMITTED — prevents dirty reads, but allows non-repeatable reads and phantoms
  • REPEATABLE READ — prevents dirty and non-repeatable reads, may still allow phantoms
  • SERIALIZABLE — highest isolation; transactions appear to run one after another (no anomalies)

Concurrency control techniques (overview)

  • Locking: Prevent conflicting accesses using locks. Two main lock types: shared (S) for read and exclusive (X) for write.
  • Two-Phase Locking (2PL): Each transaction has a growing phase (acquires locks) and a shrinking phase (releases locks). Strict 2PL holds all write locks until commit to ensure recoverability.
  • Timestamp ordering: Each transaction gets a timestamp; ordering of read/write operations follows timestamps to ensure serializability.
  • Optimistic concurrency control: Execute without locks, validate at commit; if conflict detected, roll back one transaction.

Serializability (correctness criterion)
A concurrent schedule (interleaving of operations) is correct if it is serializable — i.e., its effect is equivalent to some serial execution of the same transactions. A common test: conflict serializability. Build a precedence (conflict) graph where nodes are transactions and directed edges show a conflict (Ti's operation before Tj on the same data with at least one write). If the graph is acyclic, the schedule is conflict-serializable.

Recoverability and cascading aborts

  • A schedule is recoverable if whenever a transaction Tj reads a value written by Ti, Ti commits before Tj commits. This avoids committing based on uncommitted data that may later be rolled back.
  • Cascading aborts occur when an aborted transaction forces other transactions that read its uncommitted data to also abort. Casadeless schedules avoid this by making transactions read only committed data.

Practical tips for students

  • Always use transactions for multi-step changes (e.g., banking transfers): START, do updates, then COMMIT.
  • Use appropriate isolation level: higher isolation prevents anomalies but may reduce concurrency.
  • When debugging concurrency anomalies, draw timelines of operations to spot interleavings causing problems.

📌 Examples
  • Bank transfer: To move Rs. 10,000 from A to B, two updates are needed: debit A and credit B. Wrap them in a single transaction. If the system crashes after debiting A but before crediting B, rollback ensures money is not lost.
  • Online ticket booking: Two users attempt to book the last seat simultaneously. Concurrency control (locks or atomic reserve) ensures only one succeeds and prevents double booking.
  • Inventory update in e-commerce: Two parallel orders decrement stock. Without proper locking, stock can become negative or oversold. Transactions ensure stock consistency.
  • Shopping cart checkout: Multiple steps (check price, apply discount, reduce stock, create order). If any step fails, ROLLBACK restores consistent state.
  • Exam grading updates: Two graders updating the same student record must use transactions to avoid overwriting each other's marks (lost update).
🧮 Formulas
  1. \[Conflict condition: Two operations conflict if they access the same data item and at least one is a write. (Read-Write\]
    \[Write-Read\]
    \[Write-Write are conflicts.)\]
  2. \[Precedence (conflict) graph rule: Schedule is conflict-serializable iff the conflict graph is acyclic.\]
  3. \[Timestamp ordering rule (basic idea): For transactions Ti (TS(i)) and Tj (TS(j))\]
    \[if TS(i) < TS(j) then Ti's operations should appear before Tj's operations on each data item\]
    \[Violation => abort one transaction.\]
  4. \[Recoverability requirement: If Tj reads a value written by Ti\]
    \[then commit(Ti) must occur before commit(Tj).\]
  5. \[Lock compatibility matrix (conceptual): Shared (S) vs Shared (S) -> compatible\]
    \[Shared (S) vs Exclusive (X) -> incompatible\]
    \[Exclusive (X) vs Exclusive (X) -> incompatible.\]
  6. \[Two-Phase Locking (2PL) property: Growing phase (acquire locks) then Shrinking phase (release locks)\]
    \[Strict 2PL: hold all exclusive locks until commit to ensure serializability and recoverability.\]
💻13

Advanced Query Constructs

💻 COMPUTER SCIENCE / IT

Advanced Query Constructs

Key Point: Basic SELECT structure: SELECT FROM

[WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ];

Overview: Advanced query constructs extend basic SELECT/WHERE queries to perform grouping, joining, combining result sets, nested logic, conditional calculations and analytical computations. These constructs let you answer business questions (e.g., top customers, monthly trends, exceptions) efficiently.

Major constructs:

  • Aggregate with GROUP BY and HAVING: use aggregate functions (SUM, COUNT, AVG, MIN, MAX) to compute summaries per group. HAVING filters groups after aggregation (WHERE cannot filter aggregated values).
  • Joins: combine rows from two or more tables using relationships: INNER JOIN (matching rows), LEFT/RIGHT/FULL OUTER JOIN (preserve non-matching rows), CROSS JOIN (Cartesian product), SELF JOIN (table joined to itself).
  • Subqueries (nested queries): queries inside queries. Non-correlated subqueries run independently; correlated subqueries reference outer query columns and run per outer row.
  • Existence and membership tests: IN / NOT IN for set membership; EXISTS / NOT EXISTS for existence of rows returned by a subquery; ANY/ALL for comparisons with sets.
  • Set operations: UNION / UNION ALL (concatenate result sets), INTERSECT (common rows), MINUS/EXCEPT (rows in first not in second). Useful for combining similar result sets from different sources.
  • Conditional expressions: CASE ... WHEN ... THEN ... ELSE ... END lets you compute conditional columns inside SELECT.
  • Window (analytic) functions (advanced): functions like ROW_NUMBER(), RANK(), DENSE_RANK(), SUM() OVER(PARTITION BY ... ORDER BY ...) compute running totals, ranks and top-N per group without collapsing rows.
  • Performance/Practicals: use appropriate indexes, avoid SELECT *, prefer JOINs or EXISTS for large subqueries depending on DB optimizer, use LIMIT/TOP for top-N queries, and test execution plans for heavy queries.

When to use what (short guide):

  • Use GROUP BY + HAVING to get aggregated metrics by category (sales by region, avg marks by class).
  • Use JOINs to assemble related data (customer + orders + products).
  • Use subqueries when a value depends on another query (e.g., compare to an aggregate value) or to filter with IN/EXISTS.
  • Use set operations to merge result sets from similar-structured queries (e.g., historical and current tables).
  • Use window functions to rank or compute moving totals while preserving row-level detail.

Important notes: HAVING filters after aggregation; WHERE filters rows before grouping. EXISTS generally stops at first match (often faster) while IN builds a set (careful with NULLs). UNION removes duplicates; UNION ALL keeps them.

📌 Examples
  • Aggregate + GROUP BY + HAVING (Sales per region): SELECT region, SUM(amount) AS total_sales FROM Sales GROUP BY region HAVING SUM(amount) > 100000 ORDER BY total_sales DESC;
  • INNER JOIN (Customer orders): SELECT c.customer_id, c.name, o.order_id, o.order_date, o.total_amount FROM Customers c INNER JOIN Orders o ON c.customer_id = o.customer_id WHERE o.order_date BETWEEN '2025-01-01' AND '2025-06-30';
  • LEFT JOIN (include customers with no orders): SELECT c.customer_id, c.name, o.order_id FROM Customers c LEFT JOIN Orders o ON c.customer_id = o.customer_id WHERE o.order_id IS NULL; -- customers with no orders
  • Subquery (non-correlated) — products never ordered: SELECT p.product_id, p.name FROM Products p WHERE p.product_id NOT IN (SELECT DISTINCT product_id FROM OrderDetails);
  • Correlated subquery — employees paid above dept average: SELECT e.emp_id, e.name, e.salary FROM Employees e WHERE e.salary > ( SELECT AVG(salary) FROM Employees WHERE dept_id = e.dept_id );
  • EXISTS vs IN: -- Using EXISTS (stops at first match): SELECT c.customer_id, c.name FROM Customers c WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.customer_id = c.customer_id AND o.total_amount > 500); -- Equivalent using IN may be less efficient for very large sets.
🧮 Formulas
  1. \[Basic SELECT structure: SELECT <columns> FROM <table> [WHERE <condition>] [GROUP BY <columns>] [HAVING <group_condition>] [ORDER BY <columns>];\]
  2. \[Aggregate examples: SUM(col)\]
    \[COUNT(*)\]
    \[AVG(col)\]
    \[MIN(col)\]
    \[MAX(col)\]
    \[Use in SELECT and HAVING with GROUP BY.\]
  3. \[Join syntax: SELECT ..\]
    \[FROM A INNER JOIN B ON A.key = B.key\]
    \[LEFT/RIGHT/FULL OUTER JOIN preserve non-matches from left/right/both respectively.\]
  4. \[Subquery (in predicate): SELECT ..\]
    \[FROM T WHERE col IN (SELECT col FROM T2 WHERE ...)\]
  5. \[Correlated subquery template: SELECT ..\]
    \[FROM A WHERE A.col > (SELECT AGG(B.col) FROM B WHERE B.fk = A.pk)\]
  6. \[Existence tests: EXISTS (SELECT 1 FROM ..\]
    \[WHERE ...)\]
    \[-- true if subquery returns any row\]

Key Concepts

SELECT
Retrieves specified columns from one or more tables.
FROM
Specifies the table(s) from which to retrieve data.
WHERE
Filters rows based on a specified condition.
ORDER BY
Sorts the result set by one or more columns, ascending (ASC) or descending (DESC).
GROUP BY
Groups rows that have the same values in specified columns for aggregate calculations.
HAVING
Filters groups created by GROUP BY using a condition (used with aggregates).
DISTINCT
Removes duplicate rows from the result set for the selected columns.
Aggregate Functions
Functions that perform calculations on a set of rows and return a single value (e.g., COUNT, SUM, AVG, MIN, MAX).
JOIN
Combines rows from two or more tables based on a related column between them.
INNER JOIN
Returns rows when there is a match in both tables.
LEFT JOIN
Returns all rows from the left table and matching rows from the right table; NULL for non-matching right rows.
RIGHT JOIN
Returns all rows from the right table and matching rows from the left table; NULL for non-matching left rows.
SUBQUERY
A query nested inside another query, used to supply values to the outer query.
ALIAS
Temporary name assigned to a table or column in a query for readability or convenience.
UNION
Combines results of two SELECT queries and returns distinct rows from both.
INSERT
Adds new row(s) into a table.
UPDATE
Modifies existing row(s) in a table based on a condition.
DELETE
Removes row(s) from a table based on a condition.
CREATE TABLE
Data Definition Language (DDL) command to create a new table with specified columns and types.
CONSTRAINT (PRIMARY KEY / FOREIGN KEY)
Rules applied to table columns to enforce data integrity: PRIMARY KEY uniquely identifies rows; FOREIGN KEY enforces referential integrity between tables.

Practice Questions

  1. Define SQL and name its four command categories. / SQL को परिभाषित कीजिए और इसकी चार कमांड श्रेणियों के नाम बताइए।
    Show answer

    SQL (Structured Query Language) is the standard declarative language for relational databases; its categories are DDL, DML, DCL and TCL. / SQL (Structured Query Language) संबंधपरक डेटाबेस के लिए मानक घोषणात्मक भाषा है; इसकी श्रेणियाँ DDL, DML, DCL और TCL हैं।

  2. What is the difference between the WHERE and HAVING clauses? / WHERE और HAVING उपवाक्य में क्या अंतर है?
    Show answer

    WHERE filters individual rows before aggregation and cannot use aggregate functions, while HAVING filters groups after aggregation and can use aggregates. / WHERE एकत्रीकरण से पहले अलग-अलग पंक्तियों को छानता है और एकत्रीकरण फलन का उपयोग नहीं कर सकता, जबकि HAVING एकत्रीकरण के बाद समूहों को छानता है और एकत्रीकरण फलन का उपयोग कर सकता है।

  3. Write an SQL query to display the average marks of each class only for classes with average marks >= 60. / प्रत्येक कक्षा के औसत अंक केवल उन कक्षाओं के लिए दिखाने हेतु SQL क्वेरी लिखिए जिनके औसत अंक >= 60 हों।
    Show answer

    SELECT class, AVG(marks) AS avg_marks FROM student_marks GROUP BY class HAVING AVG(marks) >= 60; / SELECT class, AVG(marks) AS avg_marks FROM student_marks GROUP BY class HAVING AVG(marks) >= 60;

  4. Distinguish between a PRIMARY KEY and a FOREIGN KEY. / PRIMARY KEY और FOREIGN KEY में अंतर बताइए।
    Show answer

    A PRIMARY KEY uniquely identifies each row and is UNIQUE plus NOT NULL; a FOREIGN KEY links a column to a primary/unique key in another table to enforce referential integrity. / PRIMARY KEY प्रत्येक पंक्ति की अद्वितीय पहचान करती है और UNIQUE तथा NOT NULL होती है; FOREIGN KEY संदर्भात्मक अखंडता हेतु किसी कॉलम को दूसरी तालिका की primary/unique key से जोड़ती है।

  5. Why is 'WHERE col = NULL' incorrect, and what should be used instead? / 'WHERE col = NULL' गलत क्यों है, और इसके बजाय क्या प्रयोग करें?
    Show answer

    Any comparison with NULL returns UNKNOWN, so it matches no rows; use 'IS NULL' or 'IS NOT NULL' instead. / NULL के साथ कोई भी तुलना UNKNOWN लौटाती है, इसलिए कोई पंक्ति मेल नहीं खाती; इसके बजाय 'IS NULL' या 'IS NOT NULL' का उपयोग करें।

  6. Differentiate between an INNER JOIN and a LEFT OUTER JOIN. / INNER JOIN और LEFT OUTER JOIN में अंतर बताइए।
    Show answer

    INNER JOIN returns only matching rows from both tables, while LEFT OUTER JOIN returns all rows from the left table with NULLs for unmatched right-table columns. / INNER JOIN केवल दोनों तालिकाओं की मेल खाती पंक्तियाँ लौटाता है, जबकि LEFT OUTER JOIN बाईं तालिका की सभी पंक्तियाँ लौटाता है तथा बेमेल दाईं तालिका कॉलम में NULL रखता है।

  7. What is a correlated subquery and how does it differ from a non-correlated one? / सहसंबद्ध उपक्वेरी क्या है और यह असहसंबद्ध से कैसे भिन्न है?
    Show answer

    A correlated subquery references columns from the outer query and is re-evaluated for each outer row; a non-correlated subquery runs once and feeds its result to the outer query. / सहसंबद्ध उपक्वेरी बाहरी क्वेरी के कॉलम का संदर्भ देती है और प्रत्येक बाहरी पंक्ति के लिए पुनः मूल्यांकित होती है; असहसंबद्ध उपक्वेरी एक बार चलती है और परिणाम बाहरी क्वेरी को देती है।

  8. Differentiate between UNION and UNION ALL set operators. / UNION और UNION ALL समुच्चय संकारकों में अंतर बताइए।
    Show answer

    UNION combines the rows of two queries and removes duplicates, whereas UNION ALL keeps all rows including duplicates. / UNION दो क्वेरियों की पंक्तियों को मिलाता है और डुप्लिकेट हटाता है, जबकि UNION ALL डुप्लिकेट सहित सभी पंक्तियाँ रखता है।

Related Laws & Principles

Explore all

Foundational laws & principles connected to this chapter — tap to open in the Laws Explorer.

Loading related laws…
Sourced from 213 content files · LLOS Learn · browse all chapters