Computer Science • Databases

SQL Challenges

The database

Question

SQL Challenges — Explanation

You're connected to a database, and each question asks you to find something out by writing an SQL query. Your queries run on your own copy of the database, in your browser, so try anything: you can't break it.

Some questions show a query and ask what it outputs instead. Questions marked AS go beyond IGCSE: AVG, MIN, MAX, LIKE (with % standing for any characters) and GROUP BY.

2+ tables. These databases are normalised (third normal form): each table holds one kind of thing, and a foreign key in one table holds the primary key of a row in another. To answer a question about more than one table, join them where the keys match. There are two ways to write it, and both are right:

Write a column as Table.Column when it's in more than one of the tables. A one-to-many relationship needs one join; a many-to-many one goes through a link table (such as Enrolments, between Students and Courses), so it needs two. Leave out a join condition and every row of one table is paired with every row of the other. When you get one right, both ways of writing it are shown.

Checking. Your query is run and its result is compared with the right answer's, so any query that gives the right rows is right, whatever its column names. When a question asks for an order, your rows must be in that order too. If it's not right, you're told what's wrong with it (never the answer), and you can try again. Stuck? Hint says which tables, columns and parts of SQL to use, and halves the question's mark; Show me shows a query that works, and the question scores nothing. Explore, in the title bar, lets you run any query on the database, unchecked.

Mysteries. A mystery is a case to solve with several tables of evidence: join them (INNER JOIN), count and add them up (GROUP BY, COUNT, SUM), and keep your own list of suspects, with CREATE TABLE, ALTER TABLE, INSERT INTO, UPDATE and DELETE FROM, as in Cambridge AS 9618. The evidence can't be changed, but your own tables can. Each step is checked against your database; everything you change is kept, so you can stop and carry on, or start the case again.

Signed in, your queries are saved: come back and carry on. Each question is worth a mark when you get it right on your own, half a mark after a hint, and none if you're shown the answer. Questions your teacher has set are marked, and come up first.

Objective: read, understand and complete SQL scripts to query data stored in a single database table (Cambridge IGCSE Computer Science 0478, 9.4), and write SQL to query data, including from more than one table with INNER JOIN (9618 8.3).

Where this fits

  • AQA: AQA A Level Computer Science (7517)
  • Cambridge: Cambridge IGCSE Computer Science (0478); Cambridge A Level Computer Science (9618); Cambridge AS Level Computer Science (9618) Goes beyond Cambridge IGCSE Computer Science (0478): Joins, GROUP BY, LIKE and the AS questions go beyond 0478, which uses one table; the IGCSE questions fit it.
  • IB: IB Computer Science HL; IB Computer Science SL
  • NCEA Level 2 Digital Technologies: 91892 Use advanced techniques to develop a database; 91892 Use advanced techniques to develop a database
  • NCEA Level 3 Digital Technologies: 91902 Use complex techniques to develop a database; 91902 Use complex techniques to develop a database
  • Pearson Edexcel International: Edexcel International A Level Information Technology
  • USDP: USDP Computer Science

SQL Challenges — Key Terms

Key concepts in English, with te reo Māori, Chinese (Simplified) and Korean.

EnglishTe reo Māori中文(简体)한국어What it means on this page
Databasepātengi raraunga数据库데이터베이스A structured collection of related tables: here, the one you are connected to.
Table (relation)ripanga表테이블A grid of rows and columns holding data about one kind of thing, such as songs.
Row (record)haupae / pūkete行행One entry in a table: here, one song.
Column (field)tīwae / āpure字段필드One named piece of data stored for every row, such as Title or Year.
Primary keypātuhi matua主键기본 키The field that identifies each row of a table, such as SongID.
SQL (Structured Query Language)no attested term结构化查询语言 (SQL)구조적 질의 언어 (SQL)The standard language for defining, querying and changing the data in a relational database.
Queryno attested term查询질의 (쿼리)A request for data from a database, written in SQL: SELECT the columns FROM a table WHERE a condition is true.
Conditionno attested term条件조건A test that is true or false for each row, after WHERE: only the rows where it is true are kept.
Ascending order (ASC)no attested term升序오름차순Smallest, earliest or A first: ORDER BY Year ASC.
Descending order (DESC)no attested term降序내림차순Largest, latest or Z first: ORDER BY Year DESC.
Aggregate functionno attested term聚合函数집계 함수A function that works out one value from many rows: COUNT counts them, SUM adds up a column.

On the te reo Māori column. Terms marked as gaps have no attested equivalent in the sources checked — Karaitiana Taiuru's Dictionary of Māori Computer and Social Media Terms, Paekupu, the Reserve Bank's te reo financial glossary, NZQA and Te Aka. No coinage is printed as though it were established; where a class needs one, commission it from Te Taura Whiri i te Reo Māori and credit the translator. Te reo Māori is not italicised and takes no plural "s".