Query Languages
This chapter covers formal query languages and SQL, connecting mathematical relational theory with practical database querying.
3.1 Relational Calculus
Introduction / Definition
Relational calculus is a declarative formal query language based on predicate logic. It describes the properties that desired results must satisfy rather than prescribing an operation sequence. Tuple Relational Calculus uses tuple variables, while Domain Relational Calculus uses domain variables.
Syntax / Structure
TRC: { t | P(t) }
DRC: { <x1, x2, ...> | P(x1, x2, ...) }
Detailed Explanation
For students with marks above 80, the query describes all student tuples t such that t.Marks > 80. It focuses on what qualifies as an answer.
Example
{ t | t ∈ STUDENT AND t.Marks > 80 }
Output / Result
The set of Student tuples whose Marks are greater than 80.
Important Points / Notes
- Declarative approach.
- Based on predicate logic.
- Useful for understanding theoretical query foundations.
Mini Practice Question
Write a TRC-style condition for employees earning more than 50,000.
3.2 Relational Algebra
Introduction / Definition
Relational algebra is a procedural formal query language built from operations on relations. Each operation produces a relation that can become input to another operation. It is useful for understanding query transformations and optimization.
Syntax / Structure
σ_p(R)
π_A(R)
R ⋈_condition S
Detailed Explanation
An algebraic expression can first filter rows and then project columns. Query optimizers can transform equivalent expressions to reduce intermediate results and cost.
Example
π_Name(σ_Marks > 80(STUDENT))
Output / Result
The names of students whose marks exceed 80 are returned.
Important Points / Notes
- Operators are composable.
- Intermediate results are relations.
- Equivalent expressions may have different execution costs.
Mini Practice Question
Express “names of employees in Department 10” using algebra.
3.3 SQL
Introduction / Definition
SQL is the practical language widely used with relational DBMSs. It supports querying, table creation, inserts, updates, deletes, constraints, transactions, and permissions. SQL is largely declarative: the user specifies the desired result while the DBMS chooses an execution strategy.
Syntax / Structure
SELECT ... FROM ... WHERE ...;
INSERT INTO ... VALUES (...);
UPDATE ... SET ... WHERE ...;
DELETE FROM ... WHERE ...;
Detailed Explanation
SELECT retrieves data, WHERE filters rows, JOIN combines tables, GROUP BY forms groups, HAVING filters groups, and aggregate functions such as COUNT and AVG summarize data.
Example
SELECT d.Department_Name, AVG(s.Marks) AS AvgMarks
FROM Student s
JOIN Department d ON s.Dept_ID = d.Department_ID
GROUP BY d.Department_Name
HAVING AVG(s.Marks) > 70;
Output / Result
Departments whose average student marks exceed 70 are returned.
Important Points / Notes
- WHERE filters rows; HAVING filters groups.
- JOIN combines related tables.
- Aggregate functions summarize data.
- Use WHERE carefully with UPDATE and DELETE.
Mini Practice Question
Write SQL to display student names with Marks > 80.