Read, understand and complete SQL scripts to query data

📚 Databases – IGCSE Computer Science 0478

What is a Database?

A database is like a digital filing cabinet where information is stored in tables. Each table is a grid of rows (records) and columns (fields). Think of it as a spreadsheet that can be searched, sorted, and updated quickly.

🔍 SQL – The Language of Databases

Key SQL Concepts

  • SELECT – choose which columns to display.
  • FROM – specify the table.
  • WHERE – filter rows.
  • JOIN – combine rows from two tables.
  • GROUP BY – aggregate data.
  • ORDER BY – sort results.

📝 Example Script

SELECT student_id, name, grade
FROM Students
WHERE grade > 70
ORDER BY grade DESC;

🧩 Common SQL Tasks

1️⃣ Retrieve All Data from a Table

SELECT * FROM Employees;

2️⃣ Filter Rows with WHERE

SELECT name, salary
FROM Employees
WHERE department = 'Sales';

3️⃣ Join Two Tables

Imagine you have Students and Enrollments tables. You want to see which courses each student is taking.

SELECT s.name, c.course_name
FROM Students s
JOIN Enrollments e ON s.student_id = e.student_id
JOIN Courses c ON e.course_id = c.course_id;

4️⃣ Aggregate with GROUP BY

Find the average grade per class.

SELECT class_id, AVG(grade) AS avg_grade
FROM Grades
GROUP BY class_id;

5️⃣ Subquery Example

Show students whose grade is above the class average.

SELECT name, grade
FROM Students
WHERE grade > (
  SELECT AVG(grade) FROM Students
);

📌 Exam Tips & Tricks

Tip 1: Read the Question Carefully

Identify the required output and any filters (e.g., “students with grades > 80”).

Tip 2: Use Aliases for Readability

Shorten table names with AS to keep queries tidy.

Tip 3: Test Incrementally

Start with a simple SELECT, then add WHERE, JOIN, etc., checking results each step.

🧠 Quick Reference Table

Clause Purpose Example
SELECT Choose columns SELECT name, age
FROM Specify table FROM Employees
WHERE Filter rows WHERE salary > 50000
JOIN Combine tables JOIN Orders ON Customers.id = Orders.customer_id
GROUP BY Aggregate data GROUP BY department
ORDER BY Sort results ORDER BY age DESC

💡 Final Thought

Think of SQL as a set of building blocks. Each clause adds a layer of detail to your query. Mastering these blocks lets you pull exactly the information you need from a database – a skill that’s useful in coding, data analysis, and everyday tech tasks.

Revision

Log in to practice.

0 views 0 suggestions