SQL Serie #2 — The SELECT clause

Fabien Fricker
3 min readMay 23, 2024

--

Note: I will use PostgreSQL syntax for all upcoming posts. I find that it is the most commonly used flavor in business (from my experience) and has syntax closest to the standard SQL. Here is a link to the documentation

Designed by vectorjuice

Hello everyone,

Today, we dive into the most significant category of commands for a Data Analyst, known as the Data Query Language (or simply DQL), which is single-handedly represented by the SELECT clause.

If you don’t remember the different categories of commands, I advise you to go back and re-read the first article, which summarizes the different SQL commands available.

Let’s start at the beginning, that is, the SELECT & FROM clauses.

Here is a fictitious table named Sales that I created to show you how the first clauses we are going to use, SELECT and FROM, work. As you can see, the entire dataset contains only 10 rows and relates to sales of different objects.

Sales table created on db-fiddle.com

Let’s break down the first two queries:

Let’s start with the first query. Here, our FROM clause is used to determine the table from which we will work, in our case the Sales table that I previously created. With this, we tell SQL to only manipulate the data of the Sales table.

Next, we have the SELECT clause. As its name indicates, it allows us to specify the columns we want to appear in our result.

Thus, if I translate everything in our first example, we are telling SQL that we want to retrieve all the columns mentioned that are located in the Sales table. As simple as that!

Now, what do you think the second query means? Well, the asterisk allows us to select all columns without having to write them out one by one! Therefore, both queries serve the same purpose: to retrieve all the columns from the Sales table. The advantage of the asterisk is that the syntax is much shorter, which can save us precious time when working with large datasets!

With that, you have successfully completed your first data manipulation in SQL. Congratulations!

Order of operations in SQL

But before I let you go, I would like to briefly talk about the concept of “order of operations” in SQL. As you probably noticed, I wrote the queries in a certain order but explained their meaning in another order. Well, the SQL engine does pretty much the same thing when it executes the query, and this is what we call the execution order.

I will spend more time on this concept in the following posts, especially when I explain how the WHERE clause works. You will have a better understanding of why it is important and helpful to know the logic of execution.

In the meantime, here is a small overview of the differences between lexical order and logical order. Don’t worry if you don’t understand everything yet.

I hope this article has been helpful, feel free to suggest any improvements in the comments.

Happy reading!

--

--