Overview

2 Your first SQL query

This chapter quickly moves from orientation to action by grounding SQL in familiar concepts and focusing on immediate effectiveness. It frames relational data through the lens of spreadsheets: tables are the primary storage structures, composed of vertical columns and horizontal rows, and each row holds a value for every column. With that foundation, the chapter introduces a small, essential vocabulary—data set, table, column, row, and value—so readers can describe and reason about data the same way an RDBMS does, without getting bogged down in jargon.

SQL is presented as a declarative, English-like language: you state what you want, and the database figures out how to get it. The chapter demonstrates translating a plain-English request into SQL by introducing the core keywords SELECT (what to return) and FROM (where to get it), emphasizing accurate column names, the default of returning all rows unless further constrained, and the importance of ending statements with a semicolon. It also distinguishes a “query” (a statement that retrieves data) from other types of SQL statements and culminates in a simple pattern exemplified by SELECT Name FROM vegetables; as the canonical first query.

Finally, the chapter walks through running your first real query in MySQL Workbench—connecting, selecting the default schema, entering SELECT Outcome FROM MyFirstQuery;, and executing it via menu, hotkeys, or toolbar—then interpreting the results and execution output. It reinforces good habits (consistent semicolon use, awareness of connection context) and shows how result sets reflect the columns selected, even when the output is just one row and one column. The chapter closes by reviewing the key terms and keywords introduced and offering a light lab to solidify the mental model of tables, rows, columns, values, and multi-statement execution.

A spreadsheet with five columns (A through E) of values for several extraordinary fictional books. The structure is organized similarly to a table in a database.
The spreadsheet of book titles, with the Title column highlighted to show the vertical nature of columns.
The spreadsheet of books, with the first horizontal collection of data highlighted to show the horizontal nature of rows.
The value of Price in the row with the title The Sum Also Rises highlighted to indicate a value. This is just one value of many.
A vegetables table with two columns and five rows. We’re going to learn how to create a query that shows the names of all the vegetables.
The MySQL Workbench open with our Month of Lunches connection, with administration information shown in the Navigator panel. We enter queries like the one we just wrote in the Query panel.
The Month of Lunches connection again, but now we see the Schemas information in the Navigator panel. The sqlnovel database is now shown in bold text, indicating it is the default database.
The query entered into the Query panel, ready to execute.
The highlighted plain lightning bolt in the Query panel. Clicking the plain lightning bolt will execute the selected part of the Query panel, which is the same thing that pressing Ctrl + Enter on your keyboard will do.
The results after query execution. In the Result panel, we can see the result is, “Hello, World!” In the Output panel, a green circle with a check mark indicates the query executed successfully, and other information shows the time it was executed, what the query was, how many rows were returned, and the duration of the query execution.

Lab answers

  1. No, it is not possible to have a table with no columns. This is why I mentioned earlier that you could think of a table as a collection of columns. There must always be at least one column; otherwise, there is nowhere to put the values of data.
  2. Yes, you can definitely have tables without rows. In fact, every time a table is created, it initially starts with no rows. This is not uncommon, and it’s something to consider when querying and the result is zero rows.
  3. This is a bit of a tricky question, as it depends on the way the table was set up. We can definitely have rows of valid data that don’t have values for particular columns. For example, think of a column for Middle Name in a table of persons. Not everyone has a middle name, so we have to be able to accommodate that lack of data for rows with people who have no middle name. That said, designers can also put restrictions on tables to require all rows to have a value for particular columns, such as a column capturing a last name for all rows in that same table of persons.
  4. This actually will execute, and will result in two separate data sets being returned. This is the very reason we put those semicolons at the end of our queries, so that the RDBMS knows we have to separate the queries we are executing.

All right, let’s move on to Chapter 3 and learn more fun ways to query using the SELECT keyword!

FAQ

What is a SQL query?A query is a SQL statement that retrieves data. You typically use SELECT ... FROM ... to ask the RDBMS for specific columns from a data set, and it returns a separate result set (not the original table).
How do I write my first query?Example: SELECT Outcome FROM MyFirstQuery; This selects the Outcome column from the MyFirstQuery table and returns the results.
What do the SELECT and FROM keywords do?SELECT specifies which columns to return and how to present them. FROM identifies the data set to read (often a table). If you don’t add any filtering, all rows in that data set are returned for the listed columns.
Do I need to end SQL statements with a semicolon?The semicolon is a statement terminator. Some systems require it, others don’t, but it’s best practice to always end statements with a semicolon.
What’s the difference between a statement and a query?A statement is any instruction you send to the RDBMS. A query is a specific kind of statement that retrieves data (e.g., SELECT ...).
In tables, what do column, row, and value mean? Is “field” the same as column?- Column: a vertical set of attributes across all rows. - Row: one horizontal record of related data. - Value: the specific data in a column for a row. “Field” is commonly used in apps, but the SQL term is “column.”
Can a table have no columns? Can it have zero rows?No columns: not allowed. Zero rows: yes—tables can be empty; they still have a defined set of columns.
How does FROM know whether I’m referencing a table?You don’t say “table” in SQL. The RDBMS resolves the object by name; it could be a table (or other queryable object). FROM vegetables is sufficient.
How do I execute a query in MySQL Workbench?Type the SQL in the Query panel, then: - Click the lightning bolt (Execute Current Statement), - Press Ctrl+Enter (current statement) or Ctrl+Shift+Enter (all), or - Use Query menu options. Set your default schema (e.g., sqlnovel) in the Schemas tab first.
What happens if I run two SELECT statements at once?If you Execute All and separate them with semicolons, both run; Workbench shows separate result grids. Execute Current Statement runs only the one where the cursor is placed.

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