Databases and SQL Basics Notes - Grade 11 Computer Studies | YNetStudyHub
Computer Studies Tutorial

Databases and SQL Basics

Grade 11 CBC Computer Studies notes on databases: tables, keys, relationships, and basic SQL commands such as SELECT, INSERT, UPDATE and DELETE.

Lesson 11 of 23
10 min read Mathew Wahome

Databases and SQL Basics

What is a database?

A database is an organised collection of related data that can be stored, searched and updated easily. Examples in Kenya include the KNEC candidate registry, M-Pesa transaction records and school admissions lists. A Database Management System (DBMS) such as MySQL, PostgreSQL or Microsoft Access manages the data.

Structure

  • Table: data arranged in rows and columns about one subject (e.g. students).
  • Field (column): one attribute, e.g. name.
  • Record (row): all data about one item.
  • Data types: text, number, date, Boolean.
id name class marks
1 Wanjiku 11A 78
2 Otieno 11B 65
3 Fatuma 11A 91

Keys and relationships

  • Primary key: uniquely identifies each record (id).
  • Foreign key: a field linking to another table's primary key.
  • Relationships: one-to-one, one-to-many (one class has many students), many-to-many.

Benefits of using keys: avoids duplication, keeps data consistent (integrity) and speeds searches.

SQL (Structured Query Language)

SQL is the standard language for relational databases. Commands end with a semicolon.

SELECT

SELECT name, marks FROM students; shows two columns. SELECT * FROM students; shows all columns.

WHERE and ORDER BY

SELECT name FROM students WHERE marks >= 70; SELECT * FROM students ORDER BY marks DESC;

INSERT

INSERT INTO students (id, name, class, marks) VALUES (4, 'Kamau', '11B', 55);

UPDATE

UPDATE students SET marks = 60 WHERE id = 4;

DELETE

DELETE FROM students WHERE id = 4;

Always include WHERE in UPDATE and DELETE. Without it every record changes.

Worked example 1

Show the names of Class 11A students sorted by marks, highest first. SELECT name FROM students WHERE class = '11A' ORDER BY marks DESC; Result: Fatuma, Wanjiku.

Worked example 2: aggregates

Find the average mark: SELECT AVG(marks) FROM students; For the table above: (78 + 65 + 91) / 3 = 78. Other functions: COUNT(), SUM(), MAX(), MIN().

Data security and ethics

  • Use passwords and user roles.
  • Back up regularly.
  • Follow the Kenya Data Protection Act, 2019: collect only necessary personal data and protect it.

Try it

  • Design a table for a school library with at least five fields and choose a primary key.
  • Write SQL to show students with marks between 50 and 80.
  • Write SQL to count students in class 11A.
  • Explain why a foreign key is needed between students and classes tables.
Quick Knowledge Check
Q1. A field that uniquely identifies each record in a table is called?
A Foreign key
B Primary key
C Index card
D Query
Reveal Answer & Explanation
Correct Answer: Option B (Primary key)
A primary key, like admission number, is unique for every record.
Q2. Which SQL command retrieves data from a table?
A INSERT
B UPDATE
C SELECT
D DROP
Reveal Answer & Explanation
Correct Answer: Option C (SELECT)
SELECT reads data from one or more tables.
Q3. Which statement lists students in Form with class = '11A'?
A SELECT * FROM students WHERE class = '11A';
B GET students WHERE class = '11A';
C DELETE students WHERE class = '11A';
D SHOW class FROM students = '11A';
Reveal Answer & Explanation
Correct Answer: Option A (SELECT * FROM students WHERE class = '11A';)
SELECT with a WHERE clause filters rows.
Q4. A foreign key is used to?
A Encrypt data
B Link a table to the primary key of another table
C Delete duplicates
D Back up a database
Reveal Answer & Explanation
Correct Answer: Option B (Link a table to the primary key of another table)
Foreign keys create relationships between tables.
Key Concept Flashcards
Concept Prompt
Define a database and a DBMS.

Key Takeaway
A database is an organised collection of related data. A DBMS (e.g. MySQL, MS Access) is software used to create and manage it.
Concept Prompt
Differentiate between a record and a field.

Key Takeaway
A field is a single column (e.g. Name). A record is a complete row of related fields about one item.
Concept Prompt
Write SQL to remove a student with id 5.

Key Takeaway
DELETE FROM students WHERE id = 5;
Interview & Practice Problems
Write SQL queries Intermediate

A table students(id, name, class, marks) holds data for a school. Write SQL to (a) display names of students with marks above 70, (b) change the class of student id 3 to '11B', (c) add a new student.

View Step-by-Step Solution & Code

(a) SELECT name FROM students WHERE marks > 70; (b) UPDATE students SET class = '11B' WHERE id = 3; (c) INSERT INTO students (id, name, class, marks) VALUES (10, 'Achieng', '11A', 82);

Looking to discuss or bookmark this note?
Join the learner community, leave comments, and save to collections.
Discussion & Actions
Study Assistant

Instant help with course questions

Hi there! I'm your YnetStudyHub assistant. How can I help with your studies today?