What Is A Query In SQL?

The Q in SQL stands for Query, so, as you can glean, it’s a pretty crucial aspect of this database language, but what does it mean exactly, and how is a query built in SQL?

Well, that’s exactly what I’m going to guide you through here today.

While the nuances of data languages may appear daunting at first, they’re all actually very simple, designed to help rather than hinder us. Stick with me, and you’ll see exactly what I mean!

What Is A Query In SQL

What Is SQL?

The key to understanding queries, or anything about data languages really, is to start with the big picture and work your way down to specifics, so let’s begin with an SQL definition.

Now, you already know what the “Q” stands for, but what about the “S” and the “L”? Any ideas? All together, SQL is an abbreviation of Structured Query Language.

In a nutshell, SQL is a standardized computer language that helps us to navigate and manage computer databases… that’s it.

It’s just a very simple code set we can use to make our lives easier when we’re dealing with large databases.

Got It, But Where Do Queries Come In?

Databases are all about storing data and answering our questions.

If we need to know something, we can consult the database and find the relevant information, but here’s the thing… databases and database objects can be HUGE!

Searching for very specific bits of data in complex databases manually is like searching for a needle in a freakin’ needle stack, so we need a tool that refines our search and guides us directly to the data we’re hoping to find… this is where querying comes in.

Truth be told, there’s really no difference in core meaning between an SQL query and the actual English language word, “query” — they both mean “question”, or in more “computery” terms, requests for data.

What kind of question, you ask? Well… any question really. If the database has the data, you can ask a question about it.

Let’s say, for example, we have a database of album details, and one object within our album database is dedicated to Beatles albums, and perhaps this table is called tblBeatles and contains the following data: Album ID — Album Title — Release Date — Rating.

We may be interested to know the number of Beatles albums that were released during and prior to 1964.

Now, if we were asking a Beatles superfan, we might phrase the question along the lines of… “Do you know how many Beatles albums were released during or before 1964?”

But remember, we’re dealing with a computer here, and SQL is built from the ground up to navigate databases, so we have to phrase our query differently. Here’s how it’s done!

How To Build A Query In SQL

How To Build A Query In SQL

Building a query in SQL is all about taking your “human” question, and replacing the constituent parts of the sentence with keywords in the SQL dictionary.

These keywords will tell the computer what to look for (as in field), where to look for it (as in table), and how to look (the conditions of your request).

Rather confusingly, the corresponding keywords to “What”, “Where”, and “How” are:

Select (What)

When we type in Select, we are essentially telling our computer to go and retrieve something from a certain database or table.

From (Where)

When we follow it up with From, we’re telling our computer where to retrieve the data from.

Where (How)

When we finish things off with Where, we’re disclosing the criteria of our request.

So, our human query, “How many Beatles albums were released during or prior to 1964?” ends up looking a little something like this…

Select Album Title

From tblBeatles

Where Release Date <= 1964

Our computer will then generate the following list:

Beatles 65’
Beatles for Sale
Something New
A Hard Day’s Night
The Beatles’ Long Tall Sally
The Beatles’ Second Album
Twist and Shout
Meet the Beatles
Introducing the Beatles
With the Beatles
Please Please Me

As you can see, our computer has given us the bare minimum information, which is a list of the Beatles albums released during or before 1964, and that is what we asked for, but I bet you expected to see at least the release dates listed as well, right?

Well, while our human Beatles superfan may understand that the implication of the question was to elaborate in certain areas, our computer does not.

We need to be very specific with SQL, as computers can only operate in very literal ways.

Creating Detailed Queries

To add more details, we need to modify our query. For instance, if we wanted the release dates listed as well as the titles, our query becomes…

Select Album Title, Release Date

From tblBeatles

Where Release Date <= 1964

Simple, right? But what if we wanted the results to arrive with all fields of information in the table? Would that mean we have to type in each individual field after “Select”?

Thankfully, no.

All we have to do to specify that we want all fields in our results is to add an asterisk after “Select”, leaving our query looking a little something like this…

Select *

From tblBeatles

Where Release Date <= 1964

Adding More Conditions

If we then wanted to add more conditions to the “Where” stage of our query, all we have to do is separate them with a big, fat AND, which would look like…

Select *

From tblBeatles

Where Release Date <= 1964


Rating >= 8

Now our list would include all Beatles albums released in or before 1964 that are rated at 8 or above, as well as all fields of information corresponding to the shortlisted albums, but that’s not all we can do!

Adding More Modifiers

We can use additional keywords to modify how the results of a query are presented to us.

Say, for instance, that we want our results to be listed by rating, we can add the keyword “Order By” to our command chain, followed by “Rating”,

“Group By” is another cool modifier that will group sourced data in accordance with a certain criterion.

What’s more, if you’re dealing with a particularly large database object, and you don’t want it to pull up every single possible result, you can add a “Limit” modifier to the end, followed by the number of results you want, i.e. Limit 100.

Final Thoughts

There you have it, folks — the basics of querying in SQL.

Now, bear in mind that we’ve only just scraped the surface here today. SQL queries can be as simple or complex as you like, so be sure to familiarize yourself with code syntax and experiment with lots of different requests.

You’ll be a database wiz in no time!

Albert Niall
Latest posts by Albert Niall (see all)