3 Querying data

 

In chapter 2, we looked at a spreadsheet of fictional books to better understand some core concepts about tables in a relational database management system (RDBMS). With that spreadsheet in mind, we’re going to work with a table that looks a lot like that spreadsheet and see some of the ways we can retrieve data using the SELECT command.

First, though, let’s take a deeper look at your first query. Although it was simple, it had all the minimum components for a query. Let’s briefly examine those components as well as some potential problems regarding formatting and the use of certain words.

3.1 Rules for the SELECT statement

Chapters 1 and 2 discussed the conversational way to think about writing a query, so let’s take a moment to consider the technical aspects and requirements as well. Recall your first query, which looked like this:

SELECT Outcome FROM MyFirstQuery;

This statement has four components, each represented by a single word. Technically, the semicolon is a component as well, serving as the statement terminator, but we’ve already discussed the fact that it may not be required, so we won’t count it.

3.1.1 SELECT requirements

The words “Outcome” and “MyFirstQuery” reflect the data we want to select. These words are crucial because they provide the minimum information the database needs to retrieve data from a table. These requirements are

  • What data is to be selected
  • Where the data is to be selected

3.1.2 Keywords and reserved words

3.1.3 Case insensitivity

3.1.4 Formatting and whitespace

3.2 Retrieving data from a table

3.2.1 Retrieving an individual column

3.2.2 Retrieving multiple columns

3.2.3 Renaming output columns using aliases

3.2.4 Retrieving all columns

3.3 Lab

3.4 Lab answers