Menu

SQL

The SELECT Clause

SELECT is the part of an SQL query that lists the fields you want to see. The field names go after the keyword and are separated by commas, for example SELECT StockCode, BookName. Use commas, not AND: AND joins conditions in a WHERE clause, not field names.

SELECT chooses fields, not records. The list after SELECT controls which columns appear; it does not decide which rows are included. If a table called LibraryStock has the fields StockCode, BookName, ShelfCode and LoanDays, then selecting StockCode, BookName shows those two columns only. ShelfCode and LoanDays are hidden, but every record is still present. Filtering records is the job of WHERE, a different clause.

Using the asterisk wildcard

An asterisk (*) after SELECT means every field. SELECT * returns all columns from the table, so you do not have to type each field name. The wildcard means all fields, not all records: it does not add a condition or remove rows. A query using * still returns the records that the rest of the query includes.

What you write after SELECTWhat the result showsWhen to use it
StockCode, BookNameOnly those two fieldsWhen the request asks for particular details
*Every field in the tableWhen the request asks for all fields or all details

Names must match exactly

Field and table names must be exactly as the database stores them, including spelling, capitals and underscores. Never insert a space inside a name: write LibraryStock, not Library Stock. If you ask for a column the table does not hold — for example AuthorName, when LibraryStock only has StockCode, BookName, ShelfCode and LoanDays — the query cannot run, however sensible that extra column sounds. Use the wildcard only when all fields or all details are asked for; otherwise list only the required fields so the result is focused and easy to read.

Practise this