Subject: Computer Science | Level: A-Level | Exam Board: Edexcel
Master Edexcel A-Level SQL with this comprehensive guide, packed with examiner insights, worked examples, and multi-modal resources. Learn to construct precise, mark-scoring queries and avoid the common pitfalls that cost candidates valuable marks.
Revision Notes & Key Concepts
Key Terms & Definitions
- Relational Database
- A database structured to recognise relations between stored items of information. Data is organised as a set of formally-described tables from which data can be accessed or reassembled in many different ways without having to reorganise the database tables.
- Primary Key
- A field in a table which uniquely identifies each record in that table. It must contain a unique value for each row of data and cannot contain null values.
- Foreign Key
- A field in a table that is a primary key in another table. It acts to provide a logical link between the two tables.
- Cardinality
- The relationship between two tables, expressed as one-to-one, one-to-many, or many-to-many.
- Aggregate Function
- A function that performs a calculation on a set of values and returns a single value. Examples include COUNT, SUM, AVG, MIN, MAX.
- Alias
- A temporary name given to a table or a column in a query. An alias is created with the `AS` keyword.
Worked Examples
Worked Example
Question: A database contains two tables, `Employee` and `Department`. `Employee(EmployeeID, Name, Salary, DeptID)` `Department(DeptID, DeptName, Location)` Write an SQL query to find the names of all employees who work in the 'Sales' department and have a salary greater than 30000. The results should be listed in alphabetical order of name.
Solution: Step 1: Identify the required columns (`Name`) and the source tables (`Employee`, `Department`). This indicates a JOIN is needed. Step 2: Formulate the SELECT and FROM clauses, including the INNER JOIN on the `DeptID` foreign key. `SELECT Employee.Name FROM Employee INNER JOIN Department ON Employee.DeptID = Department.DeptID` Step 3: Add the WHERE clause to filter for the 'Sales' department AND the salary condition. `WHERE Department.DeptName = 'Sales' AND Employee.Salary > 30000` Step 4: Add the ORDER BY clause to sort the results. `ORDER BY Employee.Name ASC;` **Final Answer**: sql SELECT Employee.Name FROM Employee INNER JOIN Department ON Employee.DeptID = Department.DeptID WHERE Department.DeptName = 'Sales' AND Employee.Salary > 30000 ORDER BY Employee.Name ASC;
Worked Example
Question: Using the same `Employee` and `Department` tables, write an SQL query to find the name of each department and the number of employees working in it. The results should only include departments with more than 5 employees.
Solution: Step 1: Identify the need for aggregation (`number of employees`) and grouping (`each department`). This requires COUNT() and GROUP BY. Step 2: Select the department name and the count of employees. A JOIN is needed to get the department name. `SELECT Department.DeptName, COUNT(Employee.EmployeeID)` `FROM Department` `INNER JOIN Employee ON Department.DeptID = Employee.DeptID` Step 3: Group the results by the department name to ensure the COUNT applies to each department individually. `GROUP BY Department.DeptName` Step 4: Add the HAVING clause to filter the groups, keeping only those with a count greater than 5. `HAVING COUNT(Employee.EmployeeID) > 5;` **Final Answer**: sql SELECT Department.DeptName, COUNT(Employee.EmployeeID) FROM Department INNER JOIN Employee ON Department.DeptID = Employee.DeptID GROUP BY Department.DeptName HAVING COUNT(Employee.EmployeeID) > 5;
Worked Example
Question: A table `tblFilms` stores details about films. `tblFilms(FilmID, Title, Genre, Rating, ReleaseDate)` Write a DDL statement to add a new column named `Director` to the `tblFilms` table. The column should store a variable-length string of up to 100 characters.
Solution: Step 1: Identify the command type. The question asks to 'add a new column', which is a structural change, so a DDL statement is needed. Specifically, ALTER TABLE. Step 2: State the command and the table to be modified. `ALTER TABLE tblFilms` Step 3: State the action (ADD) and define the new column's name and data type. `ADD Director VARCHAR(100);` **Final Answer**: sql ALTER TABLE tblFilms ADD Director VARCHAR(100);
Practice Questions
Question: A database has a table `tblPlayers(PlayerID, PlayerName, TeamID, GoalsScored)`. Write an SQL query to find the names of all players who have scored more than 20 goals.
Answer:
Question: Two tables exist: `Customer(CustomerID, Name)` and `Orders(OrderID, CustomerID, OrderDate, Amount)`. Write an SQL query to list the names of all customers who have not placed any orders. Assume a customer has not placed an order if they do not appear in the Orders table.
Answer:
Question: Using the `Customer` and `Orders` tables from the previous question, write an SQL query to find the total amount spent by each customer. The output should show the customer's name and their total order amount.
Answer:
Question: Write an SQL query to find the names of all products from a `Products` table where the `ProductName` starts with the letter 'A'.
Answer:
Question: Write a DDL statement to create a new table called `Subjects` with two columns: `SubjectID` as an integer that is the primary key, and `SubjectName` as a variable-length string that cannot be empty.
Answer:




