Skip to main content

Command Palette

Search for a command to run...

Composition of a SQL Statement

Updated
•4 min read•View as Markdown
Composition of a SQL Statement
J

I am Data Engineer with a big passion for learning as much as he can. I enjoy the outdoors, mountain biking, finding cool ways to solve new coding problems, and teaching others to code.

When you're learning SQL there are a lot of things to learn, however, the best place to start is to get a good understanding of the general composition of a basic SQL statement, specifically a "select" statement. So let's break down the basic components of a SQL statement.

Components of a Basic SQL Statement

Let's start with the most generic of SQL statements:

select some_column
from some_table
where some_column = 'some value'

You can readily divide this SQL statement into 3 parts:

  • select - "select some_column"

  • from - "from some_table"

  • where - "where some_column = 'some value'"

We'll go through each, but let's start with a metaphor to illustrate what each portion represents.

Say you're getting dressed for the day. You have a closet that houses your clothing. You choose clothing from this closet, with what you choose varying depending on different factors, such as the weather or what activity you have planned for the day. Well, I'd say this particular situation represents a basic SQL statement incredibly well. Let's go through it.

From

Why are we starting with "from"? Well, given our metaphor, this is probably the best place to start. The "from" portion of SQL simply represents which table you are pulling your data from. A table is an object in a database that contains our actual data.

In our metaphor, our "from" would simply be our closet, where all our clothes are contained.

Select

The "select" portion of a SQL statement is simply what we want columns/information we want to get from the table in our "from" statement.

In our metaphor, the "select" would simply be which clothes you want from your closet. You'll likely want a shirt, pants, undergarments, socks, and maybe a jacket.

Where

The final portion of our SQL statement is "where". The "where" simply represents any sort of filtering or criterion we want to place upon our query to restrict or limit the results of our query.

We can utilize any column in our table to filter these results.

In our metaphor, the "where" would be decided based on the weather or activity for our day. Each article of clothing has a particular feature, like what weather or activity it is suitable for.

We'll want to choose different clothes from our closet based on whether is cold or warm outside; whether it's raining or not; if we're exercising; among other deciding factors that change what type of clothes we want to have for the day.

Our Metaphor in SQL

Metaphors are all well and good, but let's build a SQL statement based on our particular metaphor to put what we've learned to use.

Let's say it's winter, and we need warm clothes. So we need a shirt, pants, socks, a jacket, and shoes from our closet. Let's build our SQL:

Select

Our "select" is pretty straightforward, we need our clothes, so the "select" should look something like this:

select shirt, pants, socks, jacket, shoes

Pretty straightforward, right? When you select multiple items, all you have to do is list out each with a comma in between. Very similar to how we would structure a list grammatically.

From

Our "from" is simply from our closet. So let's add that source to our SQL statement:

select shirt, pants, socks, jacket, shoes
from closet

Now we're selecting all the required elements from our closet. However, since it's winter, I need to filter the type of clothes I'm going to get.

Where

Our "where" is probably the most complex portion of this, but it's nothing we can't tackle. We just know we need clothes for the "winter" season. Each article of clothing is specifically meant for a season. So let's add the "where" to our SQL statement:

select shirt, pants, socks, jacket, shoes
from closet
where season = 'winter'

You can see we added a criterion for our selection, but what if we wanted to add another criterion? Say it's below freezing, that set of clothes might look way different than if the temperature is in the 50s; we can utilize this other feature of our clothes as well.

To add additional criteria, you can utilize an "and" between each criterion. So, let's add this new filter to our existing query:

select shirt, pants, socks, jacket, shoes
from closet
where season = 'winter'
    and temperature = 'below freezing'

Pretty easy right? There's always more complexity that can be added, but this should give you a foundational understanding of how the "where" works.

Conclusion

As you can see a SQL statement consists of 3 components:

  • select - what columns/information we are selecting from a table

  • from - the source table of this information

  • where - the criterion we put on our query that filters our results

Once you understand the basics, learning SQL is just a matter of applying similar concepts in different ways!

Thanks for reading, and hope you learned something!

Learn SQL Series

Part 1 of 1

This series will cover basics to more advanced features of SQL, and hopefully provide a good step by step foundation of SQL knowledge.

More from this blog

J

JaggedArray

16 posts

I'm a constantly curious Data Engineer, who loves nothing more than to learn new things, and help others learn new things by making coding approachable.