Query Languages
Query Languages
Relational Algebra
1. Introduction to Query Languages
Query languages are used to interact with and retrieve data from relational databases. They allow
users to perform operations such as data retrieval, insertion, updating, and deletion. The three
primary query languages covered in this lesson are:
sql
CopyEdit
CREATE TABLE Students (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Email VARCHAR(100)
);
sql
CopyEdit
INSERT INTO Students (Student_ID, Name, Age, Email)
VALUES (101, 'Alice', 22, '[email protected]');
Basic Query
sql
CopyEdit
SELECT * FROM Students;
sql
CopyEdit
SELECT Name, Age FROM Students WHERE Age > 20;
sql
CopyEdit
SELECT Name, Age FROM Students ORDER BY Age DESC;
Aggregation Functions
sql
CopyEdit
SELECT COUNT(Student_ID), AVG(Age) FROM Students;
sql
CopyEdit
SELECT Students.Name, Courses.Course_Name
FROM Students
JOIN Enrollment ON Students.Student_ID = Enrollment.Student_ID
JOIN Courses ON Enrollment.Course_ID = Courses.Course_ID;
sql
CopyEdit
UPDATE Students SET Age = 23 WHERE Student_ID = 101;
DELETE FROM Students WHERE Student_ID = 102;
sql
CopyEdit
SELECT Name, Age FROM Students WHERE Age > 20 ORDER BY Age ASC;
4. Relational Algebra
Relational Algebra is a procedural query language that defines a set of operations for retrieving
data from relational databases. It serves as the theoretical foundation of SQL.
4.1 Basic Relational Algebra Operations
∪ Students ∪ Alumni
Projection π Selects columns π Name, Age (Students)
Union Combines two relations
Students ∩
Intersection ∩ Common elements in two relations
Scholarship_Holders
Finds records in one relation but not
Difference - Students - Dropouts
another
Cartesian
× Joins all tuples from two relations Students × Courses
Product
⋈ Students ⋈ Enrollment
Combines related tuples from two
Join
relations
plaintext
CopyEdit
σ Age > 20 (Students)
plaintext
CopyEdit
π Name, Email (Students)
plaintext
CopyEdit
Students × Courses
plaintext
Students ⋈ Enrollment ⋈ Courses
CopyEdit
plaintext
Students ∪ Alumni
CopyEdit
1. Write an SQL query to retrieve the names of students older than 21.
2. Write an SQL query to list all courses with more than 3 credits.
3. Write an SQL query to find the total number of students enrolled in a course.
Fill in the QBE grid to show all students who have enrolled in at least one course.
1. Write a relational algebra expression to retrieve all students taking "Database Systems".
2. Perform a JOIN operation between Students and Enrollment tables.