Which book would appear first if your school library sorted by most copies?
Your school library in Leeds has a list of books with at least three copies. You want the book with the most copies at the top. What would appear first if you sorted the same list alphabetically instead?
You already know that SELECT chooses columns and FROM names the table. Now read down the copies column. WHERE keeps books with at least three copies. Atlas passes, Sea Stories doesn't, and Cherry Tree and River Walk both pass.
On the left, Cherry Tree moves to the top with eight copies, Atlas sits in the middle with five, and River Walk comes last with three. High to low is descending, written D E S C. On the right, the endpoints swap. Low to high is ascending, written A S C.
You want the book with the most copies at the top of the result. Which instruction tells SQL both which field to sort by and which direction to use? Pick one.
Read the query from the top. SELECT chooses the title and copies columns. FROM names the Books table. WHERE keeps rows with at least three copies. ORDER BY copies descending gives Cherry Tree first with eight, Atlas next with five, and River Walk last with three.
This time the filter has kept Birch with nine copies, Fern with four and Oak with six. You change the sort on copies from descending to ascending. Pick the first and last books in that order.
Keep the SELECT, FROM and WHERE lines the same. Change the last clause to ORDER BY title ascending. You still get the same three books. Read the new result order below the query: Atlas comes first, Cherry Tree comes next and River Walk comes last.
Details has one shelf entry for each book title. Select the title, copies and shelf from the two tables. Match the titles to pair each book with its shelf entry. Keep books with at least three copies. Books dot title names the field you sort from A to Z.
To change the stored data, use different commands. INSERT INTO with VALUES adds Pine Hill, a new title, with four copies. UPDATE with SET changes its copy count to six. DELETE FROM removes that row. In the last two statements, WHERE targets only the Pine Hill row.