Unit 3 of 4 · BCA Sem 3

Unit 3: Relational algebra and SQL

Database Management Systems-I notes · PTU syllabus (UGCC2512)

3 min read5 topics10 exam questions
On this page
  1. Unit summary
  2. Relational algebra
  3. SQL command categories
  4. Aggregates, operators, predicates and clauses
  5. Joins
  6. Advanced SQL
  7. Key terms
  8. Quick revision
  9. Important questions

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
ClassificationCategories of SQL commands
SQL
  • DDL

    CREATE, ALTER, DROP, TRUNCATE

  • DML

    SELECT, INSERT, UPDATE, DELETE

  • DCL

    GRANT, REVOKE

  • TCL

    COMMIT, ROLLBACK, SAVEPOINT

1

Topic 1

Relational algebra

OperationSymbolMeaning
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)).

2

Topic 2

SQL command categories

ComparisonSQL sub-languages
Purpose
Commands

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;
3

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.

4

Topic 4

Joins

JoinReturns
Inner joinOnly rows with matching values in both tables
Natural joinInner join on all columns with the same name
Left outer joinAll rows of the left table, with matches or NULLs
Right outer joinAll rows of the right table, with matches or NULLs
Full outer joinAll rows of both tables
sqlSELECT s.name, c.title
FROM student s INNER JOIN course c ON s.course = c.code;
5

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 or WITH 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

  1. Q1.Differentiate between selection and projection.
  2. Q2.Differentiate between DDL and DML with examples.
  3. Q3.What is the difference between WHERE and HAVING?
  4. Q4.Define a view.
  5. Q5.What is a trigger?
  6. Q6.Differentiate between DELETE, TRUNCATE and DROP.

Long-answer questions

  1. Q1.Explain the operations of relational algebra with examples.
  2. Q2.Explain the types of joins in SQL with queries.
  3. Q3.Write SQL queries using aggregate functions, GROUP BY and HAVING for an EMPLOYEE table.
  4. 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.

WhatsApp us