Overview

3 Querying data

This chapter deepens the introduction to querying by unpacking the SELECT statement and its minimum requirements. It explains that retrieving data demands at least two clauses—SELECT (what to return) and FROM (where to get it)—and that clause order matters: SELECT must precede FROM. It clarifies the roles of keywords and reserved words, warning against using them for object names, and notes that while keywords are case-insensitive, identifiers and data may not be. The text also stresses that whitespace and formatting are flexible, encouraging consistent, readable styles (often with uppercase keywords) rather than any single “right” layout.

With the sqlnovel.title table as a working example, the chapter shows how to discover column names in a GUI and then progressively builds queries: selecting one column, then multiple columns separated by commas, in any output order, even repeating columns. It emphasizes that SQL returns rows with no implicit ordering unless explicitly requested, so the same query may display rows in different orders at different times. The discussion highlights practical naming choices (such as TitleName instead of the reserved Name) and introduces column aliases to rename output headers, either with AS or by placing the alias after the column name, while avoiding reserved words as aliases.

Finally, it covers three ways to retrieve all columns: typing them out, dragging them from the GUI into the query, or using SELECT * for quick exploration. While SELECT * is convenient, the chapter cautions against using it in reusable code because it can degrade performance on large tables and break reports when schemas change. The guidance is to reserve SELECT * for ad hoc inspection and otherwise list needed columns explicitly. A short lab reinforces these ideas by having you locate columns in another table, practice selecting multiple fields, reason about missing commas (which cause syntax errors), and recognize that the asterisk represents all columns and cannot be aliased as a single output name.

The database name has been expanded to show Tables, View, Stored Procedures, and Functions.
The Navigator panel, where Tables has been expanded to show individual table names, and the title table has been expanded to show all columns in that table.
The values of the TitleName column in the title table are returned in no particular order.
The results of the values for TitleName are now returned with a column header of BookName.
The column names for the title table highlighted after selecting the first column name and then holding the Shift key and clicking the last column name.
shows the columns of the title table after they have been pasted in the Query panel.

Lab answers

  1. The best method would be to expand the columns folder under the author table in the Navigator panel. If, for some reason, you are using an interface that doesn’t allow you to do that, you can use the SELECT * method in the following query.
  1. The answer is below:
  1. This query will execute, but the results will not be as expected. Since the comma is no longer between TitleName and Price, the word “Price” is now considered a column alias for TitleName. Only one column with an aliased name of Price will be returned, but the values will be from the TitleName column.
  2. This query won’t execute, as you can’t alias column names with SELECT *. If you try this, your first indication of a problem will be the red square with a white X on the line with the alias, as this means the Workbench application has already found your query to have an incorrect syntax.

FAQ

What are the minimum parts of a SELECT query?A query that retrieves data needs at least two clauses: SELECT (which columns to return) and FROM (which table to read). A statement terminator (such as a semicolon) may be required by your tool.
Can I put the FROM clause before SELECT?No. Clause order matters. SELECT must come before FROM; reversing them will cause a syntax error.
Are SQL keywords case-sensitive? What about table and column names?Keywords like SELECT and FROM are case-insensitive. However, object names (tables, columns, and even data values) can be case-sensitive depending on your RDBMS and settings, so use consistent casing and check your system’s behavior.
What are reserved words, and why shouldn’t I use them as identifiers?Reserved words are terms the database uses to recognize commands and special syntax. Using one as a table or column name leads to confusing or invalid SQL. Your IDE typically highlights reserved words; consult your RDBMS documentation for the full list.
Does whitespace or line breaks affect how a query runs?Generally no. Spaces, tabs, and newlines are ignored by the parser, so format your SQL for readability and be consistent.
How can I see the columns in a table before writing my query?In MySQL Workbench, expand your schema in the Navigator, then Tables, then the table, then Columns to view names. As a quick peek, you can also run SELECT * FROM your_table to see column headers (use sparingly).
How do I select multiple columns? What if I forget the comma?List columns separated by commas in the SELECT list. If you omit a comma like SELECT TitleName Price FROM title, many RDBMSs interpret the second word as an alias, returning a single column (TitleName) labeled as Price. This is rarely what you intend, so don’t skip commas.
Can I include the same column more than once? How do I avoid confusion?Yes, you can repeat a column. To keep results clear, assign column aliases, e.g., SELECT TitleName AS BookName, TitleName AS AlsoBookName FROM title.
Should I use SELECT *? Can I alias the star?SELECT * is convenient for ad hoc exploration, but it can be slow on large tables and fragile in reusable code (schema changes alter the output). Avoid it in production queries. You cannot alias the star (SELECT * AS Everything is invalid); alias individual columns instead.
Why do my results appear in different orders between runs?SQL does not guarantee row order unless you explicitly request it. Without ORDER BY, the same query can return rows in different sequences.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn SQL in a Month of Lunches ebook for free