CHAPTER 2The SELECT Statement

Before we can discuss designing analytical datasets, you first need to understand the syntax of Structured Query Language (SQL), so the next several chapters will cover SQL basics. Throughout the book, you will see many variations on the themes introduced here, combining these basic concepts with increasing complexity, because even the most complex SQL queries boil down to the same basic concepts.

The SELECT Statement

The majority of the queries in this book will be SELECT statements. A SELECT statement is SQL code that retrieves data from the database. When used in combination with other SQL keywords, SELECT can be used to view data from a set of columns in a database table, combine data from multiple tables, filter the results, perform calculations, and more.

The Fundamental Syntax Structure of a SELECT Query

SQL SELECT queries follow this basic syntax, though most of the clauses are optional:

  • SELECT [columns to return]
  • FROM [schema.table]
  • WHERE [conditional filter statements]
  • GROUP BY [columns to group on]
  • HAVING [conditional filter statements that are run after grouping] ...

Get SQL for Data Scientists now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.