The Basics of a SQL Query

SQL as the advantage of being easily understandable. You could show a SQL query to a grandma and she may be able to figure out what’s happening.

In this article I will show you the basics of a SQL query, and you will see that it’s probably easier than you thought.

What is a SQL Query

The goal of a SQL Query is to extract data from a database in a desired format.

Imagine a table that has a few rows and a few columns. You want the data in that table plus or minus some information.

For example, you might want to filter out some information, or join your table to another one to get more information.

A SQL query has basically 2 main functions:

  1. Extract Data
  2. Add or Remove Data

SQL Query Structure

Extract Data

Let’s focus on how to extract data first. To do so, I think you would agree that we first need to select some data from a table. Well, let’s do just that:

The SELECT keyword let’s you choose the columns that you want SQL to return.

The FROM keyword let’s you choose the table you want to query.

Instead of specifying all columns to return, the * symbol let’s you choose them all.

Add or Remove Data

There are many different ways to merge data together and to filter data out in SQL and we won’t go through all of them here.

To filter data out you can use the WHERE clause like below.

In that case you’re not pulling data from the whole table, but only from a subset of that table that is small cones.

To add data you can use the JOIN clause. Using or example above, you might want to know who’s the supplier for each flavor:

The “a” and “b” after the table names are called aliases. They allow you to rename a table so that you can reference it faster later on.

The ON keyword let you specify on which column(s) you want to join your 2 tables.

To join 2 tables with multiple columns you can just chain them with the keyword AND.

That’s everything you need to know to start doing some simple SQL queries πŸ™‚

Subscribe to Receive the Resources that I Use and Recommend – It’s Free

2 thoughts on “The Basics of a SQL Query”

  1. Hi there! Great start to your blog. I saw that you followed me and wanted to reach out and say thanks. One small recommendation I have, for topics like this, is just to make sure you’re using good terminology and solid fundamentals. For example, I would say that “Add or Remove data” is not the right terminology here for SELECT queries; it’s more like “filter” (with the WHERE) or “extract related data” (with a JOIN), especially since the “related” (relational) concept is fundamental to SQL. Good luck and keep up the great work! πŸ™‚

    1. Thanks for reaching out, and taking the time to write this comment πŸ™‚

      I agree with you, I will definitely review and use solid fundamentals.

      Great job will your blog btw I like reading it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.