Year 11 · Computer Science · Spring 1

Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Logic circuits and SQL

Exam practice10.3 Expressions and circuits9.3 SQL: SELECT, FROM, WHERE, AND, OR9.4 SQL: ORDER BY, SUM and COUNT

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

You will learn to

  • Draw a logic circuit from an expression, and write an expression from a circuit.
  • Complete a truth table for a circuit, using working columns.
  • Write SQL queries with SELECT, FROM, WHERE, AND, OR and ORDER BY.
  • Use SUM and COUNT in a query, and predict the exact output.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Success criteria

You have got it when you can say:

  • I can turn four circuits into expressions, and four expressions into circuits.
  • I can complete a 3-input truth table in the standard order.
  • I can write six SQL queries and check them with the SQL toy.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Retrieval starter

Questions from before. Write your answers, then show them.

  1. Last lesson

    Typing an email address twice to check it is…

    A ValidationB A trace tableC Verification

    C. Verification

  2. Last unit

    How many bits are in a byte?

    A 16B 4C 8

    C. 8

  3. Earlier this year

    A compiler…

    A Translates one line at a time, every runB Translates the whole program once into machine codeC Only checks spelling

    B. Translates the whole program once into machine code

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (1 of 8)

Logic gate
An electronic component that takes one or two inputs and gives one output by a fixed rule.
Truth table
A table that lists every possible input combination and the output for each one.
Logic circuit
Two or more logic gates joined together to give one output.
Logic expression
A written description of a circuit using gate names, such as X = A AND NOT B.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (2 of 8)

NOT
A one-input gate whose output is the opposite of its input; also called an inverter.
AND
A gate whose output is 1 only when both inputs are 1.
OR
A gate whose output is 1 when at least one input is 1.
NAND
NOT AND: the output is 0 only when both inputs are 1.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (3 of 8)

NOR
NOT OR: the output is 1 only when both inputs are 0.
XOR (EOR)
Exclusive OR: the output is 1 when the two inputs are different.
Input
A signal going into a gate or circuit, such as a sensor reading of 1 or 0.
Output
The single result signal that comes out of a gate or circuit.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (4 of 8)

Sum of products
An expression made by AND-ing the inputs of each row with output 1, then OR-ing those groups together.
Database
An organised, structured store of data that a computer can search and sort.
Table
A grid of data made of rows (records) and columns (fields).

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (5 of 8)

Field
One column of a table; it holds one item of data about every record, such as Minutes.
Record
One row of a table; it holds all the data about one thing, such as one bike hire.
Field name
The heading at the top of a column, such as HireID; it is not a record.
Data type
The kind of data a field can hold, such as integer, real or Boolean.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (6 of 8)

Text / alphanumeric
A data type for letters, digits and symbols, such as a name or a phone number.
Integer
A data type for whole numbers, such as 55.
Real
A data type for numbers with a decimal part, such as 5.50.
Date/time
A data type for a date, a time, or both, such as 05/03/2026.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (7 of 8)

SQL
Structured Query Language: the language used to ask a database for data.
Query
A question to the database, written in SQL, that returns matching data.
ORDER BY
The SQL clause that sorts the output, ascending (ASC) or descending (DESC).
SUM
An SQL function that adds up the values in a numeric field.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Key words (8 of 8)

COUNT
An SQL function that counts how many records meet a condition.

10.3 Expressions and circuits · The explanation · step 1 of 13 · part 1 of 2

From a statement to an expression

A logic expression describes a circuit in words, like `X = (A AND B) OR NOT C`.

Read the statement and turn each condition into a letter = 1 or letter = 0. "is on" or "is open" means the letter itself. "is off" or "is not" means NOT and the letter.

Join the conditions with the words the statement uses: "and" → AND, "or" → OR. Use brackets to keep each group together.

10.3 Expressions and circuits · The explanation · step 1 of 13 · part 2 of 2

From a statement to an expression (continued)

e.g.Example
Statement: a garden pump P switches on if the soil is dry (D = 1) and it is not raining (R = 0), or if the timer is on (T = 1). soil is dry → D not raining → NOT R timer is on → T Expression: P = (D AND NOT R) OR T
!Watch out

"R = 0" in a statement means you must write NOT R. Writing R on its own is the opposite of what the statement says.

10.3 Expressions and circuits · The explanation · step 2 of 13 · part 1 of 3

From an expression to a circuit

Draw one gate for every operator in the expression. Start with the brackets, then work outwards to the final gate.

Every NOT is its own gate. NAND, NOR and XOR are drawn as single gates, not built from others.

Draw exactly what is written. Do not simplify, even if you can see a shorter circuit. Label every input and the output.

10.3 Expressions and circuits · The explanation · step 2 of 13 · part 2 of 3

From an expression to a circuit (continued)

e.g.Example
X = (A AND B) OR NOT C Operators: AND, NOT, OR → 3 gates 1. A and B go into an AND gate. 2. C goes into a NOT gate. 3. The AND output and the NOT output go into an OR gate. 4. The OR output is X.
?Why?

Cambridge marks one gate at a time, checking each gate has the right inputs. A simplified circuit has different gates, so it loses marks even if it works.

10.3 Expressions and circuits · The explanation · step 2 of 13 · part 3 of 3

From an expression to a circuit (continued)

!Watch out

Every gate except NOT needs exactly two inputs. If one input, such as B, feeds two gates, branch its wire and mark the join with a dot.

10.3 Expressions and circuits · The explanation · step 3 of 13 · part 1 of 3

From a circuit to an expression

Start at the inputs on the left. Write the expression for the first gates' outputs.

Move right one gate at a time. Put each earlier part in brackets and join it with the next gate's name.

Finish at the output on the right.

10.3 Expressions and circuits · The explanation · step 3 of 13 · part 2 of 3

From a circuit to an expression (continued)

ABCA NOR BB AND CX
000101
001101
010000
011011
100000
101000
110000
111011

10.3 Expressions and circuits · The explanation · step 3 of 13 · part 3 of 3

From a circuit to an expression (continued)

e.g.Example
Circuit: A and B go into a NOR gate. B and C go into an AND gate. Both outputs go into an OR gate, output X. NOR gate → (A NOR B) AND gate → (B AND C) OR gate → X = (A NOR B) OR (B AND C)
!Watch out

Brackets matter. Without them, `A NOR B OR B AND C` could be read in several ways. Always put each gate's output in brackets.

10.3 Expressions and circuits · The explanation · step 4 of 13 · part 1 of 3

From a truth table to an expression

Look only at the rows where the output is 1.

For each of those rows, write the three inputs joined by AND. Put NOT in front of any input that is 0 in that row.

Then join all these groups with OR. This is called sum of products. It is long, but always correct.

Try the toy: type the final expression for X from the example. Its truth table should match the one below.

10.3 Expressions and circuits · The explanation · step 4 of 13 · part 2 of 3

From a truth table to an expression (continued)

ABCX
0000
0010
0100
0111
1000
1011
1101
1110

10.3 Expressions and circuits · The explanation · step 4 of 13 · part 3 of 3

From a truth table to an expression (continued)

e.g.Example
Output is 1 in three rows: A=0, B=1, C=1 → (NOT A AND B AND C) A=1, B=0, C=1 → (A AND NOT B AND C) A=1, B=1, C=0 → (A AND B AND NOT C) X = (NOT A AND B AND C) OR (A AND NOT B AND C) OR (A AND B AND NOT C)
?Why?

Each bracket is 1 for exactly one row and 0 for every other row. OR-ing them gives 1 in exactly the rows you need.

There is something to try for this step in the IGCSE library.

The explanation · 9 more steps

More on the lesson page

The lesson page has the rest of the explanation, step by step:

  • The shape of a query
  • Predicting output: one condition
  • AND: both conditions must be true
  • OR: at least one condition must be true
  • ORDER BY: sorting the output
  • SUM: adding up a field
  • COUNT: how many records match
  • Putting it all together
  • Writing a query from a question

Open the lesson page

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Try it: Play with logic gates

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Try it: Ask the database

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Quick quiz · 1 of 3

A good primary key is…

  1. A A date of birth
  2. B A surname
  3. C A unique ID number

C. A unique ID number

Two pupils can share a surname or a birthday. An ID is unique.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Quick quiz · 2 of 3

SELECT Name FROM Pupils WHERE Year = 8 ORDER BY Name; shows…

  1. A Only the first pupil
  2. B Year 8 names in alphabetical order
  3. C All pupils

B. Year 8 names in alphabetical order

WHERE filters the rows; ORDER BY sorts them.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Quick quiz · 3 of 3

The data type for 'has paid' (yes or no) is…

  1. A Boolean
  2. B Real
  3. C Text

A. Boolean

Boolean holds true or false.

Year 11 · Computer Science · Paper 2 practice: algorithms, programming, logic, databases · Lesson 4 of 5

Exit ticket

Answer on paper before you go. Then check.

  1. SELECT COUNT(*) FROM Pupils WHERE House = 'Red'; gives…

    A The total scoreB Their namesC How many pupils are in Red

    C. How many pupils are in Red COUNT gives one number: how many records match.

  2. Text in a WHERE clause goes in…

    A No quotesB Single quotes: 'Red'C Brackets

    B. Single quotes: 'Red' Without quotes the database looks for a field called Red.