access_time
hourglass_empty
person_outline

What is a Query? Database Query Explained

In regular English, a query means a request for information. What is a query in computer programming then? Simple, it’s the same – except the information is retrieved from a database. This is handy for data manipulation – adding, removing, and changing data.  That’s how we’ll use this word in the article.

However, you don’t just type in a random ‘request.’ You write your query based on a set of pre-defined code, so your database understands the instruction. We refer to this code as the query language.

The standard for database management is Structured Query Language (SQL). Remember, SQL is different from MySQL: the former is the query language, the latter is the software which uses the language. While it is true that SQL is the most popular choice among database software, it is definitely not the only one. Several others are AQL, Datalog, and DMX.

Regardless, these languages make database communication easy.

Getting started with website development? We offer fully-supported web hosting plans for small business and young bright-minded developers.

Learn More

How Does Query Work?

Let’s say that you want to order an Americano at Starbucks. You make a request by saying “Can I have an Americano?”. The Barista will understand the meaning of your request and give you the ordered item.

A query works the same way. It gives meaning to the code used in any query language. Be it SQL or anything else, both the user and the database can always exchange information as long as they ‘speak’ the same language.

You may now think that placing a query is the only way to request data. In fact, quite a few database software options let you use other methods. The most popular ones are:

  • Using available parameters
    The software, by default, has lists of parameters on their menu. Users can choose one, and the system will then guide you to produce the desired output. It’s easy, but not flexible and offers limited operations.
  • Query by example
    The system will show you a set of code with some blank areas, in which you can write and specify the fields and values of your data.
  • Query language
    This is what we’ve been talking about. You have to write the queries from scratch whenever you want to manipulate data. This method requires understanding the query language used by your database software. Although it is complex, it gives you full control over your data.

Examples of A Query

Now that we know quite a bit about queries, why not try writing some? Please be aware that for this exercise we use SQL.

Data source: Participant (Table Name)

Let’s say that you collected some data from a survey. Below is a snippet of your data.

IDNameSexAgeOccupation
1JohnMale17Student
2PeterMale26Unemployed
3MargarethFemale34Teacher
4LeaFemale34Unemployed

Operations

With SQL, you can write either SQL SELECT or SQL ACTION queries. As the name suggests, SQL SELECT lets you select parts of your data, and SQL ACTION enables you to perform action-oriented operations like deleting and inserting data, updating values, and creating new tables. Well, examples speak louder than words, let’s have a look:

  • Selecting only the “Name” and “Occupation” columns from the “Participant” table.
    SQL Statement:
    SELECT Name, Occupation FROM Participant
    Output:

    NameOccupation
    JohnStudent
    PeterUnemployed
    MargarethTeacher
    LeaUnemployed
  • Deleting data from the unemployed respondents.
    SQL Statement:
    DELETE FROM Participant WHERE Occupation = ‘Unemployed’
    Output:

    IDNameSexAgeOccupation
    1JohnMale17Student
    3MargarethFemale34Teacher
  • Inserting a new row containing a participant called Mario.
    SQL Statement:
    INSERT INTO Participant (ID, Name, Sex, Age, Occupation) VALUES (‘5’, ‘Mario’, ‘Male’, ‘67’, ‘Retired’)
    Output:

    IDNameSexAgeOccupation
    1JohnMale17Student
    2PeterMale26Unemployed
    3MargarethFemale34Teacher
    4LeaFemale34Unemployed
    5MarioMale67Retired
  • Changing Margareth’s occupation to “Headmaster”.
    SQL Statement:
    UPDATE Participant SET Occupation = ‘Headmaster’ WHERE ID = ‘3’
    Output:

    IDNameSexAgeOccupation
    1JohnMale17Student
    2PeterMale26Unemployed
    3MargarethFemale34Headmaster
    4LeaFemale34Unemployed

Wrapping up

Some SQL statements, like the ones above, are a solid testament to what a query language can do. It lets you deal with your data more effectively. Imagine if you have thousands of data rows. Manipulating them doesn’t have to be hard. Besides, most query languages are intuitive, meaning that they are easy to learn once you understand some basic rules. Let’s get our hands dirty with database queries and deal with our data like pros!

The Author

Author

Herawan Dwika P. / @dwika

A Psychology and Marketing graduate, Dwika possesses a solid mix of empathy and good eyes for opportunities that lead him towards earning years of experience with WordPress, Linux, and Hosting. When he's not sharing his knowledge with others, Dwika works on his social platform, promoting humanity through the web. He's also a movie enthusiast. He takes them very, very seriously.

Related tutorials

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

Become a part of Hostinger now!

More in Glossary
What is a Pingback in WordPress?

Close