Unit 3: Relational algebra and SQL
Database Management Systems-I notes · PTU syllabus (UGCC2512)
On this page
Unit summary
Relational algebra is the mathematical foundation of database queries, and SQL is the practical language used to define, query and control relational databases. This unit covers relational algebra operations, SQL commands, aggregate functions, clauses, joins and advanced SQL objects such as views, cursors, stored procedures and triggers.
After this unit you can
- Write relational algebra expressions using selection, projection, set operations, join and division
- Use SQL DDL, DML and DCL commands
- Use aggregate functions with GROUP BY, HAVING and ORDER BY, and write joins
- Explain views, cursors, stored procedures, triggers and analytical/recursive queries
PTU syllabus topics
- Relational algebra operations (selection, projection, set operations, join, division)
- SQL DDL/DML/DCL
- aggregate functions
- logical operators
- predicates
- clauses (group by, having, order by)
- inner/natural/outer joins
- advanced SQL — analytical
- hierarchical and recursive queries
- views
- cursors
- stored procedures
- triggers
- dynamic SQL
DDL
CREATE, ALTER, DROP, TRUNCATE
DML
SELECT, INSERT, UPDATE, DELETE
DCL
GRANT, REVOKE
TCL
COMMIT, ROLLBACK, SAVEPOINT
Topic 1
Relational algebra
| Operation | Symbol | Meaning |
|---|---|---|
| Selection | σ | Chooses rows satisfying a condition: σ age>20 (STUDENT) |
| Projection | π | Chooses columns: π name, course (STUDENT) |
| Union | ∪ | Rows in either relation (union-compatible) |
| Intersection | ∩ | Rows in both relations |
| Set difference | − | Rows in the first but not the second |
| Cartesian product | × | Every row of one with every row of the other |
| Join | ⋈ | Product followed by a selection on matching columns |
| Division | ÷ | Rows related to all rows of another relation |
Example
Names of BCA students: π name (σ course = 'BCA' (STUDENT)).
Topic 2
SQL command categories
DDL
Define structure
CREATE, ALTER, DROP, TRUNCATE, RENAME
DML
Change data
INSERT, UPDATE, DELETE
DQL
Query data
SELECT
DCL
Control access
GRANT, REVOKE
TCL
Manage transactions
COMMIT, ROLLBACK, SAVEPOINT
sqlCREATE TABLE student (
roll INT PRIMARY KEY,
name VARCHAR(40) NOT NULL,
course VARCHAR(10),
marks INT CHECK (marks BETWEEN 0 AND 100)
);
INSERT INTO student VALUES (1, 'Ana', 'BCA', 82);
UPDATE student SET marks = 85 WHERE roll = 1;
SELECT name, marks FROM student WHERE course = 'BCA' ORDER BY marks DESC;Topic 3
Aggregates, operators, predicates and clauses
- Aggregate functions: COUNT, SUM, AVG, MIN, MAX.
- Operators and predicates: AND, OR, NOT, BETWEEN, IN, LIKE ('A%' starts with A), IS NULL.
- GROUP BY groups rows; HAVING filters groups; ORDER BY sorts.
sqlSELECT course, COUNT(*) AS students, AVG(marks) AS average
FROM student
GROUP BY course
HAVING AVG(marks) > 60
ORDER BY average DESC;Exam tip
WHERE filters rows before grouping; HAVING filters groups after grouping. This difference is asked very often.
Topic 4
Joins
| Join | Returns |
|---|---|
| Inner join | Only rows with matching values in both tables |
| Natural join | Inner join on all columns with the same name |
| Left outer join | All rows of the left table, with matches or NULLs |
| Right outer join | All rows of the right table, with matches or NULLs |
| Full outer join | All rows of both tables |
sqlSELECT s.name, c.title
FROM student s INNER JOIN course c ON s.course = c.code;Topic 5
Advanced SQL
- View: a virtual table defined by a query:
CREATE VIEW toppers AS SELECT * FROM student WHERE marks > 80; - Cursor: a pointer that processes the rows of a query result one at a time (in PL/SQL or stored procedures).
- Stored procedure: a named block of SQL statements stored in the database and executed with CALL/EXEC.
- Trigger: a block that runs automatically on INSERT, UPDATE or DELETE (for example logging changes).
- Dynamic SQL: SQL statements built and executed at run time.
- Analytical queries use window functions such as
RANK() OVER (ORDER BY marks DESC); hierarchical and recursive queries (CONNECT BY orWITH RECURSIVE) process tree data like an employee–manager chart.
Key terms
- Selection (σ)
- Relational operation choosing rows
- Projection (π)
- Relational operation choosing columns
- Aggregate function
- A function summarising many rows into one value
- View
- A virtual table based on a query
- Trigger
- SQL code executed automatically on a data change
Quick revision
- σ picks rows, π picks columns, ⋈ joins.
- DDL, DML, DQL, DCL, TCL.
- WHERE before grouping; HAVING after.
- Inner join = matches only; outer joins keep unmatched rows.
- Views are virtual; triggers fire automatically.
Important exam questions
Practice questions written to the PTU exam pattern for this unit's syllabus: short answers (Section A style) and long answers (Sections B and C style).
Short-answer questions
- Q1.Differentiate between selection and projection.
- Q2.Differentiate between DDL and DML with examples.
- Q3.What is the difference between WHERE and HAVING?
- Q4.Define a view.
- Q5.What is a trigger?
- Q6.Differentiate between DELETE, TRUNCATE and DROP.
Long-answer questions
- Q1.Explain the operations of relational algebra with examples.
- Q2.Explain the types of joins in SQL with queries.
- Q3.Write SQL queries using aggregate functions, GROUP BY and HAVING for an EMPLOYEE table.
- Q4.Explain views, stored procedures, cursors and triggers with examples.
Stuck on this unit?
Message SBS on WhatsApp for help with Database Management Systems-I, or to ask about studying BCA at Synetic.
