TK Study

Software Engineering › How to answer › SQL questions

How to answer SQL questions

SQL questions ask you to write one query (or complete one). They are marked on a query that would run and returns the right rows in the right order. Use the same order of clauses every time. From course notes From NESA material

What it looks like in the real exam

From NESA material 2025 HSC 2025 HSC exam, Q21 (4 marks): count jobs by status and category, filter and sort. 2025 sample: 2025 NESA sample exam, Q17 (3 marks: join, filter, order), 2025 NESA sample exam, Q22A (2 marks: complete the query). Familiarisation: 2025 NESA familiarisation exam, Q12A (2 marks) and 2025 NESA familiarisation exam, Q12B (3 marks, three tables, AS renames). The online exam has a SQL query tool.

The clause order (memorise)

SELECT what to show
FROM which table(s)
WHERE which rows (filter)
GROUP BY make groups for COUNT/SUM/AVG
ORDER BY sorting, ASC or DESC

From NESA material “SELECT fields FROM table WHERE criteria GROUP BY field ORDER BY field ASC or DESC; AS renames a field for display” (course specification).

The tables used in the examples below (original, I created and ran them in a real database):

CREATE TABLE Students (StudentID INTEGER PRIMARY KEY, Name TEXT, Year INTEGER, House TEXT);
INSERT INTO Students VALUES (1,'Ava',12,'Red'),(2,'Ben',11,'Blue'),(3,'Cleo',12,'Blue'),(4,'Dan',12,'Red'),(5,'Eli',11,'Red');
CREATE TABLE Scores (StudentID INTEGER, Subject TEXT, Mark INTEGER);
INSERT INTO Scores VALUES (1,'SE',80),(1,'Maths',70),(2,'SE',60),(3,'SE',90),(4,'SE',50),(5,'Maths',75);

Method

  1. Read the question and look at the table(s) (names of the tables and fields). Copy the exact names. From course notes
  2. Underline the parts and name the clause for each: “show” → SELECT; “from” → FROM; “only those where” → WHERE; “how many / total / average for each” → aggregate + GROUP BY; “highest first / A to Z” → ORDER BY. General advice
  3. Write FROM first (the table(s) you need, with a join if the data is in two tables), then SELECT, WHERE, GROUP BY, ORDER BY. General advice
  4. Quote text, not numbers: Status = 'Pending', Year = 12. Join conditions with AND / OR; use brackets when mixing them. From NESA material NESA 2025: quotation marks around searched data; correct AND/OR.
  5. If you use COUNT, SUM, AVG, MAX or MIN with another field in the SELECT, that other field must be in GROUP BY. From course notes
  6. Check: would it run? semicolon at the end; commas between fields; every table named; the order of rows as asked; any rename done with AS. From NESA material NESA: the query must run.

Worked example 1: count, filter, group and sort (2025 HSC Q21)

2025 HSC exam, Q21 · 4 marks

Below is the Issues table for a technical support company. FixTime, in minutes, is the time taken to complete each job. [table image not extractable: columns include JobID, Category, Status, FixTime] Construct a SQL query that displays: the number of pending software jobs; the number of pending hardware jobs; the number of escalated software jobs; the number of escalated hardware jobs. List the results in order from the highest to lowest, based on the number of jobs. (4 marks)

Official marking criteria

  • 4 marks: Correct SQL query that extracts 'Pending' and 'Escalated' jobs for the 'Software' and 'Hardware' categories, counts the jobs for each status in each category, and outputs results in descending order.
  • 3 marks: Addresses most requirements.
  • 2 marks: Some understanding of the problem.
  • 1 mark: Some understanding of SQL or the problem.

NESA sample answer

SELECT Category, Status, COUNT(JobID) AS Jobs
FROM Issues
WHERE (Status = 'Pending' OR Status = 'Escalated')
GROUP BY Status, Category
ORDER BY Jobs DESC

Notes

Note: the sample does not filter Category to Software/Hardware (the table is assumed to have only those two). Adding AND (Category = 'Software' OR Category = 'Hardware') is safe. Marker feedback: query must run; quotation marks around data being searched; use OR/AND in WHERE; correct GROUP BY and ORDER BY fields.

Open this question page · some of the NESA wording or answer is an image or is unpublished, as noted

  1. “count the jobs” → COUNT(JobID) AS Jobs.
  2. “for each status in each category” → GROUP BY Status, Category.
  3. “only Pending and Escalated, Software and Hardware” → a WHERE with quoted text and OR/AND brackets.
  4. “descending order” → ORDER BY Jobs DESC.

From NESA material The bank notes that the official sample doesn’t filter the category to Software or Hardware (the table is assumed to hold only those two). Adding the extra condition is safe: AND (Category = 'Software' OR Category = 'Hardware').

Original practice I ran this pattern on an invented table and it returned counts sorted high to low.

Worked example 2: a join (2025 sample Q17)

2025 NESA sample exam, Q17 · 3 marks

A shop uses an online database with a Products table (ProductID, Name, Price, Mass, Producer, Quantity, SupplierID) and a Suppliers table (SupplierID, Name). The prices are in dollars and the masses are in grams. The shop wants to identify all the products with a mass of at least 50 grams that are produced by Yumtreats. Write an SQL query to display the product names and supplier names, in descending order by product name. (3 marks)

Official marking criteria

  • 3 marks: Correct SQL query.
  • 2 marks: Addresses some of the requirements.
  • 1 mark: Some understanding of SQL.

NESA sample answer

SELECT Products.Name AS 'Product', Suppliers.Name AS 'Supplier'
FROM Products, Suppliers
WHERE Products.SupplierID = Suppliers.SupplierID
    AND Products.Producer = 'Yumtreats'
    AND Products.Mass >= 50
ORDER BY Products.Name DESC

Notes

An INNER JOIN ... ON equivalent is equally valid. Quote text values. Both tables have a column called Name, so qualify them (Table.Name).

Open this question page

From NESA material The bank notes: an INNER JOIN … ON version is equally valid; quote text values; both tables have a column called Name, so write Table.Name.

Original practice Both forms below return the same rows (checked on a test database):

SELECT Students.Name, Scores.Subject, Scores.Mark
FROM Students, Scores
WHERE Students.StudentID = Scores.StudentID AND Scores.Mark > 75
ORDER BY Scores.Mark DESC;

SELECT Students.Name, Scores.Subject, Scores.Mark
FROM Students INNER JOIN Scores ON Students.StudentID = Scores.StudentID
WHERE Scores.Mark > 75
ORDER BY Scores.Mark DESC;

Result: Cleo SE 90; Ava SE 80.

Worked example 3: three tables and renaming (2025 familiarisation Q12b)

2025 NESA familiarisation exam, Q12B · 3 marks

[Question stem not available as text (it is on the interactive NESA familiarisation site). Task per the NESA mapping grid: write a SQL query over Customers, Products and Orders showing customer, product and quantity with renamed columns, ordered by OrderID]

Official marking criteria

  • 3 marks: Correct SQL query.
  • 2 marks: Substantially correct query with at least TWO of: renaming the columns; selecting the three tables; joining the tables.
  • 1 mark: Some understanding of renaming the columns or joining the tables.

NESA sample answer

SELECT Customers.CustName as 'Customer', Products.ProductName as 'Product',
Orders.OrderQuantity as 'Quantity'
FROM Products, Customers, Orders
WHERE Orders.ProductID = Products.ProductID
AND Orders.CustID = Customers.CustID
ORDER BY Orders.OrderID ASC

Open this question page · some of the NESA wording or answer is an image or is unpublished, as noted

Reasoned Method: join two links (A to B and B to C), so you need two join conditions joined by AND. AS 'Customer' renames a column for display. The query is not published in full text, so I checked the pattern on my own three-table database: result Ana/Book/2, Raj/Pen/5, Ana/Pen/1.

Aggregates, GROUP BY, ALIAS and CRUD, with outputs

Original practice Run on the table above:

SELECT Name, House FROM Students WHERE Year = 12 ORDER BY House ASC, Name DESC;

Cleo Blue; Dan Red; Ava Red.

SELECT Subject, COUNT(StudentID) AS Entries, AVG(Mark) AS Average
FROM Scores GROUP BY Subject ORDER BY Entries DESC;

SE 4 entries, average 70.0; Maths 2 entries, average 72.5.

SELECT MAX(Mark), MIN(Mark) FROM Scores;

90 and 50.

SELECT Students.House, SUM(Scores.Mark) AS Total
FROM Students, Scores WHERE Students.StudentID = Scores.StudentID
GROUP BY Students.House ORDER BY Total DESC;

Red 275; Blue 150.

From course notes CRUD: INSERT INTO Students VALUES (6,'Fay',11,'Blue'); · SELECT … · UPDATE Scores SET Mark = 85 WHERE StudentID = 1 AND Subject = 'SE'; · DELETE FROM Scores WHERE StudentID = 5;. Never run UPDATE or DELETE without WHERE unless you mean every row.

Common mistakes

  • From NESA material NESA 2025 Q21: the query must run; no quotation marks around the data being searched; AND/OR misused in the WHERE; wrong or missing GROUP BY/ORDER BY fields.
  • General advice WHERE Status = 'Pending' AND Status = 'Escalated' never matches (one row has one status). Use OR.
  • General advice Selecting a plain field with COUNT without GROUP BY.
  • General advice Forgetting the join condition: you get every row paired with every row.
  • General advice Ambiguous names: when two tables have a Name column, write Table.Name.
  • General advice Putting ORDER BY before WHERE.

Time per mark

Derived 4 marks ≈ 7 minutes; 3 marks ≈ 5 minutes; 2 marks ≈ 3.5 minutes. General advice Spend a minute naming each clause before you type.

Practice (5 items, with answers)

Item 1 · original

Using the Students table, list the names of Year 12 students in the Red house.

Show the answer
SELECT Name FROM Students WHERE Year = 12 AND House = 'Red';

Ava, Dan. Original practice

Item 2 · original

List all Year 12 student names in reverse alphabetical order.

Show the answer
SELECT Name FROM Students WHERE Year = 12 ORDER BY Name DESC;

Dan, Cleo, Ava. Original practice

Item 3 · original

Show how many students are in each house.

Show the answer
SELECT House, COUNT(*) FROM Students GROUP BY House;

Blue 2, Red 3. Original practice

Item 4 · original

Show each student’s name and SE mark, only where the mark is 80 or more, in A to Z order of name.

Show the answer
SELECT Students.Name, Scores.Mark
FROM Students, Scores
WHERE Students.StudentID = Scores.StudentID
  AND Scores.Subject = 'SE' AND Scores.Mark >= 80
ORDER BY Students.Name;

Ava 80, Cleo 90. Original practice

Item 5 · original

Find the average SE mark.

Show the answer
SELECT AVG(Mark) FROM Scores WHERE Subject = 'SE';

70. Original practice

More: multiple-choice bank (SQL items), flashcards.

← How to answer Python questionsHow to answer trace table and desk checking questions →