Which books in a Cardiff library have fewer than three copies?
Your library in Cardiff wants a list of titles with fewer than three copies. The catalogue has all the numbers, but your query lists every book. How can you make it show just the titles the library needs?
You already know this catalogue query. Select Title tells the database which column to display. From Books tells it which table to read. There’s no test for which rows to keep, so you get the title from every row.
Watch what happens when you test the copy count in each row. Harbour has two copies and Lakes has one, so both stay. Moorland has exactly three, and Railways has four, so both go. In SQL, you put that row test after the word where.
The condition says to keep a row if its copy count is less than three. A book has exactly three copies. Does its row pass the test? Pick the answer that gives the right reason.
Add where after from Books and put the semicolon at the end. Copies less than three keeps Harbour and Lakes. In the second query, put single quotes around Fiction because it’s text. That comparison keeps Harbour and Moorland, the two titles whose genre is Fiction.
Keep select Title and from Books. Then join the two comparisons with and. A row now needs fewer than three copies and its genre must be Fiction. If either comparison is false, the row stays out of the result.
Check the four rows against both tests. Harbour passes both. Lakes has one copy but is Travel, so it fails the genre test. Moorland and Railways fail the copy test. That leaves Harbour. Since you selected only Title, the result has one column and one row.
Now select only Title for Travel books with fewer than four copies. Book A is Travel with three copies. B is Fiction with two. C is Travel with four. Pick the output the query would produce.
Insert into Books names the columns. Values supplies Rivers, Travel and five copies for the new row. Update sets the count to six, and where targets Rivers. Delete from uses the same condition to remove Rivers. Without where, either the update or the delete would affect every row.
Retrieve titles with fewer than three copies in descending title order. Here, Stock holds the same counts as before. Select the title from Books, read both tables and match each book to its stock row by title. Keep counts below three. Order by title descending puts Lakes before Harbour.