Courses / Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course
3.5 Databases, Keys and SQL: Create, Insert, Update, Delete and Select
Unit 3 – Programming Fundamentals · 100 min read · 10 MCQs to check yourself
How to write your first SQL query: SELECT, FROM, WHERE and ORDER BY
1. Why use a database?
A database is an organised collection of related data stored so it can be searched, updated and shared easily. A DBMS (Database Management System) is the software that manages it – e.g. MS Access (desktop, forms and reports), MySQL/PostgreSQL (servers, websites), SQLite (a file-based database built into Python and phones), Oracle, SQL Server.
| Files (text/CSV) | Database |
|---|---|
| Data repeated in many files → inconsistency | Data stored once, linked by keys |
| Hard to search and update many records | Fast searching with SQL and indexes |
| No control over who changes what | Users, permissions, security |
| Risk of corruption when two people edit | Many users at once, transactions, backup |
Uses: school records, banking, NADRA, hospital patients, online shops, airline bookings.
2. Tables, records, fields and keys
- Table (relation) – data about one kind of thing (Students). Record (row / tuple) – one item (one student). Field (column / attribute) – one property (name).
- Primary key (PK) – a field (or fields) whose value is unique and never empty for every record:
roll_no,CNIC. - Candidate key – any field that could be the primary key (e.g.
roll_no,CNIC,email). - Secondary (alternate) key – a candidate key that was not chosen as the PK; also used to search/index records (e.g.
email). Non-unique fields used for searching (e.g.city) are also called secondary keys in some books. - Composite key – a PK made of two or more fields (
order_id + product_id). - Foreign key (FK) – a field in one table that refers to the PK of another table, creating a relationship.
3. SQL – Structured Query Language
| Category | Commands |
|---|---|
| DDL – define structure | CREATE TABLE, ALTER TABLE, DROP TABLE |
| DML – work with data | INSERT, UPDATE, DELETE, SELECT |
CREATE TABLE employees (
E_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
D_id INTEGER,
department TEXT,
salary INTEGER
);
INSERT INTO employees VALUES (1, 'Ali', 100, 'IT', 80000);
INSERT INTO employees VALUES (2, 'Sara', 101, 'HR', 60000);
INSERT INTO employees VALUES (3, 'Ahmed', 102, 'Finance', 90000);
INSERT INTO employees VALUES (4, 'Ayesha', 100, 'IT', 85000);
INSERT INTO employees VALUES (5, 'Bilal', 103, 'Marketing', 70000);
UPDATE employees SET salary = 65000 WHERE E_id = 2; -- edit a record
DELETE FROM employees WHERE E_id = 5; -- delete a record
ALTER TABLE employees ADD COLUMN email TEXT; -- update the table structure
Warning:
UPDATEorDELETEwithout WHERE changes or deletes every row.
SELECT – retrieving data
| Query | Meaning |
|---|---|
SELECT * FROM employees; |
all columns, all rows |
SELECT name, salary FROM employees; |
chosen columns |
SELECT * FROM employees WHERE department = 'IT'; |
filter rows |
SELECT * FROM employees WHERE salary > 70000 AND D_id = 100; |
combine conditions (AND, OR, NOT) |
SELECT * FROM employees WHERE name LIKE 'A%'; |
names starting with A |
SELECT * FROM employees ORDER BY salary DESC; |
sort (ASC is the default) |
SELECT COUNT(*), AVG(salary), MAX(salary) FROM employees; |
aggregate functions |
SELECT department, AVG(salary) FROM employees GROUP BY department; |
one row per group |
Worked example (FBISE model paper) – original table
| E_id | name | D_id | department | salary |
|---|---|---|---|---|
| 1 | Ali | 100 | IT | 80000 |
| 2 | Sara | 101 | HR | 60000 |
| 3 | Ahmed | 102 | Finance | 90000 |
| 4 | Ayesha | 100 | IT | 85000 |
| 5 | Bilal | 103 | Marketing | 70000 |
(a) SELECT * FROM employees WHERE department = 'IT';
| E_id | name | D_id | department | salary |
|---|---|---|---|---|
| 1 | Ali | 100 | IT | 80000 |
| 4 | Ayesha | 100 | IT | 85000 |
(b) SELECT AVG(salary) AS average_salary FROM employees; → (80000 + 60000 + 90000 + 85000 + 70000) / 5 = 77000
| average_salary |
|---|
| 77000 |
(c) SELECT name FROM employees ORDER BY salary DESC; → Ahmed, Ayesha, Ali, Bilal, Sara.
Short and long questions
1. Consider the employees table (Eid, name, Did, department, salary) with rows (1, Ali, 100, IT, 80000), (2, Sara, 101, HR, 60000), (3, Ahmed, 102, Finance, 90000), (4, Ayesha, 100, IT, 85000), (5, Bilal, 103, Marketing, 70000). Write the output of: (a) SELECT FROM employees WHERE department = IT; (b) SELECT AVG(salary) AS averagesalary FROM employees; (1.5 + 1.5)
Model answer
(a)
| E_id | name | D_id | department | salary |
|---|---|---|---|---|
| 1 | Ali | 100 | IT | 80000 |
| 4 | Ayesha | 100 | IT | 85000 |
(b) (80000 + 60000 + 90000 + 85000 + 70000) / 5 = 385000 / 5
| average_salary |
|---|
| 77000 |
2. A college keeps records of Students (rollno, name, class) and Fees (receiptno, rollno, amount, date). (a) Identify the primary and foreign keys and explain the relationship. (b) Write SQL to create both tables, insert two records into each, delete one fee record, and display all fees of roll number 12 sorted by date. (2 + 4)
Model answer
(a) Keys (2): Students – PK roll_no. Fees – PK receipt_no; FK roll_no refers to Students.roll_no. Relationship: one student → many fee receipts (one-to-many).
(b) SQL (4):
CREATE TABLE Students (roll_no INTEGER PRIMARY KEY, name TEXT NOT NULL, class TEXT);
CREATE TABLE Fees (receipt_no INTEGER PRIMARY KEY, roll_no INTEGER, amount INTEGER, date TEXT,
FOREIGN KEY (roll_no) REFERENCES Students(roll_no));
INSERT INTO Students VALUES (12, 'Ayesha', 'XII-A');
INSERT INTO Students VALUES (15, 'Hamza', 'XII-B');
INSERT INTO Fees VALUES (501, 12, 15000, '2026-03-01');
INSERT INTO Fees VALUES (502, 12, 15000, '2026-09-01');
DELETE FROM Fees WHERE receipt_no = 501;
SELECT * FROM Fees WHERE roll_no = 12 ORDER BY date;
3. Write SQL queries on Students(roll, name, city, marks) to: (a) show students from Lahore, (b) list names in descending order of marks, (c) count students with marks above 80.
Model answer
SELECT * FROM Students WHERE city = 'Lahore';
SELECT name FROM Students ORDER BY marks DESC;
SELECT COUNT(*) FROM Students WHERE marks > 80;
4. What is a database? Give three advantages of a database over file-based storage.
Model answer
A database is an organised collection of related data managed by a DBMS so it can be stored, searched, updated and shared efficiently. Advantages: (1) less redundancy and better consistency – each fact stored once; (2) fast searching and reporting with SQL; (3) security and multi-user access with permissions, backup and recovery.
5. Write SQL statements to (a) create a table Books(bookid, title, author, price), (b) insert one record, (c) increase the price of book 3 to 950.
Model answer
CREATE TABLE Books (book_id INTEGER PRIMARY KEY, title TEXT NOT NULL, author TEXT, price INTEGER);
INSERT INTO Books VALUES (1, 'Computer Science XII', 'FBISE', 850);
UPDATE Books SET price = 950 WHERE book_id = 3;
6. Differentiate between primary key, candidate key and foreign key with examples.
Model answer
- Primary key: the chosen field that uniquely identifies each record and cannot be null –
roll_noin Students. - Candidate key: any field that could serve as the primary key –
roll_no,CNIC,email. Candidates not chosen are alternate/secondary keys. - Foreign key: a field that refers to the primary key of another table –
dept_idin Employees refers toDepartments.dept_id.
This lesson ends with a quiz of 10 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: 3.4 File Handling in Python: Writing, Reading and Counting Letters · Next: 3.6 Data Normalisation: 1NF, 2NF and 3NF
All lessons in Computer Science XII (HSSC-II) – FBISE Complete SLO-Based Course