0% found this document useful (0 votes)
2 views5 pages

Query Languages

Uploaded by

safarijimmy25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Query Languages

Uploaded by

safarijimmy25
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Query Languages: SQL, QBE, and

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:

 Structured Query Language (SQL)


 Query By Example (QBE)
 Relational Algebra

2. Structured Query Language (SQL)


SQL is the most widely used database query language. It follows a declarative approach,
meaning users specify what they want rather than how to retrieve it.

2.1 SQL Categories

SQL is divided into four main categories:

1. Data Definition Language (DDL) – Defines the structure of a database.


o CREATE, ALTER, DROP, TRUNCATE
2. Data Manipulation Language (DML) – Manages data within tables.
o INSERT, UPDATE, DELETE, SELECT
3. Data Query Language (DQL) – Used to retrieve data.
o SELECT
4. Data Control Language (DCL) – Controls access to data.
o GRANT, REVOKE

2.2 Implementing Queries Using SQL

a) Creating Tables (DDL)

sql
CopyEdit
CREATE TABLE Students (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Age INT,
Email VARCHAR(100)
);

b) Inserting Data (DML)

sql
CopyEdit
INSERT INTO Students (Student_ID, Name, Age, Email)
VALUES (101, 'Alice', 22, '[email protected]');

c) Retrieving Data (DQL - SELECT Queries)

 Basic Query

sql
CopyEdit
SELECT * FROM Students;

 Filtering with WHERE Clause

sql
CopyEdit
SELECT Name, Age FROM Students WHERE Age > 20;

 Sorting with ORDER BY

sql
CopyEdit
SELECT Name, Age FROM Students ORDER BY Age DESC;

 Aggregation Functions

sql
CopyEdit
SELECT COUNT(Student_ID), AVG(Age) FROM Students;

 Joining Tables (INNER JOIN Example)

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;

d) Updating and Deleting Data (DML)

sql
CopyEdit
UPDATE Students SET Age = 23 WHERE Student_ID = 101;
DELETE FROM Students WHERE Student_ID = 102;

3. Query By Example (QBE)


Query By Example (QBE) is a visual approach to querying databases where users provide
sample data rather than writing SQL statements. It is commonly used in GUI-based database
systems like Microsoft Access and MySQL Workbench.

3.1 Features of QBE

 Uses a grid or form to define queries.


 Generates SQL queries automatically.
 Supports SELECT, INSERT, UPDATE, DELETE operations.

3.2 Example of QBE Query for Retrieving Student Data

Field Table Criteria Sort Show


Name Students Age > 20 ASC ✅
Age Students ✅

 The above table would generate the SQL query:

sql
CopyEdit
SELECT Name, Age FROM Students WHERE Age > 20 ORDER BY Age ASC;

3.3 Advantages of QBE

✔️User-friendly, no need to write SQL manually.


✔️Helps beginners understand SQL structure.
✔️Reduces errors in complex queries.

3.4 Disadvantages of QBE

❌ Limited in functionality compared to SQL.


❌ Not as widely supported across all database systems.

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

Operation Symbol Description Example


Selection σ Filters rows σ Age > 20 (Students)

∪ 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

4.2 Examples of Relational Algebra Queries

1) Selection (σ) - Filtering Data

Find students older than 20:

plaintext
CopyEdit
σ Age > 20 (Students)

2) Projection (π) - Selecting Specific Columns

Get only student names and emails:

plaintext
CopyEdit
π Name, Email (Students)

3) Cartesian Product (×) - Combining Two Relations

Pair every student with every course:

plaintext
CopyEdit
Students × Courses

4) Join (⋈) - Combining Related Data

Retrieve student names and their enrolled courses:

plaintext
Students ⋈ Enrollment ⋈ Courses
CopyEdit

5) Union (∪) - Merging Two Relations

Find all unique students and alumni:

plaintext

Students ∪ Alumni
CopyEdit

5. CAT 1 (Continuous Assessment Test 1) – Practice


Questions
Section A: SQL Queries (10 Marks)

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.

Section B: Query By Example (QBE) (5 Marks)

Fill in the QBE grid to show all students who have enrolled in at least one course.

Section C: Relational Algebra (5 Marks)

1. Write a relational algebra expression to retrieve all students taking "Database Systems".
2. Perform a JOIN operation between Students and Enrollment tables.

You might also like