Menu

SQL

Writing a Query from a Description

To turn a written request into a query, pick out three things: the fields to show, the table to search and the condition the records must meet. Imagine a table TblBooks holding BookID, BookName, Author, Copies and Shelf. A request for “the name and shelf of books with more than 4 copies” needs BookName and Shelf, from TblBooks, where Copies is greater than 4.

Build it in order

Write SELECT first, then your field names separated by commas — never by AND. Next write FROM and the exact table name. Last, write WHERE and the condition, using a single = for equality and quotation marks around text values, such as "Priya Raman". Numbers do not need quotes.

All fields or every record

Put the * wildcard after SELECT only when the request asks for all fields or all details; when it names particular fields, list just those. Leave WHERE out when the request wants every record, because a query with no condition returns them all — adding an invented condition quietly drops rows you were asked for.

Correcting a query

Each part of a statement earns its own mark, so keep the parts that are already right and change only what is wrong. Check one fault at a time: the clause keyword, the field list, the table name, the operator and the quotation marks around text.

Part of the queryWrongRight
Clause keywordIFWHERE
Field listBookName AND AuthorBookName, Author
Table nameTblAuthorTblBooks
Text valueAuthor = Priya RamanAuthor = "Priya Raman"

Practise this