What is a query?

What is a query?

A query is a request for information or an action from a database or search engine. In plain English, a query is simply a question. In a technical context, it’s a specific command sent to a system to retrieve data or perform a task.

While you might use a query to find information in a search engine like Google, the term most often refers to requests sent to a database.

To communicate with a database, you must write these requests in a specific query language, such as Structured Query Language (SQL).

To help you understand how queries work, we will cover:

  • The mechanism behind queries in databases and search engines.
  • The different types of queries used to manage data.
  • The main languages used to write queries.
  • Practical examples of database queries.

For a visual explanation of what a query is, check out this video from Hostinger Academy.

Subscribe For more educational videos! Hostinger Academy

Download glossary for web beginners

How does a query work?

A query works by sending a structured request in a specific language. A system like a database processes the request and returns the needed information or performs an action.

Think of it like ordering at a coffee shop. When you ask for an Americano, you are making a request (a query). The barista understands your language, processes your request (makes the coffee), and gives you the output (your drink).

A database query works the same way: as long as you and the database “speak” the same language, you can request information, and the system provides it.

This process follows a simple flow:

  • Request. You write a command in a query language to ask for specific data.
  • Process. The database management system (DBMS) interprets your command.
  • Output. The system retrieves the data or performs the action and returns a response.

Queries generally fall into two categories:

  • A Select query retrieves information, like fetching a list of all customer names from a table.
  • An Action query manipulates data, such as adding a new customer, updating an address, or deleting a record.

What are the different types of queries?

The main types of database queries are Select queries, which retrieve data, and Action queries, which modify it. You can also combine these operations to perform more complex tasks.

Here are the most common types and the SQL commands that define them:

  • Select query. The most common type of query. It fetches data from one or more tables without changing the data itself.
    • Example: Use the SELECT command to get the names and occupations of everyone in a Participant table.
  • Action queries. These queries perform operations on data, such as creating, changing, or deleting records.
    • Insert query. Adds new records or rows to a table using the INSERT INTO command.
    • Update query. Modifies existing records in a table with the UPDATE command and specific criteria.
    • Delete query. Removes one or more records from a table with the DELETE command, permanently erasing the data.
  • Combined query. This query uses a SELECT statement inside an Action query to identify which records to modify. Developers often achieve this with a subquery in the WHERE clause.
    • Example: Use UPDATE to change the status of all employees in a Sales department, where the department ID is first found using a SELECT subquery.
UPDATE Employees  
SET Status = 'Inactive'  
WHERE DepartmentID IN (SELECT DepartmentID FROM Departments WHERE DepartmentName = 'Sales');

Query languages

To write a query, you need a query language. While several exist, the most common follow either SQL or NoSQL principles.

SQL is the standard language for relational databases. These databases store data in tables with rows and columns.

It’s important to note the difference between SQL and MySQL: SQL is the query language, while MySQL is a popular database management system that uses SQL.

NoSQL (Not Only SQL) refers to languages designed for non-relational databases. These databases handle data more flexibly and store unstructured formats, such as documents or key-value pairs.

A NoSQL database works well when your data doesn’t fit neatly into tables.

Other specialized query languages include ArangoDB Query Language (AQL), Datalog, and Data Mining Extensions (DMX).

Query examples

Let’s look at some practical SQL query examples. Suppose you have a database table named Participant with the following data:

IDNameSexAgeOccupation
1JohnMale17Student
2PeterMale26Unemployed
3MargarethFemale34Teacher
4LeaFemale34Unemployed

Selecting specific data

To retrieve only the Name and Occupation columns from the Participant table, you would use a SELECT query.

SELECT Name, Occupation FROM Participant;

The result would be a new table with only the requested data:

NameOccupation
JohnStudent
PeterUnemployed
MargarethTeacher
LeaUnemployed

Deleting data

To remove all participants whose occupation is Unemployed, you would use a DELETE query with a WHERE clause to specify the condition.

DELETE FROM Participant WHERE Occupation = 'Unemployed';

This command removes the matching rows, leaving this result:

IDNameSexAgeOccupation
1JohnMale17Student
3MargarethFemale34Teacher

Inserting new data

To add a new row to the table for a participant named Mario, you would use an INSERT INTO query.

INSERT INTO Participant (ID, Name, Sex, Age, Occupation)
VALUES (5, 'Mario', 'Male', 29, 'Plumber');

The Participant table would then include the new record.

IDNameSexAgeOccupation
5MarioMale29Plumber

Updating existing data

To change Margareth’s occupation to Headmaster, you can use an UPDATE query. The WHERE clause modifies only the correct record.

UPDATE Participant SET Occupation = 'Headmaster' WHERE ID = 3;

The query finds the row with ID = 3 and updates the Occupation field.

IDNameSexAgeOccupation
3MargarethFemale34Headmaster

A note on search engine queries

It’s also worth noting that the term “query” is used differently for search engines. A search engine query is simply the word or phrase you type into the search bar.

Unlike database queries, these don’t require a special language – the search engine’s algorithm processes your natural language to find relevant results.

How do queries work in databases?

Understanding what queries are and how to write them is the first step in managing data. The next step is knowing where systems execute these queries. Most of the time, that place is a DBMS.

A DBMS provides tools to store, manage, and retrieve data using a query language. MySQL is one of the most popular open-source relational database systems in the world.

To see how queries function in real-world applications, we recommend you to learn what MySQL is.

All of the tutorial content on this website is subject to Hostinger's rigorous editorial standards and values.

Author
The author

Nabilla R.

Nabilla is a website hosting and development enthusiast. She loves to share her knowledge with others in order to help them grow their online presence. When she's not busy writing, Nabilla enjoys exploring nature and going on adventures. She is passionate about helping people achieve their online goals.

Author
The Co-author

Ariffud Muhammad

Ariffud is a Technical Content Writer with an educational background in Informatics. He has extensive expertise in Linux and VPS, authoring over 200 articles on server management and web development. Follow him on LinkedIn.