What is Query in SQL?

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

If you asked a librarian for ALL books from the library, you would get a funny look. You can ask a computer the same question and it just might try to perform the task. This lesson will discuss a query (question) in SQL and provide code examples.

Using SQL to Get Information

A query is really a question or request for data. For example, ''Tell me how many books there are on computer programming'' or ''How many Rolling Stones albums were produced before 1980?'' When we query databases, we can use a common language to get the information. SQL or Structured Query Language, is fairly universal. There are some different flavors, but once you know the basics you can easily adapt your questions.

Remember that a database stores information in tables, consisting of rows and columns of information. The database fields are the columns. They tell the database what to store, such as an album title. The rows make up the data. Take a look at the sample data set below. The column headings indicate fields, while the data fills in the rows. As it contains information about some albums, the name of the table is appropriately tblAlbum.

albumID albumTitle releaseYear artistID rating
100 Symphony in D Minor 1888 5 10
105 Raised on Radio 1986 10 8.5
110 Poet's Heart 1985 15 9
120 The Wurst Album 1965 20 1

In order to ask a well-defined question, you are going to need to know the following:

  • WHERE are you getting the data from? Which table? (In some cases, which database?)
  • WHAT are you selecting? Which fields?
  • Are there any conditions to the selection? Do you want all albums ever produced, or just those with a rating above 9?

Build a Query

Let's build a query based on the sample data above. We want to select albums that have a rating of 9 or above. In plain terms, we are asking the following of the database:

  • Give us album titles from the database, where the rating is greater than equal to 9

In SQL, we actually have key words that match our statement. We will say SELECT, (go get from the database), FROM (which table are you looking at?), and WHERE (what criteria?).

Therefore, our SQL query matches pretty closely to the plain statement we had earlier:

SELECT albumTitle FROM tblAlbums
WHERE rating >= 9;

We are selecting (SELECT) data from (FROM) the table tblAlbums, where (WHERE) the album rating is 9 or above. The WHERE statement is our filter, and it can be very complex (with lots of nested logic), or it may be very simple.

When the query is run, the following data will be displayed:

Symphony in D Minor
Poet's Heart

This is quite a small list! Where did all the fields go? Recall that computers will only do what you tell them to do. We only asked to pull the album title and nothing else. Now, let's re-work that query a little so that we get some more data. In fact, we want ALL fields.

If we want to get all data, use the asterisk (*) in the code. The asterisk indicates a wildcard, meaning we want everything. This is the equivalent of asking the librarian for ALL books. There's one key difference, however. Again, we only want albums with a rating of 9 or greater!

FROM tblAlbums
WHERE rating >= 9;

Since we pulled all fields, but restricted our criteria with the WHERE clause, we get the following:

albumID albumTitle releaseYear artistID rating
100 Symphony in D Minor 1888 5 10
110 Poet's Heart 1985 15 9

Remember, you need a SELECT and a FROM statement or SQL won't have a clue what to do. A WHERE clause is highly recommended, especially if you have a lot of data (like all books at the library!).

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account